Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am getting this error while accessing data:
"The name 'sEcode' does not exist in the current context"

The code snippet which causes this is below

C#
protected void btnEmpDet_Click(object sender, EventArgs e)
{
    Response.Redirect("empfetch.aspx?Ecode=" + sEcode + "&Employeename=" + sEmployeename + "&department" + sdepartment + "&location" + slocation);
}

What can I do to remove the error?

Thanks
Posted
Updated 16-May-10 22:15pm
v2

avijit1112010 wrote:
"empfetch.aspx?Ecode=" + sEcode + "&


You are trying to use a variable named 'sEcode'. I don't find that defined in your button click event method. Is that a Global variable?
Error suggests it's not a defined variable (not global!).

In order to resolve, before using the Response.Redirect, declare and define the values of all the query strings you are trying to use.
 
Share this answer
 
Have you declared sEcode?

If it has not been declared the IDE cannot recognise it.

Either declare it as Public or within the sub as private.
That way it will be 'seen' and made usable.
 
Share this answer
 
Where did you defined sEcode ?
please recheck the variable name.
 
Share this answer
 
As others have so eloquently pointed out, you have not declared the variable so that it is visible. I'd like to take this opportunity to address another couple of potential problems with your code though.

First of all, you are relying on the querystring to pass in information; this means that you need to be aware that this code is open to a query string injection attack on an unpatched internet server.

Secondly, you are missing the equals sign between &department and the variable, and &location and the variable.

Third, you need to ensure that you have UrlEncoded the parameters so that they are actually valid.

So, how to get round the querystring issues. Well, here's a quick sample I've knocked together in the answer editor here (it may need some tweaking, but you should get the idea).
C++
public static class Utility
{
  public static string CreateSecuredParameters(Dictionary<string, string> parameters)
  {
    StringBuilder queryString = new StringBuilder();
    string keyValue = string.Empty;

    foreach (KeyValuePair<string, string> parameter in parameters)
    {
      queryString.AppendFormat("{0}{1}={2}", keyValue, parameter.Key, HttpUtility.UrlEncode(parameter.Value));
      keyValue = "&";
    }

    // Now we need to create the hash.
    queryString.AppendFormat("{0}d={1}", keyValue, ComputeHash(queryString.ToString());

  }

  private static string ComputeHash(string queryString)
  {
    string salt = "C0d39r0j3ct1sF4bul0us";
    queryString = string.Format("{0}{1}{0}", salt, queryString);
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    byte[] hashed = md5.ComputeHash(new UTF8Encoding.GetBytes(input));

    return Convert.ToBase64String(hash).TrimEnd('=');
  }
}
This code isn't perfect because it doesn't prevent replay attacks; you can add an expiration time to the salt to accomplish this, but that would affect how spiders index your site. It's up to you how far you want to take this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900