How to get excel column names into a C# combo box?

hi i am trying get a excel sheet from anywhere in the pc and whenever someone browse and select a excel sheet i want its column names to be listed in a combo box. So far i had code the hard part and i still get a little error saying "The Microsoft Access database engine could not find the object 'columns'. Make sure the object exists and that you spell its name and the path name correctly. If 'columns' is not a local object, check your network connection or contact the server administrator" But i have successfully implement same code on sql server database. this error come only when i try to access a excel sheet. why this error coming and how to fix it? thank you.

    private String openfile()
    {

        string tempPath = "";
        OpenFileDialog fDialog = new OpenFileDialog();
        fDialog.Title = "open MSexcel file ";
        fDialog.Filter = "All Files(*.*)|*.*";
        fDialog.InitialDirectory = @"C:\";
        if (fDialog.ShowDialog() == DialogResult.OK)
        {

              tempPath = fDialog.FileName;
              return tempPath;

        }
        return null;
    }

    private void openpath(string path)
    {

        newconn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source="
                   + Convert.ToString(path) + ";Extended Properties=\"Excel 12.0;HDR={1}\";");




    }

    private void loadtolist()
    {
        newconn.Open();
        cmb.Items.Clear(); //cmb is combo box name
        newcmd = new OleDbCommand();
        newcmd.Connection = newconn;
        newcmd.CommandType = CommandType.Text;
        newcmd.CommandText = "SELECT column_name from information_schema.columns where table_name = 'BigData' Order by ordinal_position";

        dr = newcmd.ExecuteReader();
        if (dr.HasRows)

        {
            while (dr.Read())
            {
                cmb.Items.Add(dr[0].ToString());
            }

        }
        dr.Close();
        newconn.Close();
    }

Answers


Give a try to this..

    string connectionString = string.Format(Thread.CurrentThread.CurrentCulture, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES;'", excelFilePath);
    DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
    using (DbConnection connection = factory.CreateConnection())
    {
        connection.ConnectionString = connectionString;
        using (DbCommand command = connection.CreateCommand())
        {
            command.CommandText = @"SELECT [File], [ItemName], [ItemDescription], [Photographer name], [Date], [Environment site] FROM [Metadata$]";
            connection.Open();
            using (DbDataReader dr = command.ExecuteReader())
            {
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        .......
                    }
                }
            }
            connection.Close();
        }
    }

Need Your Help

IndexOf, encrypting, array help in java

java arrays encryption indexof

I know I'm not supposed to do this but I am a beginner and I need help on this. I am wondering what I need in each number on the bottom. I am not asking for the code but just help on what to use.