Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void buttonIssue_Click(object sender, EventArgs e)
      {
          try
          {
              AdminPanel n = new AdminPanel();
              if (textBox1.Text == string.Empty)
              {
                  MessageBox.Show("Please enter Student ID");
              }
              else
              {
                  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();
                      }
                  }
              }
          }
          catch (Exception)
          {
              //MessageBox.Show(ex.ToString());
              throw;
          }


What I have tried:

I tried and verified if the code is valid and i also build and rebuild the project still the same sqlexception must declare the scalar variable @ID
Posted
Updated 29-Nov-21 23:41pm
Comments
PIEBALDconsult 30-Nov-21 9:19am    
Ensure that the parameter value isn't NULL.

If I create a dummy Table like yours, and copy'n'paste your code into one of my apps:
C#
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:
C#
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.
 
Share this answer
 
The problem may be that textBox1.Text is null, which is not the same as DBNull (which is the null value that is okay for database work).

Try it this way:

C#
comm.Parameters.AddWithValue("@ID", (string.IsNullOrEmpty(textBox1.Text))?DBNull.Value:textBox1.Text;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900