Orion Orion - 3 months ago 30
ASP.NET (C#) Question

Must declare the scalar variable "@loginName"

I am trying to login using LoginID and Password as user name and password respectively. I am stuck here. Please help me.

protected void SignInButton_Click(object sender, EventArgs e)
{
int customerId = 0;
int loginName = Convert.ToInt32(LoginTextBox.Text);
string password = PasswordTextBox.Text;
SqlCommand command = new SqlCommand();
SqlDataReader reader;
command.CommandText = "Select CustomerID from Customers where LoginID= @loginName and Password= @password";
command.Parameters.AddWithValue("@LoginID", loginName);
command.Parameters.AddWithValue("@Password", password);
command.Parameters.AddWithValue("@CustomerID", customerId);
reader = DbUtility.GetDataReader(command);

if (reader.HasRows)
{
reader.Read();
customerId = reader.GetInt32(0);
Session["CustomerID"] = customerId;
Response.Redirect("ListCategories.aspx");
}
else
{
MessegeLabel.Text = "User Name or Password Incorrect";
}
}

Answer

You have some SQL there which includes the parameter @loginName. You never provide that parameter; you provide an unused parameter called @LoginID instead (along with an unused @CustomerID also, for some reason).

It's telling you that it needs you to provide all the parameters your SQL snippet uses.

command.CommandText = "Select CustomerID from Customers where LoginID= @loginName and Password= @password";
command.Parameters.AddWithValue("@LoginID", loginName);
command.Parameters.AddWithValue("@Password", password);
command.Parameters.AddWithValue("@CustomerID", customerId);

So provide it:

command.Parameters.AddWithValue("@loginName", loginName);