Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace practice2
{
    public partial class Form1 : Form
    {
       private OleDbConnection connection = new OleDbConnection();
       
        public Form1()
        {
            InitializeComponent();
             connection.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\Ahmed ch\Documents\Database1.accdb;
            Persist Security Info = False;";
           
        }

      
        private void Form1_Load(object sender, EventArgs e)
        {
           try
            {
            connection.Open();
           
                OleDbCommand comand = new OleDbCommand();
                String query =  "select* from Sheet2"
                comand.CommandText = query;
                OleDbDataReader reader = comand.ExecuteReader();
                while (reader.Read())
                {
                     comboBox1.Items.Add(reader["ProductName"].ToString());
                }


                connection.Close();
               
            }
                catch(Exception ex)
            {
                MessageBox.Show("Error" + ex);
                
            }
           
       
        }}}


What I have tried:

i also install oledb driver , MS Access database Engine X86 , also X64 , but every time error comes ,
C#
OleDbDataReader reader = comand.ExecuteReader();
in this line
Posted
Updated 5-Aug-16 11:52am
v2
Comments
Karthik_Mahalingam 5-Aug-16 12:54pm    
what is the error message?
[no name] 5-Aug-16 12:59pm    
You forgot to set the Connection to your OleDBCommand.

Btw: I prefer to do it this way: OleDbCommand command = new OleDbCommand(sql, oleDbConnection);
ZurdoDev 5-Aug-16 13:46pm    
The error would tell you, and us, exactly what the problem is. Then bingo, problem solved faster than the amount of time it took you to ask. ;)

The exception you get is most probably "System.InvalidOperationException: ExecuteReader: Connection property has not been initialized". This because you forgot to set the Connection property of the OleDBCOmmand.

C#
private void Form1_Load(object sender, EventArgs e)
{
  try
  {
    connection.Open();

    OleDbCommand comand = new OleDbCommand();
    String query = "select* from Sheet2"
    comand.CommandText = query;
    comand.Connection = connection;  // <<<< set connection
    OleDbDataReader reader = comand.ExecuteReader();
  // ....
}
 
Share this answer
 
Comments
Karthik_Mahalingam 5-Aug-16 14:16pm    
5,good catch
[no name] 5-Aug-16 14:17pm    
Thank you.
ahmed ch 5-Aug-16 22:02pm    
Thank you , error solved by this way
[no name] 6-Aug-16 1:54am    
You are welcome.
Two possible problems: one is (as 0x01AA said) that the command is not connected to any database. The other is that you should have a space between "SELECT" and "*" in your SQL command - it's not required, but it's a good idea - particularly as "SELECT *" is considered a bad idea anyway. You should name your columns in the SELECT so that you only fetch the ones you need.
In addition, Commands and Connections are scarce resources, and it's a good idea to use a using block to ensure they are closed and disposed as needed.

C#
try
    {
    using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\Ahmed ch\Documents\Database1.accdb;Persist Security Info = False;";))
        {
        con.Open();
        using (OleDbCommand cmd = new OleDbCommand("SELECT ProductName FROM Sheet2", con))
            {
            using (OleDbDataReader reader = cmd.ExecuteReader())
                {
                while (reader.Read())
                    {
                    comboBox1.Items.Add(reader["ProductName"].ToString());
                    }
                }
            }
        }
    }
catch(Exception ex)
    {
    MessageBox.Show("Error" + ex);
    }
 
Share this answer
 
Comments
[no name] 5-Aug-16 15:02pm    
A 5 for all the hints.

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