简体   繁体   中英

why is my parameter value not being passed in QueryString

I have a parameter:

string custName = "";

custName = AddressDT.Rows[0]["first_name"].ToString() + " " + AddressDT.Rows[0]["last_name"].ToString();

I am performing my Response.Redirect

Response.Redirect("~/Account/EmailError.aspx?parm1=custName");

and I am retrieving the parameter value:

custName = Request.QueryString["parm1"];

when I run debug: ... custName = "custName"

what am I doing wrong? I have done this before with no issue.

Your Response.Redirect is passing the string "custName", not the value of the variable.

This will fix it for you:

Response.Redirect("~/Account/EmailError.aspx?param1=" + custName);

That is because you're using a String Constant as the value for the parameter.

Response.Redirect("~/Account/EmailError.aspx?parm1=custName");

That would always cause the URL to be set with the string custName .

Try using the variable name as:

Response.Redirect("~/Account/EmailError.aspx?parm1=" + custName);

Now the variable would be appended to the request.

Now when you'll run the code, it would produce the value in the Variable and you'll get the code running fine.

Always remember to finish the string and then adding the variable values. Everything inside the double qoutes is considered to be a string and not a variable name or a keyword.

If you already know that QueryString value is string, you need to use UrlEncode .

In addition, you want to use String.Format as much as possible for good design practice instead of +.

string custName = String.Format("{0} {1}", 
   AddressDT.Rows[0]["first_name"].ToString(), 
   AddressDT.Rows[0]["last_name"].ToString());

string url = String.Format("~/Account/EmailError.aspx?parm1={0}", 
   Server.UrlEncode(custName));

Response.Redirect(url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM