Getting Primary key,from Username and Password/Getting Row number from Username and Password.
I'm doing a desktop application that needs to get the Primary key/Row in the database that matches the username and password in that row.
I need to obtain the USERID from the Username and Password they use to login.
Can anyone help?

AaronPosted Aug 19, 2009, 3:06 PM
Kirtan PatelPosted Aug 19, 2009, 2:32 PM
You can Get PrimaryKEy From Following Code if you have Primary Key Column name = UserId Change it According to your need in code.
You Will get UserId in int UserId if found .
dont Forget to mark "Do you like Answer" it will give me some credits :)
string Username = textBox1.Text.Trim();
string password = textBox2.Text.Trim();
SqlConnection con = new SqlConnection("@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"");
con.Open();
SqlCommand comm = new SqlCommand("select UserID from UsersTable where username=@username and password=@password", con);
comm.Parameters.AddWithValue("@username", Username);
comm.Parameters.AddWithValue("@username", password);
int UserId = Convert.ToInt32(comm.ExecuteScalar());
con.Close();
MessageBox.Show("User id is = " + UserId);