I'm working on a school project to create a registration system. My chosen means of a DB was to use T-SQL as it's something I'm already familiar.
I'm using the below code to query a DB
public void button3_Click(object sender, EventArgs e)
{
string StudentID = (textBox1.Text);
string queryDOB = "SELECT DOB,LastName,Degree FROM Student Where StudentID = " + StudentID;
SqlConnection con = new SqlConnection(Properties.Settings.Default.SRSConnectionString);
con.Open();
SqlCommand DOB = new SqlCommand(queryDOB, con);
SqlDataReader stidreader = DOB.ExecuteReader();
if (stidreader.Read())
{
textBox2.Text = stidreader["DOB"].ToString();
textBox3.Text = stidreader["LastName"].ToString();
textBox5.Text = stidreader["Degree"].ToString();
}
else
{
MessageBox.Show("Your ID is not recognised. Please try again.");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox5.Clear();
}
con.Close();
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
string Degree = textBox5.Text;
string degsql = "SELECT ModName FROM Module Where Degree = " + Degree;
SqlConnection con = new SqlConnection(Properties.Settings.Default.SRSConnectionString);
DataSet dsm = new DataSet();
SqlDataAdapter connect = new SqlDataAdapter(degsql, con);
con.Open();
connect.Fill(dsm);
this.listBox2.DataSource = dsm.Tables[0];
this.listBox2.DisplayMember = "ModName";
}
The first section for the button click works perfectly but when I run the query in the next event (textbox change) the query returns an error message that an invalid column is found. Well, the column found is the variable that it should be using for the Query, not the column itself which is very confusing.
I know there can be issues with something called MARS but the DB seems to be the right version.
Can anyone shed any light?
Appreciate it,
Jamie