title: ASP.NET MySQL Undefined Parameter
body: <p>Good day!</p>
  
  <p>I have a code here that inserts, but never updates. There error I get is <code>Parameter '@id_number' must be defined</code>.</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>string query = "INSERT INTO pupil (`fname`, `lname`, `mi`) VALUES (@fn, @ln, @mi)";
  MySqlCommand com = new MySqlCommand(query, connection);
  
  com.Parameters.AddWithValue("@fn", txtAddFName.Text);
  com.Parameters.AddWithValue("@ln", txtAddLName.Text);
  com.Parameters.AddWithValue("@mi", txtAddMI.Text);
  com.ExecuteNonQuery();
  
  //create ID number
  long lastID = com.LastInsertedId;
  int length = 4;
  string currentYear = DateTime.Now.Year.ToString("yy");
  string result = lastID.ToString().PadLeft(length, '0');
  string id_number = currentYear + "-" + result ;
  string updateIDNumber = "UPDATE pupil SET id_number=@id_number WHERE pupil_id=@pupil_id";
  MySqlCommand IDcom = new MySqlCommand(updateIDNumber, connection);
  
  com.Parameters.AddWithValue("@id_number", id_number);
  com.Parameters.AddWithValue("@pupil_id", lastID);
  IDcom.ExecuteNonQuery();
  </code></pre>
  
  <p>The Insert code works, but the Update isn't. What I wanted to do is to get the last inserted ID and update it's field <code>id_number</code> with the last 2 digits of the current year + the last inserted ID with 4 leading 0's. For example if the last Inserted ID is 1, the value of <code>lastID</code> variable will be <strong>16-0001</strong>.</p>
  
  <p>The schema by the way is this:</p>
  
  <pre><code>pupil_id INT(11) AUTO_INCREMENT
  fname VARCHAR(50)
  lname VARCHAR(50)
  mi VARCHAR(50)
  id_number VARCHAR(255)
  </code></pre>
  
  <p>Any help would be much appreciated.</p>