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

sqldatasourcequerystring

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 =
Request.QueryString["productID"];
else
e.Command.Parameters["@productID"].Value = 23;
}





About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: