If I create a dummy Table like yours, and copy'n'paste your code into one of my apps:
private void button1_Click(object sender, EventArgs e)
{
string constring = SMDBSupport.SMInstanceStorage.GetInstanceConnectionString("Testing");
using (SqlConnection connect = new SqlConnection(constring))
using (SqlCommand comm = new SqlCommand())
{
connect.Open();
comm.CommandText = "SELECT * FROM student WHERE studentno = @ID";
comm.Connection = connect;
comm.Parameters.AddWithValue("@ID", textBox1.Text);
SqlDataReader read = comm.ExecuteReader(CommandBehavior.CloseConnection);
if (read.Read() == false)
{
MessageBox.Show("The student ID you've entered doesn't have a library card yet.", "No Record");
}
else
{
checkIssue();
}
}
}
void checkIssue() { }
It compiles clean, it runs, and I get a message box if the ID value is not present, and no message if it is.
So ... I suspect that either you aren't running compiled code (because an error elsewhere meant no exe was produced) or your code isn't executing that method.
Break out the debugger and start checking - we can't help you if the problem is not in the code you show us!
BTW: the code can be simplified, and cleaned up quite a little:
private void button1_Click(object sender, EventArgs e)
{
string constring = SMDBSupport.SMInstanceStorage.GetInstanceConnectionString("Testing");
using (SqlConnection connect = new SqlConnection(constring))
{
connect.Open();
using (SqlCommand comm = new SqlCommand("SELECT * FROM student WHERE studentno = @ID", connect))
{
comm.Parameters.AddWithValue("@ID", textBox1.Text);
using (SqlDataReader read = comm.ExecuteReader())
{
if (read.Read())
{
checkIssue();
}
else
{
MessageBox.Show("The student ID you've entered doesn't have a library card yet.", "No Record");
}
}
}
}
}
Your
try...catch
block is useless if all you do is rethrow the exception: it does nothing at all that omitting it doesn't! If you aren't going to handle a problem (or at least log it) then don't try to catch it at all as it implies the problem is dealt with.