private void Form2_Load(object sender, EventArgs e)
{
LoginPage LP = new LoginPage();
int ID = LP.ID;
// Setting buttons as user's subjects.
#region
mainPageConnection.Open();
SqlCommand subject1Command = new SqlCommand("SELECT Subject_1 FROM SubjectsTable WHERE ID = '" + ID + "'", mainPageConnection);
string subject1 = (string)subject1Command.ExecuteScalar();
btnSubject1.Text = subject1;
Working on a winforms project. For now I have 2 forms, LoginPage (the first one) and FormMainPage. The code above is on FormMainPage. My problem is that I have the table shown in the image but I can’t manage to get the result of the select query into a variable, I’ve tried various different ways but it didn’t work. Can anyone show me what I did wrong please?
for the code above, the text for btnSubject1
ends up being blank!!
I’ve tried many different methods related to SqlCommand class. Such as SqlNonQuery, SqlDataReader and then doing
if(reader.Read())
{
variable = reader["Subject1"]
}
I’ve spent quite a while on this. The big codeblock there is is the only version of what I’ve tried that doesn’t produce an error, except that the subject buttons end up being blank.
Thanks in advance!
Deadly Pyke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.