title: How to use SqlTransaction in C#
body: <p>I am using the following code to execute two commands at once. I used SqlTransaction to assure either all command get executed or rolled back. When I run my program without &quot;transaction&quot;, it runs properly; but when I use &quot;transaction&quot; with them, they show error.</p>
  <p>My code:</p>
  <pre class="lang-cs prettyprint-override"><code>SqlTransaction transaction = connectionsql.BeginTransaction();
              
  try
  {
      SqlCommand cmd1 = new SqlCommand(&quot;select account_name from master_account where NOT account_name = 'BANK' AND NOT account_name = 'LOAN'&quot;, connectionsql);
      SqlDataReader dr1 = cmd1.ExecuteReader();
      while (dr1.Read())
      {
          comboBox1.Items.Add(dr1[0].ToString().Trim());
      }
      cmd1.Dispose();
      dr1.Dispose();
  
      SqlCommand cmd2 = new SqlCommand(&quot;select items from rate&quot;,connectionsql);
      SqlDataReader dr2 = cmd2.ExecuteReader();
      while (dr2.Read())
      {
          comboBox2.Items.Add(dr2[0].ToString().Trim());
      }
      cmd2.Dispose();
      dr2.Dispose();
      transaction.Commit();
  
      dateTimePicker4.Value = dateTimePicker3.Value;
  }
  catch(Exception ex)
  {
      transaction.Rollback();
      MessageBox.Show(ex.ToString());
  }
  </code></pre>
  <p>Error:</p>
  <p><img src="https://i.stack.imgur.com/LBIvy.jpg" alt="enter image description here" /></p>