0

Hi,

Is there a way to set a default query string if query string is null?

Something like:

if (Request.QueryString["ID"] == null)
        {
            //set default here
        }

Many Thanks

Grant

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Ramesh S
0

Hi,

Is there a way to set a default query string if query string is null?

Something like:

if (Request.QueryString["ID"] == null)
        {
            //set default here
        }

Many Thanks

Grant

Hi

You can assign like this

Dim str as string 
str = Request.QueryString("ID")
if isnothing(str) then
 ''\\ do your stuff
end if

if this answer helps you. pls. mark as Solved

Edited by reach_yousuf: n/a

1

Hi Grant,
Request.QueryString["ID"] is read only collection which means that you cannot set/insert a value to the collection. But you can retrieve the value from the collection if exists. Request.QueryString is internally populated by ASP.NET when you pass query strings explicitly from a page.

0

Hi Thanks for the replys,

The issue lies within my input for which gets the ID from the querystring to add intot he database. And if someone goes onto the page without searching where a querystring is passed, the querystring will be blank and my input form will fail if someone trys to add a record.

command.Parameters.AddWithValue("@supplier_id", Convert.ToInt32(Request.QueryString["ID"]));

would there be a better way to get the ID, or perhaps i could modify the above to default to a ID?

Thanks

Grant

0

Decided to do it like this:

if (Request.QueryString["ID"] != null)
            {
                command.Parameters.AddWithValue("@supplier_id", Convert.ToInt32(Request.QueryString["ID"]));
            }
            else
            {
                command.Parameters.AddWithValue("@supplier_id", 2);
            }

If theres a better way let me know!

Grant

Edited by gdp_87: n/a

1

Decided to do it like this:

if (Request.QueryString["ID"] != null)
            {
                command.Parameters.AddWithValue("@supplier_id", Convert.ToInt32(Request.QueryString["ID"]));
            }
            else
            {
                command.Parameters.AddWithValue("@supplier_id", 2);
            }

If theres a better way let me know!

Grant

Hi Grant,

The above approach seems to be okay. But still you can reduce the number of lines in the following way.

int supplierId = (Request.QueryString["ID"] != null) ? Convert.ToInt32(Request.QueryString["ID"]) : 2;        
command.Parameters.AddWithValue("@supplier_id", Convert.ToInt32(supplierId);

Edited by Ramesh S: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.