One obvious reason why your code isn't working because you haven't assigned the parameter
@UserID
in your SQL query. To fix that, you can do something like this:
private void GetQuestionOne(int userID){
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using(SqlConnection con = new SqlConnection(strConnString)){
using(SqlCommand cmd = new SqlCommand("SELECT question1, question2 from Customer where username=@UserID", con)){
con.Open();
cmd.Parameters.AddWithValue("@UserID", userID);
using (SqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
lblqnsOne.Text = read["question1"].ToString();
lblqnsTwo.Text = read["question2"].ToString();
}
}
}
}
}
Tip: Make it a habit to put
objects
that eat resources such as
SqlConnection
,
SqlCommand
,
SqlDataAdapter
,
SqlDataReader
within a
using statement
to ensure that objects will be properly disposed and closed after they are used.