January 13, 2014

I have noticed few times that when a new column is added to SQL server database table, the column gets added from design view and I can write a select query for it. But in the select query, the column name gets highlighted and says the column name is not valid even when  the select query works. An example is below.

January 11, 2014

Few days back, while I was at work, I found that my computer running slow. I quickly checked the task manager and found that I was running out of memory. That was a bit strange as I have 8GB of memory. Anyways, what I found is there were lot of processes running from Google Chrome that was killing it. Basically, each of the tab is a process and there is no way to add up the full memory usage in Task Manager. So, I did something silly and add up the memory from each of the processes in the calculator. But that's silly!

January 10, 2014

Query xml from sql

sSql Server has the option to store XML data. This is great as it means we can store xml data and also run sql scripts on it. Yes, we can run sql scripts to retrieve certain fields within the xml.  Ti start with, let's use the Customer table that I created in previous post and add a new field Person that is of type xml.

January 8, 2014

html import–a reality

Few days back I was reading an article on html5rocks.com about html imports and I was delighted even it's experimental at this stage. Html import did not exist and was always an import feature to me for web development. I remember creating asp pages for small sites when html would have easily suffice. I needed to add header / footer and using asp for just that did not make any sense to me. Html had ways for importing javascript and css and I was always wondered about html imports. I also used iframes and frames instead but never really liked the way frames work.

So, how does html import work? Well, it does not work in most browsers. In fact, it only works in Chrome 31+ when "html imports" is turned on. It is important to note that it is a standard mentioned in W3C so the feature might become common in other browsers. So, how do I write the import code snippet. Here's how.

  1. <link rel="import" id="id1" href="page2.html" />


As you can see, it's similar to css import except the rel attribute value is now import. So, as a next step, I ran the page in the Chrome browser and it does not do anything. Oh, I need to turn on the html import feature first. To do so, I went to chrome://flags/#enable-html-imports and clicked on enabled and fingers crossed.


But no luck, nothing happened. I could see a flicker that the document is loaded while running the browser but it does not appear in the display. After more readings, I figured html import only the import the file. To display the content or a portion of the content from the file, we need to use javascript. Here's the javascript and the full page.

  1. <!DOCTYPE html>
  2. <html><!-- xmlns="http://www.w3.org/1999/xhtml">-->
  3. <head>
  4.         <title></title>
  5.     <link rel="import" id="id1" href="page2.html" />
  6. </head>
  7. <body>
  8.       <div id="page1div"></div>
  10. <script>
  11.      var content = document.querySelector('link[rel="import"]').import;
  12.      var el = content.querySelector('#page2div'); //id of div whose content will be displayed
  13.      document.getElementById("page1div").appendChild(el.cloneNode(true));
  14.    </script>
  15. </body>
  16. </html>


  1. <div id="main">main content
  2. <div id="page2div">page 2 content</div>
  3. </div>


The javascript uses the .import selector to get the content from the html document. I then select the div from imported html document whose content will be displayed and clone the node. Then, I run the page and that's it. It works now.

January 7, 2014

Consume Web Service using jQuery

jQuery is a javascript library than can be used to call web services among many other functions. In this example, I will create a web service using asmx and use jQuery to pass and retrieve data from the web service using json. This is really useful as this means we can consume the web service from any platform like jsp, php and not only from asp.net. I have used json for transmitting data and it is in a platform independent technology that transmits data in an "attribute : value" format.

For this example, I will start with the web service. It's going to be asmx service that simply adds and subtracts numbers that it retrieves from the front end. Here's the code for the web service.

web service
  1. using System.Web.Services;
  3. namespace WebApplication1
  4. {
  5.     [WebService(Namespace = "http://tempuri.org/")]
  6.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  7.     [System.ComponentModel.ToolboxItem(false)]
  8.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  9.     [System.Web.Script.Services.ScriptService]
  10.     public class WSPrac : System.Web.Services.WebService
  11.     {
  12.         [WebMethod]
  13.         public double Add(double a, double b)
  14.         {
  15.             return a + b;
  16.         }
  18.         [WebMethod]
  19.         public double Substract(double a, double b)
  20.         {
  21.             return a - b;
  22.         }
  23.     }
  24. }

