Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
1.67/5 (5 votes)
See more:
hi every one.i am working with login functionality.i have code below

C#
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
            conn.Open();

            string id = TextBox1.Text;
            string password = Textbox2.Text;
            SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname = @id and pswd = @password ", conn);

            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                conn.Close();
                Response.Redirect("Default.aspx");

            }
}

i am getting the error after button click....Must declare the scalar variable "@id".

any help would be greatly appriciated.....
Posted
Updated 17-Jan-13 23:45pm
v3
Comments
rizwan muhammed khan gouri 24-Jan-13 0:21am    
who give me down vote .First check it
Thanks7872 24-Jan-13 0:22am    
i am not getting you.what you mean?

C#
SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname = @id and pswd = @password ", conn);


after your above code, write these lines

C#
cmd.Parameters.Add(new SqlParameter("@id", "id here"));
cmd.Parameters.Add(new SqlParameter("@password", "password here"));
 
Share this answer
 
v2
To add to what Faisalabadians says (and he is right as far as it goes), do not do it that way anyway!
Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
 
Share this answer
 
You are using the scalar variable @id and @password. You should also mention what type of parameter is this! Add the following line of code.
C#
SqlCommand cmd = new SqlCommand("Select uname, pswd from emp_details where uname = @id and pswd = @password ", conn);
cmd.Parameters.Add(new SqlParameter("@id", System.Data.SqlDbType.VarChar)).Value = TextBox1.Text;
cmd.Parameters.Add(new SqlParameter("@password", System.Data.SqlDbType.VarChar)).Value = TextBox2.Text;

Here,
cmd.Parameters.Add(new SqlParameter("@id", System.Data.SqlDbType.VarChar)).Value = TextBox1.Text;

is telling the compiler that the parameter @id is of type SQL VarChar, and then assigning the value TextBox1.Text to the parameter @id.
Now, since your parameters are declared, when the compiler reaches at this line
SqlDataReader dr = cmd.ExecuteReader();

It knows the data type of your parameter and you will not get the error you mentioned in your question.

Hope that helps.
Cheers,
Naman
 
Share this answer
 
Comments
Thanks7872 18-Jan-13 6:03am    
Great reply from your side..rarely found on the forums..deeply explained each and everything....i got the point.thanks....keep it up....
Naman Kumar Sinha 18-Jan-13 6:22am    
Just a note on top of this. You should consider using Forms authentication with Membership class. Storing clear text as password is not a recommended practice. .Net comes with a SecureString data type and all this functionalities are in - built in Membership class.

Happy coding :)

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



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