Thursday, 20 August 2015

SQL Server - Disable Identity Insert temporarily and enable back

At times, you might want to Disable SQL Server Identity Insert in order to insert some records with ids our of series and after the inserts are done you want to enable back the identity insert.
Find the code snippet below which lets you do it for SQL Server:
SET IDENTITY_INSERT <TableName> ON

-- Insert into the table <TableName> with the ids you prefer.

SET IDENTITY_INSERT <TableName> OFF

SET IDENTITY_INSERT ON allows explicit values to be inserted into the identity column of a table.

Friday, 31 July 2015

SQL Server - Management Studio (SSMS) - Saving Changes is not Permitted

When I try to save changes to edit columns in a table and the change is very straight forward like
  • converting the type from int to long or 
  • changing the column name,
SSMS doesn't allow that. This happens even when the table has no data. Thus I am forced to drop the table and recreate it. The error I receive from SSMS (Sql Server Management Studio) while trying to save changes to data is:

"Saving changes is not permitted. The change you have made requires the following table to be dropped and re-created. You have either made changes to a table that can't be recreated or enabled the option prevent saving changes that require the table to be re-created."


Error Message from SQL Server Managemet Studio

This can be fixed by changing some options in SSMS.
Goto Menu
TOOLS > OPTIONS > DESIGNERS (in Options window)
Select the "Prevent saving changes that require table re-creation" option.



Now you are good to go. You can make changes to your table without the error message form SSMS.

SQL Server - Saving Changes is not Permitted

When I try to save changes to edit columns in a table and the change is very straight forward like
  • converting the type from int to long or 
  • changing the column name,
SSMS doesn't allow that. This happens even when the table has no data. Thus I am forced to drop the table and recreate it. The error I receive from SSMS (Sql Server Management Studio) while trying to save changes to data is:

"Saving changes is not permitted. The change you have made requires the following table to be dropped and re-created. You have either made changes to a table that can't be recreated or enabled the option prevent saving changes that require the table to be re-created."


Error Message from SQL Server Managemet Studio

This can be fixed by changing some options in SSMS.
Goto Menu
TOOLS > OPTIONS > DESIGNERS (in Options window)
Select the "Prevent saving changes that require table re-creation" option.



Now you are good to go. You can make changes to your table without the error message form SSMS.

Tuesday, 26 May 2015

Delete partitions on USB Flash Drive

Today, I was using my USB Flash Drive on my MAC and had to format from MAC and accidently created a partition of 200MB. This made only 200MB accessible on my Drive usable from my windows machine. Even from Disk Management tool on windows, I couldn't do much to claim back my 16GB.
Finally Diskpart tool in windows came for my help.

Follow the below steps to delete all the partition on your drive and get back the full space to use:
  1. Open Command Prompt in Administrative mode (elevated mode).
  2. In the prompt, Type Diskpart
  3. In the prompt, Type List disk
  4. Note the disk number that corresponds to your USB drive (it should be obvious going by size)
  5. select disk X where X is the number from step 4
  6. In the prompt, Type list partition - There should be two, numbered 0 and 1, each about 7 GB
  7. In the prompt, Type select partition 0
  8. In the prompt, Type delete partition
  9. In the prompt, Type select partition 1
  10. In the prompt, Type delete partition
  11. In the prompt, Type create partition primary
  12. In the prompt, Type exit
  13. Exit Command Prompt (type exit or just close the window)
  14. In Windows, go to Computer and try to open the disk. It will ask you to format it.
  15. Format it with the default settings and give it a name if you want.
  16. It should now a single, unified partitioned drive.

Sunday, 3 May 2015

C# - WebClient Authentication not working

Today I had a scenario where I was using WebClient class to POST XML to a particular Url.

Even after many trials, the authentication was not working fine.

Finally I had to do it the old fashion way of specifying the authorization credentials in the header.

This scenario happens because WebClient doesn't send the Authorization header untill it receives a 401 status.

The code that failed:

string xml = @"<messages>......</messages> ;
string url = new UriBuilder("http", "www.abc.com", 9443).ToString();

// create a client object
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Credentials = new NetworkCredential("user1", "password1");
// performs an HTTP POST
string resp = client.UploadString(url, xml);
txtResponse.Text = "Response:" + resp;
}


The workaround:

string xml = @"<messages>......</messages> ;
string url = new UriBuilder("http", "www.abc.com", 9443).ToString();

// create a client object
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("user1:password1"));
// performs an HTTP POST
string resp = client.UploadString(url, xml);
txtResponse.Text = "Response:" + resp;
}

Monday, 20 April 2015

ASP.NET - Current Page Url with QueryString

You might need to extract different values from url.
It might be the path or the current page or values in the querystring.
Here is a simple reference to different uri based info available to you out of the box.

SAMPLE URL:
http://habeeb.in/UrlTest/Default.aspx?QueryString1=1&QueryString2=2

CODE:
Response.Write( Request.Url.Host );
Response.Write( Request.Url.AbsolutePath );
Response.Write( Request.ApplicationPath );
Response.Write( Request.Url.AbsoluteUri );
Response.Write( Request.Url.PathAndQuery );

OUTPUT:
habeeb.in
/UrlTest/Default.aspx
/UrlTest
http://habeeb.in/UrlTest/Default.aspx?QueryString1=1&QueryString2=2
/UrlTest/Default.aspx?QueryString1=1&QueryString2=2

The above sample "Request.Url" is when you use in ASP.NET Code behind.
If you use it within a class library then you will have to refer it as ""HttpContext.Current.Request.Url".

Sunday, 12 April 2015

IIS - Restrict IP Address Range

Allowing and restricting visits from certain IPs or IP ranges can be easily done on IIS.
Working with a simple IP is easy. Working on an IP range is a bit more complicated.
We will discuss about it here. I will discuss about the restriction case and you can follow the same method for "Allow IP " case also.
Follow the below steps:
  1. Make sure "IP Security" feature is installed on your IIS server machine.

    • Goto Control Panel > On Left Pane select "Turn Windows features on or off".
    • On Windows Features window, select "IP Security" and click "OK".
    • Installing this you might be prompted for a reboot.

  2. Now browse to your website on IIS (Internet Information Services Management Services Console) and Open (Double Click) "IP Address and Domain Restrictions".

  3. Right Click > Select option "Add Deny Entry".

  4. Enter the IP Address Range and Subnet Mask to restrict.
    • In the above example all IPs in the range: 115.164.0.1 - 115.164.255.254 will be blocked.
    • To help you calculate the subnet mask, use the tool: http://www.subnet-calculator.com/subnet.php