title: Trying to execute a query multiple times using while loop
body: <p>I'm trying to execute a query for MySql using while loop but it gives me error and I don't have any idea how to fix it. Without the loop, the code works well.</p>
  
  <p>Additional information: Object reference not set to an instance of an object.</p>
  
  <p>Here is what i got:</p>
  
  <pre><code>String copies = txtcopies.Text;
  int x = Convert.ToInt32(copies);
  int n = 0;
  
  while (n &lt; x)
  {
      string loadstring = @"server=localhost;database=librarys;userid=root;password=1234;";
  
      MySqlConnection conDataBase = new MySqlConnection(loadstring);
  
      MySqlCommand cmdDataBase = new MySqlCommand("SELECT func_add_book('" + this.lblbnum.Text + "','" + this.txtisbn.Text + "','" + this.txttitle.Text + "','" + this.txtauthor.Text + "','" + this.txtpublisher.Text + "','" + this.txtdate.Text + "','" + this.txtcost.Text + "');", conDataBase);
      string returnedValue = cmdDataBase.ExecuteScalar().ToString();
  
      n++;
  
      conDataBase.Open();
      ClearAllText(txtcopies);
      MessageBox.Show(returnedValue);
  }
  </code></pre>