Click here to Skip to main content
15,905,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

im working on a Winform C# program and when i populate Combobox from mysql table, but i added a blank line to the combo throu code or on the table in mysql. and when i click save bottom it tells me that the connection to the database has not been closed i have check and close it on many diferent way and it just wont work, but if i take the blank line it would work with no problem (i need the blank line).

if any body can help me please.

Sorry, here is the code for populating the combobox

C#
private void cargacombocs(string bsc)
        {
            try
            {                

                MySqlCommand cmd = Form2.conecxionsyscon.CreateCommand();

                MySqlDataReader Reader;
                
                cmd.CommandText = "SELECT subcatp FROM budcatpaf WHERE indpp = '" + bsc + "'";

                Form2.conecxionsyscon.Open();
                Reader = cmd.ExecuteReader();
                while (Reader.Read())
                {
                    string thisrow = "";
                    for (int i = 0; i < Reader.FieldCount; i++)
                        thisrow += Reader.GetValue(i).ToString();
                    comboBox2.Items.Add(thisrow);
                    comboBox3.Items.Add(thisrow);
                    comboBox8.Items.Add(thisrow);
                }
                
            }
            catch (MySqlException ex2)
            {
                MessageBox.Show("Code: " + ex2.Message);
            }
            Form2.conecxionsyscon.Close();
        }


this is save to a table in mysql which after saving opens up a crystal report with some info
but before it opens the crystal i get the error message that the coneccion is still open

This is where i create the conection

C#
MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();

                connBuilder.Add("DataBase", srv[1]);
                connBuilder.Add("Data Source", srv[0]);
                connBuilder.Add("User Id", srv[2]);
                connBuilder.Add("Password", srv[3]);
                                
                conecxionsyscon = new MySqlConnection(connBuilder.ConnectionString);
                
                MySqlCommand cmd = conecxionsyscon.CreateCommand();
Posted
Updated 19-Jun-14 12:49pm
v3
Comments
[no name] 19-Jun-14 16:03pm    
Sure. Should we all just drop by your place to look at your code?
azteca_04 19-Jun-14 17:09pm    
Sorry, you right i forgot to put the code
[no name] 19-Jun-14 21:08pm    
Try adding another catch block. I believe that you are generating another exception that you are not catching.
Jafarinejadvazifehkhorani 19-Jun-14 17:20pm    
provide Form2.conecxionsyscon.Close(); and Form2.conecxionsyscon.CreateCommand(); code as well in order to be able to help you .
j snooze 19-Jun-14 17:40pm    
Don't forget to close the datareader too. Reader.close();

1 solution

Hello
I have checked your code and i found that in catch block also you need to close sql connection and while opening new connection you need to check whether existing connection is open.
 
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