locked
ExecuteReader: Connection Property has not been initialized RRS feed

  • Question

  • I have ExecuteReader error. I have tried to connect database and get some values from my database. I have searched but can not figure it out. Here is my code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.OleDb;
    using System.Data;
    using System.Windows;
    using System.Drawing;
    using System.Collections;
    using Microsoft.VisualBasic;
    using System.IO;
    using System.Windows.Forms;
    
    namespace PhoneCaseWFA
    {
        class Database
        {
            private OleDbConnection bag = new OleDbConnection();
            private OleDbCommand komut = new OleDbCommand();
            private OleDbDataReader reader = null;
            public DataTable datatable = new DataTable();
            string Ad, Soyad, EPosta, DogumTar;
        int TelefonNum, PinKodu, Kimlik, KasaNum;
    
        public void TabloBaglantisi()
        {
            string baglanti = "Provider = Microsoft.ACE.OLEDB.12.0; Source = SarjVeritabani.mdb; Persist Security Info = False;";
            bag.Close();
            bag.ConnectionString = baglanti;
    
            try
            {
                bag.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
    
            OleDbDataAdapter adapter = new OleDbDataAdapter("Select * From SarjVerileri", baglanti);
            adapter.Fill(datatable);
        }
    
        public Database()
        {
            string baglanti = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = SarjVeritabani.mdb; Persist Security Info = False;";
            bag.ConnectionString = baglanti;
    
            try
            {
                bag.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    
        public int KasaNoBul()
        {
            string Kasalar = "SELECT KasaNumarasi FROM KasaKontrol";
    
            int kasa1 = 10000000, kasa2 = 01000000, kasa3 = 00100000, kasa4 = 00010000,
                kasa5 = 00001000, kasa6 = 00000100, kasa7 = 00000010, i = 0, j = 0, 
                boskasa = 00000000;
    
            int[] KasaNo = new int[7];
    
            if (bag.State == ConnectionState.Closed)
                bag.Open(); //It gives me error here
    
            komut.Connection = bag;
            komut.CommandText = Kasalar;
            reader = komut.ExecuteReader();
    
            while (reader.Read())
            {
                KasaNo[i] = Convert.ToInt32(reader);
                i++;
            }
    
            reader.Close();
    
            while(j<7)
            {
                if (kasa1 != KasaNo[j])
                {
                    boskasa = kasa1;
                    break;
                }
    
                else if (kasa2 != KasaNo[j])
                {
                    boskasa = kasa2;
                    break;
                }
    
                else if (kasa3 != KasaNo[j])
                {
                    boskasa = kasa3;
                    break;
                }
    
                else if (kasa4 != KasaNo[j])
                {
                    boskasa = kasa4;
                    break;
                }
    
                else if (kasa5 != KasaNo[j])
                {
                    boskasa = kasa5;
                    break;
                }
    
                else if (kasa6 != KasaNo[j])
                {
                    boskasa = kasa6;
                    break;
                }
    
                else if (kasa7 != KasaNo[j])
                {
                    boskasa = kasa7;
                    break;
                }
    
                j++;
            }
    
            return boskasa;
        }

    This is some of my code. I donnot know how to figure it out. If anybody can help me I'll be greatfull.

    Saturday, April 16, 2016 9:13 AM

Answers

  • An OleDbConnection is supposed to be short-lived. You open the connection, connect to the database and reads the data using the reader and then you close the connection:

            public int KasaNoBul()
            {
                string baglanti = "Provider = Microsoft.ACE.OLEDB.12.0; Source = SarjVeritabani.mdb; Persist Security Info = False;";
                string Kasalar = "SELECT KasaNumarasi FROM KasaKontrol";
    
                int kasa1 = 10000000, kasa2 = 01000000, kasa3 = 00100000, kasa4 = 00010000,
                    kasa5 = 00001000, kasa6 = 00000100, kasa7 = 00000010, i = 0, j = 0,
                    boskasa = 00000000;
    
                int[] KasaNo = new int[7];
    
                using (OleDbConnection connection = new OleDbConnection(baglanti))
                {
                    using (OleDbCommand command = new OleDbCommand(Kasalar, connection))
                    {
                        connection.Open();
                        using (OleDbDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                KasaNo[i] = Convert.ToInt32(reader);
                                i++;
                            }
                        }
                    }
                }
    
                while (j < 7)
                {
                    if (kasa1 != KasaNo[j])
                    {
                        boskasa = kasa1;
                        break;
                    }
    
                    else if (kasa2 != KasaNo[j])
                    {
                        boskasa = kasa2;
                        break;
                    }
    
                    else if (kasa3 != KasaNo[j])
                    {
                        boskasa = kasa3;
                        break;
                    }
    
                    else if (kasa4 != KasaNo[j])
                    {
                        boskasa = kasa4;
                        break;
                    }
    
                    else if (kasa5 != KasaNo[j])
                    {
                        boskasa = kasa5;
                        break;
                    }
    
                    else if (kasa6 != KasaNo[j])
                    {
                        boskasa = kasa6;
                        break;
                    }
    
                    else if (kasa7 != KasaNo[j])
                    {
                        boskasa = kasa7;
                        break;
                    }
    
                    j++;
                }
    
                return boskasa;
            }
    

    Don't open the connection in one method and use it on another one and don't use long-lived connection objects.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Ali Dayan Sunday, April 17, 2016 3:08 PM
    Saturday, April 16, 2016 9:49 AM

All replies

  • I recommend you use a "Using" statement rather than code like you have there, so the connection is always going to be disposed.

    An example piece of such code ( oibviously this does something entirely different ):

                string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Biz\AccessPoints.accdb;
    Persist Security Info = False;";
                List<string> tables = new List<string>();
                using (OleDbConnection con = new OleDbConnection(connString))
                {
                    string[] restrictions = new string[4];
                    restrictions[3] = "Table";
                    con.Open();
                    DataTable schema = con.GetSchema("Tables", restrictions);
                    foreach (DataRow row in schema.Rows)
                    {
                        tables.Add(row.Field<string>("TABLE_NAME"));
                    }
                }


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Saturday, April 16, 2016 9:25 AM
  • An OleDbConnection is supposed to be short-lived. You open the connection, connect to the database and reads the data using the reader and then you close the connection:

            public int KasaNoBul()
            {
                string baglanti = "Provider = Microsoft.ACE.OLEDB.12.0; Source = SarjVeritabani.mdb; Persist Security Info = False;";
                string Kasalar = "SELECT KasaNumarasi FROM KasaKontrol";
    
                int kasa1 = 10000000, kasa2 = 01000000, kasa3 = 00100000, kasa4 = 00010000,
                    kasa5 = 00001000, kasa6 = 00000100, kasa7 = 00000010, i = 0, j = 0,
                    boskasa = 00000000;
    
                int[] KasaNo = new int[7];
    
                using (OleDbConnection connection = new OleDbConnection(baglanti))
                {
                    using (OleDbCommand command = new OleDbCommand(Kasalar, connection))
                    {
                        connection.Open();
                        using (OleDbDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                KasaNo[i] = Convert.ToInt32(reader);
                                i++;
                            }
                        }
                    }
                }
    
                while (j < 7)
                {
                    if (kasa1 != KasaNo[j])
                    {
                        boskasa = kasa1;
                        break;
                    }
    
                    else if (kasa2 != KasaNo[j])
                    {
                        boskasa = kasa2;
                        break;
                    }
    
                    else if (kasa3 != KasaNo[j])
                    {
                        boskasa = kasa3;
                        break;
                    }
    
                    else if (kasa4 != KasaNo[j])
                    {
                        boskasa = kasa4;
                        break;
                    }
    
                    else if (kasa5 != KasaNo[j])
                    {
                        boskasa = kasa5;
                        break;
                    }
    
                    else if (kasa6 != KasaNo[j])
                    {
                        boskasa = kasa6;
                        break;
                    }
    
                    else if (kasa7 != KasaNo[j])
                    {
                        boskasa = kasa7;
                        break;
                    }
    
                    j++;
                }
    
                return boskasa;
            }
    

    Don't open the connection in one method and use it on another one and don't use long-lived connection objects.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Ali Dayan Sunday, April 17, 2016 3:08 PM
    Saturday, April 16, 2016 9:49 AM