public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Execute Web Service via GET Method

Posted by Virendra Dugar on September 8, 2009

When we created any web service via Vistual Studio, we always get a default method named “Hello world”.

When you hit this URL in your browser, http://localhost/SampleWebService/Service.asmx, it shows list of all the web method. Click on Hello World and then Invoke. It gives following output:

I am assuming that you have hosted your web service in IIS.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<string xmlns=”“>Hello World</string>

Looks good. Now let’s go and modify our service.cs file and add a new method which takes one parameter.

public string HelloWorldWithParam(int a)
return "Hello World " + a.ToString();

Now, hit this URL http://localhost/SampleWebService/Service.asmx. It is showing both the methods.


Click on HelloWorldWithParam. It asks for parameter value. Without giving any value just click invoke.


It shows following error:


Above error is coming as method is expecting a value, and method tries to convert null value to string. If you refresh your browser then following error comes:


Now let’s provide the parameter value via URL only. Hit this URL, http://localhost/SampleWebService/Service.asmx/HelloWorldWithParam?a=1 , and then also you will receive the above error.

Reason: By default, Web service created via Visual Studio executes through HTTP Post method. When Post method is used, querystring is not visible. We need to configure our web service, so it works with HTTP GET and POST method. How can we do this?

Go to web.config and add this code in system.web section.

        <add name=”HttpGet”/>
        <add name=”HttpPost”/>

Good to go now. Hit this URL again and this time you will see “Hello World 1”.



Posted in ASP.NET | Tagged: | 1 Comment »

Difference between Theme and StylesheetTheme attribute

Posted by Virendra Dugar on August 31, 2009

Learn new ASP.NET Core 1.0 Framework

The Page directive includes the attribute Theme and StylesheetTheme.You can use both to apply themes to a page. So, the question is: If you have a Theme attribute and a StylesheetTheme attribute for the Page directive, what is the difference between the two?

The StylesheetTheme attribute works the same as the Theme attribute. The difference is that the when attributes are set locally on the page within a particular control, the attributes are overridden by the theme if you use the Theme attribute. They are kept in place, however, if you apply the page’s theme using the StylesheetTheme attribute. Suppose you have a text box control like the following:


In this example, the ForeColor settings is overridden by the theme if you have applied theme using the Theme attribute in the Page directive. If, you applied the theme using the StylesheetTheme attribute in the Page directive, the ForeColor settings remain in place, even if they are explicitly defined in the theme.

Quick summary of what’s changed in ASP.NET Core 1.0


Posted in ASP.NET | Tagged: | 1 Comment »

Case Sensitive Comparison in SQL Server

Posted by Virendra Dugar on July 24, 2009

SQL Server performs the string comparison irrespective of the case. For eg. “VIRENDRA” and ‘virendra’ are equal when we do comparison in SQL Server.

I found an article which gives complete detail about case sensitive string comparison.

Below are the techniques to do case sensitive comparison

  • Converting data to binary type before comparison.
  • Using the COLLATE clause to dictate the case sensitiveness of the query.
  • Using BINARY_CHECKSUM function.
  • Changing the collation of the column permanently, so that all comparisons are case sensitive by default.
  • Using computed columns.

Check the original article for more details.


Posted in SQL Server | Tagged: | Leave a Comment »

JavaScript Best Pratices

Posted by Virendra Dugar on July 17, 2009

Check this article on Best Pratices of JavaScript.


Posted in JavaScript | Tagged: | Leave a Comment »

Access ViewState Across the Pages

Posted by Virendra Dugar on July 2, 2009

Before I start this article, Let me ask you something. Is it possible to access the ViewState variable of one page to another page? I don’t know what is your answer. Well, frankly speaking, My answer was  also “NO”  before writing this article As it is said that ViewState is page specific that means it is available on the same page only on which it is created. Once you redirect to another page, previous page viewstate is no more accessible. But that is not true.Yes, we can access the viewstate variables across the pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user on other page. If Response.redirect is used then ViewState can not be accessed across the pages.

Before you go down, Please read these articles on Cross Page Posting and Server.transfer.

Ok, So all set now.. I will demonstrate this using the demo created by me. You can download the demo from here.

I have created two .aspx page named
1. ViewStateContainer.aspx : This page set the ViewState variable and transfers the user to another page using Server.transfer.
2. AccessViewState.aspx : This page access the ViewState variable of ViewStateContainer.aspx Page.

This is the code of ViewStateContainer.aspx Page :

ViewState Container Page

As you can see, I have set a ViewState variable in Page Load and transfers the user to AccessViewState.aspx page using Server.transfer() method.
This page also contains a method ReturnViewState() which actually returns the ViewState of this page to the calling function. Return type of the method is StateBag Class.

StateBag Class : This class is the primary storage mechanism for all HTML and Web server controls. It stores attribute/value pairs as strings associated with the control. It tracks changes to these attributes only after the OnInit method is executed for a page request, and saves the changes to the page’s or control’s view state.

Now let’s take look at AccessViewState.aspx Page code:

Access View State

Whenever we use Server.transfer or Cross Page Posting, We can get the previous page object via PreviousPage Property. Using Previous Page, we can find the controls of previous page. As for example, One can access Label control placed in ViewStateContainer Page in Current Page.

Looking at the code, I have created a PreviousPageViewState property in this page, which returns the previous page’s ViewState. It first checks whether PreviousPage is null or not, if it’s not null then create an object of the previous page. Now using Reflection, we can invoke the method of the previous class. Using MethodInfo class, I have invoked the ReturnViewState() method of ViewStateContainer Page.

