How to pass two parameters in query string?

77,607

Solution 1

It's usually much easier to read using String.Format:

Response.Redirect(String.Format("ViewCartItems.aspx?CartID={0}&ProductID={1}", id, productid));

Also, it is prefable to use Response.Redirect(url, false) instead of just Response.Redirect(url), so you don't get a ThreadAbortException.

From MSDN:

When you use this method in a page handler to terminate a request for one page and start a new request for another page, set endResponse to false and then call the CompleteRequest method. If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes. This exception has a detrimental effect on Web application performance, which is why passing false for the endResponse parameter is recommended.

Reading: Response.Redirect

Solution 2

You need to concatenate the values into the string:

Response.Redirect("ViewCartItems.aspx?CartID=" + id.ToString() + "&ProductID=" + productid.ToString());

Solution 3

You are putting space between '&', 'variable name' , '='.

Don't put space. Write like this: &name=, not like & name =.

Response.Redirect("ViewCartItems.aspx?CartID="+id+"&ProductID="+productid);

This will work.

Share:
77,607
VJain
Author by

VJain

I am a computer science student and i just love programming.

Updated on July 09, 2022

Comments

  • VJain
    VJain almost 2 years

    I want to pass two parametrs cartid and productis via query string. Cartid is to be generated either from session(if available) else from database and Product is to be fetch from previous query sting

    My code is(in case cart id is to be fetch from database)

            CartInfo cartinfo = new CartInfo();
            cartinfo.UserName = Session["UserName"].ToString();
            cartinfo.IsOrder = "0";
            cartinfo.CartDate = DateTime.Now;
            int id = new InsertAction().InsertData(cartinfo);
            if (id!=0)
            {
                lblmsg.Text = "Inserted Sucessfully";
                Session["CartID"] = id;
                if (Request.QueryString["ProductID"] != null)
                {
                   int productid = int.Parse(Request.QueryString["ProductID"]);
                }
                Response.Redirect("ViewCartItems.aspx?CartID=id & ProductID=productid");
    
            }
    

    and in case cartid is to be fetch from the session created

    if (Session["CartID"] != null)
            {
                string cartid;
                int productid;
                if (Request.QueryString["ProductID"] != null)
                {
                    cartid = Session["CartID"].ToString();
                    productid = int.Parse(Request.QueryString["ProductID"]);
                    DataSet ds = new AddCartItem().GetCartItem(cartid, productid);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataSet ds1 = new AddCartItem().UpdateCartItem(cartid, productid);
    
                    }
    

    but both the queries are wrong the are generating url like this

    http://localhost:1030/SShopping%20Website/client/ViewCartItems.aspx?CartID=id%20&%20ProductID=productid
    

    Please help