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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjeeb LenkaPosted Aug 2, 2013, 8:39 AM
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();
Prasenjit DeyPosted Aug 3, 2013, 6:43 AM
if (db_con.State == ConnectionState.Closed || db_con.State == ConnectionState.broken)
db_con.Open();
if (db_con.State == ConnectionState.Open)
db_con.Close();
honar abubakrPosted Aug 2, 2013, 9:16 AM
Iftikar HussainPosted Aug 2, 2013, 8:34 AM
Try like this
Regards,
Iftikar
honar abubakrPosted Aug 2, 2013, 8:33 AM
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
Sandeep Singh ShekhawatPosted Aug 2, 2013, 8:29 AM
using System.Data.SqlClient;
using System.Data;
write your connection in this if block
if (db_con.State == ConnectionState.Closed)
{
db_con.Open();
}
Sunny SharmaPosted Aug 2, 2013, 8:23 AM