In Page_Load event, I am able to access the ViewState variable of  ViewStateContainer Page. You can access all the viewstate variables set in ViewStateContainer Page.


Posted in ASP.NET | Tagged: | Leave a Comment »

Get size of a file placed on any web server

Posted by Virendra Dugar on June 25, 2009

Download Source Code

This post demonstrate you that how can you find the size of file which is placed on any webserver.

I have placed a label, a textbox and a button on an aspx page.  Textbox will be having valid URL with the file name.

On button click, Place this code


That’s it..


Posted in ASP.NET | Tagged: | Leave a Comment »

Using C# and VB.NET classes together in the App_Code folder

Posted by Virendra Dugar on June 15, 2009

Download Source Code

Learn new ASP.NET Core 1.0 Framework

In ASP.NET 2.0 one can mix web forms coded in C# and VB.NET together in a single web site. This works great for web forms. However if you want to code classes from App_Code folder in different languages? Such mixing of classes coded in different languages is not allowed with default settings. You can, however,configure your web site to get this done. This article is going to explain you that how one can do that.

Creating sub folders and classes

First of all, Create a new webSite in VS.NET 2005. Add App_Code folder to it. You can do so easily by right clicking on the web site in the Solution Explorer and choosing Add ASP.NET Folder” option (see below).

Different Language

Once you add the App_Code folder add two class files – Class1.cs and Class2.vb. Note that one class file must be in C# where as the other must be in VB.NET. Add the following code in Class1.cs.
C# Code

Similarly, add the following code in Class2.vb.


Both of these classes contain a method called HelloWorld() that simply return a string to the caller.

Now try compiling the web site. What happens? You will get an error as shown below:

Error 1 The files ‘/VBandCSharptogether/App_Code/Class2.vb’  and ‘/VBandCSharptogether/App_Code/Class1.cs’ use a
different language, which is not allowed since they need to be compiled together.

The error message bluntly tells us that you can not use different coding languages for the classes in App_Code folder. Fortunately, there is a way to get out of this trap. Firstly you need to put C# and VB.NET classes in separate sub-folders under App_Code. Secondly you need to add some markup in the web.config file to tell ASP.NET compiler about your intention.

<codeSubDirectories> Section

Create two folders under App_Code filder named CSCode and VBCode. Move Class1.cs inside CSCode folder and Class2.vb inside VBCode folder.

Add a web.config file to your web site and add the following markup to it:

Web Config Code

Here, we added <compilation> section. The <codeSubDirectories> section defines a set of sub-directories relative to App_Code folder that are compiled at run time. The directoryName attribute points to the sub-folder of App_code. Each sub folder is compiled separately and hence each can have classes coded in different languages. We added our CSCode and VBCode folder in this section. This way the compiler will compile classes from CSCode and VBCode folders separately.

After configuring the web site try to compile it. This time it compiles successfully.


Quick summary of what’s changed in ASP.NET Core 1.0

Posted in ASP.NET, C#, VB.NET | Tagged: , , | 18 Comments »

Exception Handling Best Practices in .NET

Posted by Virendra Dugar on June 10, 2009

Check out this cool article on code project by Daniel Turini on best practice of Exception Handling in .NET.

A very good article indeed.


Posted in ASP.NET | Tagged: | Leave a Comment »

Textbox with autopostback and regularexpression validator

Posted by Virendra Dugar on June 5, 2009

Quick summary of what’s changed in ASP.NET Core 1.0

Suppose you have a textbox on your aspx page that has a autopostback set to true and It makes a call to server side function on TextChanged Event. You have also placed a RegularExpression Validator which validates the textbox value against the validation expression.

For example: You want to allow only numeric characters in the text box and you have placed a regular expression validator to achieve this. And you also want to do some server side operation on text change.

 TextBox and RegularExpression Validator
When you run the application and if you type any alphabets in the textbox, the regular expression validates the input, it shows the error message but the weird thing is it also does the post back and call the server side text change event and error message is gone. This is what you don’t want. TextChange event must be called only when inputs are correct. Then how do you do this?

A very simple solution. Just set the CausesValidation="true" for the textbox and you are done.


Learn new ASP.NET Core 1.0 Framework

Posted in ASP.NET | Tagged: | 11 Comments »

No need to type runat=”server” now

Posted by Virendra Dugar on June 1, 2009

If you are using Visual Studio 2005 or 2008, for every or Html control, you need to explicity write runat=”server” attribute. Sometimes I really wonder, why I have to write this attribute for every control as most of time I want to access controls on server side. Is n’t is tedious task? It should be added automatically. But it’s not possible with Visual Studio 2005 or 2008.

Well, we have a good news now. Now there is no need to type runat=”server“. It does not mean that It has been removed. Nope.. it’s get added automatically in VS 2010. Wow.. amazing..

In VS 2010, Microsoft has added hundereds of new code snippest for HTML, JavaScript and for ASPX.  If you want to place a textbox on the page, type tb and press Tab. Wow… You will see something like this..

<asp:TextBox ID="TextBox1" runat="server"/>

Type req and press Tab. Boom…

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="errormessage"></asp:RequiredFieldValidator>

There are lots of other code snippest almost for every control. Even for HTML tag also like table. Is n’t is cool?

Check out this video by Jeff king on new web development features of VS 2010.


Posted in ASP.NET 4.0 | Tagged: | Leave a Comment »

%d bloggers like this: