Hi, I have a simple question
On my page I have two text boxes, a button and a label.
I have a database DB_rental which contains a table TB_credentials (empID, username, password)
User enters the username and password in the textboxes and clicks the button. The application should check the database if the username and password match. and if they do, the 'empID' should be displayed in the label.
Following is the code I have written and its not working. Please point out where I am wrong and if possible provide me with the code so that i can test myself too! Thanks :)
protected void Button1_Click(object sender, EventArgs e)
{
AuthenticateUser(TextBox1.Text, TextBox2.Text);
}
public string AuthenticateUser(string Username, string Password)
{
Username = TextBox1.Text;
Password = TextBox2.Text;
using (SqlConnection cn = new SqlConnection("Data Source = .\\SQLEXPRESS; Initial Catalog=DB_Rental; Integrated Security=True;"))
{
cn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = cn;
cmd.CommandText = "select empID from TB_credentials WHERE @Username = userNameTXT and @Password=passwordTXT";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@userNameTXT", Username);
cmd.Parameters.AddWithValue("@passwordTXT", Password);
SqlDataReader reader;
try
{
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
string ID = Convert.ToString(reader);
return ID ;
}
}
catch
{
cn.Close();
throw;
}
}
}
}