How can I save the UserID from the same datatable row whenever user inputs correct UserName and UserPassword? I want to use the saved GetUserID to another form.
Datatable 1 (Users):
- UserID (IDENTITY 1 , 1)
- UserName
- UserPassword
My code:
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text != string.Empty || textBox1.Text != string.Empty)
{
cmd = new SqlCommand("select * from Users where UserName='" + textBox1.Text + "' and UserPassword='" + textBox2.Text + "'; SELECT SCOPE_IDENTITY()", cn);
dr = cmd.ExecuteReader();
if (dr.Read())
{
dr.Close();
getUserID = Convert.ToInt32(cmd.ExecuteScalar());
form2 win_pagr = new form2();
this.Hide();
win_pagr.ShowDialog();
}
else
{
dr.Close();
MessageBox.Show("There is no user by this name or the password is incorrect!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please fill the blanks!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I Have tried using SCOPE_IDENTITY as you can see, It worked for something other I needed, but I doubt I need it here.