April 18, 2020

How to build query string for System.Net.HttpClient GET Request

In this post I will explain multiple ways to use parameters for HTTP GET Request using System.Net.HttpClient. You don't need to build parameters string by concatenating different values from a name-value collection.

  • First method is to use HttpUtility.ParseQueryString() function.

    var query = HttpUtility.ParseQueryString(string.Empty);
    query["p1"] = "value1";
    query["p2"] = "value2";
    string queryString = query.ToString();
      

    queryString variable contains the parameters in query string format.

  • Another way could be to use UriBuilder class.

    var builder = new UriBuilder("http://example.com");
    builder.Port = -1;
    
    var query = HttpUtility.ParseQueryString(builder.Query);
    query["p1"] = "value1";
    query["p2"] = "value2";
    builder.Query = query.ToString();
    
    string url = builder.ToString();
      

    The url variable will contain both the page URL and query string parameters.

  • If you do not want to include a reference to System.Web in your project, you can use FormDataCollection class by adding reference to System.Net.Http.Formatting.

    var parameters = new Dictionary()
    {
     { "p1", "value1" },
     { "p2", "value2 + SomeOtherValue?" },
    }; 
    
    var queryString = new FormDataCollection(parameters).ReadAsNameValueCollection().ToString();
      

    queryString variable now contains the parameters in query string format.

  • If you are using ASP.Net Core, you can use the QueryHelpers class by first adding reference to Microsoft.AspNetCore.WebUtilities.

    var query = new Dictionary
    {
     ["p1"] = "value1",
     ["p2"] = "value2",
    };
    
    var response = await client.GetAsync(QueryHelpers.AddQueryString("/api/", query));
      

How to solve SQL Server Error 1222 i.e Unlock a SQL Server table

I faced this error while working with transactions in MS SQL Server with a time consuming query. I was inserting data into a table from a select query which was fetching data from a Linked Server. Because of huge data load the server, query was taking too much time, so I finally stopped the query manually. This was the point which caused the SQL Server Error 1222.

 Lock request time out period exceeded

Since the table was being stuck in a transaction, and I was unable to perform any (read/write) operation on the target table.

Here is the solution I found worked for me.

In the SQL Server Management Studio, first find out details of the active transaction. Execute following command

 DBCC OPENTRAN

You will get the detail of the active transaction, including SPID.

If you want to get the details about the SPID, you can use following commands:

 EXEC sp_who2 <SPID>
 EXEC sp_lock <SPID>

This SPID is the one which you have to kill to unlock the table being stuck in the transaction.

For example the SPID in my case is 65. Kill that process using the following command:

 KILL 65

This will unlock the table and you can perform operations on this table without locking error.