The code self explanatory. The important bit to note is that the line "[System.Web.Script.Services.ScriptService]" needs to be uncommented. This will allow the service to be called using javascript.

On the front end, I have couple of text fields, 1 option field to select to add or to subtract, a button that will trigger the script when clicked and a span element that will display the data. Here's the code for the html front end.

html front end
  1. <body>
  2.     <input type="text" id="t1" />
  4.     <select id="sel">
  5.         <option value="Substract">Substract</option>
  6.                 <option value="Add">Add</option>
  7.     </select>
  9.     <input type="text" id="t2" />
  10.     <input type="button" id="b2" value="Calc" /><br />
  11.     <span id="span1"></span>
  12. </body>

In the last bit is the javascript section. Here's the code for this.

jQuery block
  1. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  2.        <script type="text/javascript">
  4.     $(document).ready(function () {            
  5.         $('#b2').click(function () {
  6.             var url = "WSPrac.asmx/" + $('#sel').val();
  7.             $.ajax({
  8.                 type: "POST",
  9.                 contentType: "application/json; charset=utf-8",
  10.                 url: url,
  11.                 data: "{ a: '" + $('#t1').val() + "',  b: '" + $('#t2').val() + "'}",
  12.                 dataType: "json",
  13.                 success: function (data) {
  14.                     $("#span1").html(data.d);                  
  15.                 },
  16.                 error: function () {
  17.                     $("#span1").html("Error!!!");
  18.                 }                   
  19.         });
  20.     });
  21.     });
  22. </script>

To start with I am calling jquery source file. Next, in the button click handler, I am forming the service url based on the selection. The important bit is this line

  1. data: "{ a: '" + $('#t1').val() + "',  b: '" + $('#t2').val() + "'}"


Note that a and b are parameter names in the service and I am sending the value of the text boxes. Finally, on success, I am populating the data in the span element. In case, error occurs, for example, if we type in "abc" in the text box or the web service is down, then the error function is fired and the text "Error!!!" is displayed within the span element.

Another thing to note is that the return value in json has .d at the end. This is within the success function as below.

  1. success: function (data) {
  2.     $("#span1").html(data.d);                  
  3. }

The .d is a wrapper that ensures the payload is a json object.

January 6, 2014

A unique key is a field in a table that appears only once in the table and therefore can uniquely identify the table. It's similar to a primary key in the database but it differs in some ways including that it can accept null value. The null value can appear only once for the unique column in the table.

To add a unique key using sql server management studio, and click on indexes / keys option. The following popup will display.



From the popup options, select the columns that need to be unique, choose Is Unique to Yes and set the type as Unique Key. Click close and save the changes to the database.

January 5, 2014

Add CSS File using Javascript

Let's imagine a situation where we want to load a css file using javascript. This could be, for example, a situation where we want to add a new stylesheet based on an user action. To do this using javascript is similar to adding a new element in the html file using javascript. Here's the source code on how to achieve this.

I recently deployed a site and faced a strange error where the website was failing to connect to Sql Server database. It was giving the error - cannot connect via NTAuthority login. This was strange as I had the username and password for the sql database specified in the connection string. Here's is how it looked like.

January 4, 2014

Register asp.net on IIS

I have worked on setting up windows servers and then IIS and sometimes I have noticed that IIS does not recognise asp.net straight way. If that happens, the error message - Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list - is displayed on the browser when the asp.net site is run. Html files will still work at this stage.

To workaround this issue, asp.net will need to be registered again. To do so, open command prompt and change the directory to point to .net framework. In 32 bit system it will be at %windir%\Microsoft.NET\Framework\v4.0.21006\ and in 64 bit system it will be at %windir%\Microsoft.NET\Framework64\v4.0.30319\ .

  1. %windir%\Microsoft.NET\Framework64\v4.0.30319\

Run the command aspnet_regiis.exe -i .

  1. aspnet_regiis.exe -i

This will register asp.net . The following screen shot will appear on command prompt.


Reference: Shahed Kazi at AspNetify.com