ASP.NET: SQLDataSource QueryString Handling

I have seen a lot of developers using the QueryStringParameter with the SQLDataSource for demo ASP.NET applications. A frequently asked question is how to perform checks in cases where the parameter has the value null or Nothing and supply a default value to it.

You can use two ways:

DefaultValue property of the ASP.NET parameter Object


When a parameter has the value null or no parameter is passed, the DefaultValue is used for the value of the parameter.

Using the SQLDataSource_Selecting event

To validate the value of the QueryStringParameter, handle the Selecting event, as shown below:

protected void objArticlesbyAuthor_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
if (Request.QueryString["productID"] != null)
e.Command.Parameters["@productID"].Value =
e.Command.Parameters["@productID"].Value = 23;

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: