'Fill: SelectCommand.Connection property has not been initialized

I am using the below code to access the MS Access Database. But i got a error message Fill: SelectCommand.Connection property has not been initialized.How can i solve this issue.

common.cs
=========
public static bool DBConnectionStatus()
        {
            try
            {
                string conString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|db_admin.mdb; Jet OLEDB:Database Password=admin";
                using (OleDbConnection conn = new OleDbConnection(conString))
                {
                    conn.Open();
                    return (conn.State == ConnectionState.Open);
                }
            }
            catch (OleDbException)
            {
                return false;
            }


protected void btn_general_Click(object sender, EventArgs e)
        {
            try
            {
                bool state = common.DBConnectionStatus();
                if (state == true)
                {
                    cmd = new OleDbCommand("select * from tbl_admin");
                    da = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();

                    da.Fill(ds); // Error Here 
                    if (ds.Tables[0].Rows.Count > 0)
                    {

                    }

                }
            }
            catch (Exception e1)
            {
            }
        }


Solution 1:[1]

I did suggest you to modify your code to something like this:

private DataTable YourData()  
{  
    string conString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|db_admin.mdb; Jet OLEDB:Database Password=admin";  
    DataSet ds = new DataSet();  
    using (SqlConnection conn = new SqlConnection(conString))  
    {  
        try  
        {  
            conn.Open();  
            SqlCommand command = new SqlCommand("select * from tbl_admin", conn);  
            command.CommandType = CommandType.Text;  

            SqlDataAdapter adapter = new SqlDataAdapter(command);  
            adapter.Fill(ds);  

            if (ds.Tables[0].Rows.Count > 0)  
            {  
                // do somethign here  
            }  
        }
        catch (Exception)  
        {
            /*Handle error*/  
        }  
    }  
    return ds.Tables[0];  
}  

And then:

protected void btn_general_Click(object sender, EventArgs e)
{          
     this.YourData(); // you will get the Data here. you can then use it the way you want it         
}

Solution 2:[2]

You are initializing a Command which you use to construct a DataAdapter, but both without setting the required Connection property:

cmd = new OleDbCommand("select * from tbl_admin"); // <-- no connectuion assigned
da = new OleDbDataAdapter(cmd);  // <-- no connectuion assigned

So your exception is pretty self-explanatory.

One final note: using will dispose/close the connection, so the method DBConnectionStatus is pointless. So don't use it, instead use the using in in the first place:

try
{
    using(var con = new OleDbConnection(connectionString))
    using(var da = new OleDbDataAdapter("elect * from tbl_admin", con))
    {
        var table = new DataTable();
        da.Fill(table); // note that you don't need to open the connection with DataAdapter.Fill
        if (table.Rows.Count > 0)
        {
            // ...
        }
    }
}
catch (Exception e1)
{
    // don't catch an exception if you don't handle it in a useful way(at least loggging)
    throw;
}

Solution 3:[3]

As per your requirement you can also use SqlDataAdapter instand of ExecuteReader.

public void ReadMyData(string connectionString)
{
    string queryString = "SELECT OrderID, CustomerID FROM Orders";
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
        }
        // always call Close when done reading.
        reader.Close();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Praveen Nambiar
Solution 2 Tim Schmelter
Solution 3 A.M. Patel