there a problem in my project when i try insert some info it gives me error like image in the link
http://s21.postimg.org/gxkd7roh3/Capture.png
error said that connection is not closed ?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 2, 2013, 4:39 AM EDT
1st one:
private void bindcombo_college_name()
{
OleDbCommand com = new OleDbCommand();
OleDbDataReader reader;
com.Connection = db_con;
if (db_con.State == ConnectionState.Closed)
db_con.Open();
college_name.Items.Clear();
com.CommandText = "select * from colleges";
reader = com.ExecuteReader();
while(reader.Read())
{
string combo_items = reader["college_name"].ToString();
college_name.Items.Add(combo_items);//college_name is a combobox
}
db_con.Close();
2nd one:
OleDbCommand com = new OleDbCommand();
OleDbDataReader reader;
com.Connection = db_con;
dept_name.Items.Clear();
if (db_con.State == ConnectionState.Closed)
db_con.Open();
com.CommandText = "select * from [depts] inner join [colleges] on [colleges].college_code = [depts].college_code where [colleges].[college_name] = '" + college_name.Text + "'";
reader = com.ExecuteReader();
while (reader.Read())
{
string combo_items = reader["dept_name"].ToString();
dept_name.Items.Add(combo_items);//college_name is a combobox
}
db_con.Close();
Sunny SharmaRank #223 · 11.1K reputationPosted Aug 2, 2013, 4:23 AM EDT
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted Aug 2, 2013, 4:29 AM EDT
using System.Data.SqlClient;
using System.Data;
write your connection in this if block
if (db_con.State == ConnectionState.Closed)
{
db_con.Open();
}
honar abubakr62 reputationPosted Aug 2, 2013, 4:33 AM EDT
private void bindcombo_college_name()
{
OleDbCommand com = new OleDbCommand();
OleDbDataReader reader;
com.Connection = db_con;
db_con.Open();
college_name.Items.Clear();
com.CommandText = "select * from colleges";
reader = com.ExecuteReader();
while(reader.Read())
{
string combo_items = reader["college_name"].ToString();
college_name.Items.Add(combo_items);//college_name is a combobox
}
db_con.Close();
and i want in the second combobox to display only the depts of the college that i select in the first combobox i use the first combobox textchange event to do this with this code
OleDbCommand com = new OleDbCommand();
OleDbDataReader reader;
com.Connection = db_con;
dept_name.Items.Clear();
db_con.Open();
com.CommandText = "select * from [depts] inner join [colleges] on [colleges].college_code = [depts].college_code where [colleges].[college_name] = '" + college_name.Text + "'";
reader = com.ExecuteReader();
while (reader.Read())
{
string combo_items = reader["dept_name"].ToString();
dept_name.Items.Add(combo_items);//college_name is a combobox
}
db_con.Close();
it do the action that i want but when i click the insert button it inserts data and gives connection not closed error
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 2, 2013, 4:34 AM EDT
Try like this
Regards,
Iftikar
honar abubakr62 reputationPosted Aug 2, 2013, 5:16 AM EDT
Prasenjit DeyRank #6,279 · 271 reputationPosted Aug 3, 2013, 2:43 AM EDT
if (db_con.State == ConnectionState.Closed || db_con.State == ConnectionState.broken)
db_con.Open();
if (db_con.State == ConnectionState.Open)
db_con.Close();