3
Answers

Retrieving a single value from database

Daniel Koh

Daniel Koh

7y
257
1
Hi, how do you retrieve a single value stored in a datatable via user input to choose column? for example my datatable has 2 columns, one for username and password. I would like to be able to enter my username in a program to retrieve the password from the database that is in the same row as the username. How do I do that?  This part of my program is similar to that of those "forget password" things where you have to answer a security question and enter your username to retrieve your lost password. Much help appreciated thanks.
 
This is the code for checking if the security questions have been answered correctly: 
 
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Daniel Koh\Documents\AccountData.mdf;Integrated Security=True;Connect Timeout=30");
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From [Table] where Username='" + textBox1.Text + "' and EmployeeId ='" + textBox2.Text + "' and SecurityQuestionAnswer='" + textBox3.Text + "' and SecurityQuestionType='" + comboBox1.SelectedItem.ToString() + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if ((dt.Rows[0][0].ToString() == "1"))
{
[MISSING CODE HERE]
MessageBox.Show(password);
}
else
{
MessageBox.Show("Please check your Username, Security Question Answer and EmployeeID");
}
}
 
Answers (3)