Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtFlavorName.Equals("") && cboSupplier.Text.Equals(""))
            {
                MessageBox.Show("Please make sure that you have entered a flavor name and selected the corresponding supplier.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            else
            {
                try
                {
                    bool flavorExists = false;
                    int supplierid = 0;
                    //Get supplier Id

                    string query = "SELECT supplier_id FROM tbl_suppliers WHERE supplier_co_name = @supplier_name";
                    using (SqlConnection con = new SqlConnection(connstring))
                    {
                        con.Open();
                        using (SqlTransaction trans = con.BeginTransaction())
                        {
                            using (SqlCommand cmd = new SqlCommand(query, con))
                            {
                                cmd.Transaction = trans;
                                SqlDataReader reader;
                                cmd.Parameters.AddWithValue("@supplier_name", cboSupplier.Text.ToString());
                                reader = cmd.ExecuteReader();
                                if (reader.Read())
                                {
                                    supplierid = Convert.ToInt32(reader["supplier_id"].ToString());
                                }
                                reader.Close();
                            }
                        }
                    }

                    //Verify if flavor already exists
                    query = "SELECT * FROM TBL_FLAVORS WHERE flavor_name = @flavor_name AND flavor_supplierid = @supplier_id";
                    using (SqlConnection con = new SqlConnection(connstring))
                    {
                        con.Open();
                        using (SqlTransaction trans = con.BeginTransaction())
                        {
                            using (SqlCommand cmd = new SqlCommand(query, con))
                            {
                                cmd.Transaction = trans;
                                SqlDataReader read;
                                cmd.Parameters.AddWithValue("@flavor_name", txtFlavorName.Text.ToString());
                                cmd.Parameters.AddWithValue("@supplier_id", supplierid);
                                read = cmd.ExecuteReader();
                                if (read.Read())
                                {
                                    MessageBox.Show("This flavor for this supplier already exists.");
                                    return;
                                }
                                read.Close();
                            }
                            trans.Commit();
                        }
                    }
                    //
                    query = "INSERT INTO tbl_flavors (flavor_name, flavor_supplierid) VALUES (@flavor_name, @supplier_id)";
                    using (SqlConnection con = new SqlConnection(connstring))
                    {
con.Open();
                        using (SqlTransaction trans = con.BeginTransaction())
                        {
                            using (SqlCommand cmd = new SqlCommand(query, con))
                            {
                                cmd.Transaction = trans;
                                cmd.Parameters.AddWithValue("@flavor_name", txtFlavorName.Text.ToString());
                                cmd.Parameters.AddWithValue("@supplier_id", supplierid);
                                cmd.ExecuteNonQuery();
                            }
                            trans.Commit();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }



I don't know why I'm getting this exception when I'm closing the reader?

What I have tried:

I've looked into stackoverflow articles and they say it could be because of two concurrent commands but I have separated them?
Posted
Updated 2-Oct-16 18:34pm
v3
Comments
Foothill 3-Oct-16 16:55pm    
SqlDataReader implements IDisposable. Do you get the same error if you wrap the readers in a 'using' block?
kmllev 4-Oct-16 0:51am    
Somehow, removing the transaction block fixed it. I wonder why?
Foothill 4-Oct-16 9:50am    
That has to do with connection pooling. I have run into problems myself with performing certain SQL operations. Call SqlConnection.ClearAllPools() before the transaction and see if that fixes the error.

1 solution

May be my sugestion is not the perfect but I think it will help resolve your issue in this scenario.
When you expect a single value to be returned from the database, the best method to use is ExecuteScalar(). I believe, for a single value to be retrived fro the databasem it internally works same as datareader.

Replace all those relevant code blocks with ExecuteScalar() and should be handle the concurrent reads as the connection gets closed automatically. Just leave the INSERT part as it is.

Reference:
Quote:
Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database. This requires less code than using the ExecuteReader method, and then performing the operations that you need to generate the single value using the data returned by a SqlDataReader.

SqlCommand.ExecuteScalar Method (System.Data.SqlClient)[^]

If you still find problem, please let me know the issue. :)
 
Share this answer
 
Comments
kmllev 4-Oct-16 0:51am    
Somehow, removing the transaction block fixed it. I wonder why?

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