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

Recommended Answers

All 5 Replies

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

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.

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

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

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.