0

Hi i have an excel sheet with headers,i want to populate those headers into a dropdown list...

can any one help me with the select statement and procedure

i am working on a code i dnt know whether its correct or not

      DropDownList list = new DropDownList();
                    string connectionstring = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", Excelpath);  
                    string query1 =  String.Format("select * from [{0}]", DDlist.SelectedItem.Text);                                             
                    // OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query1, connectionstring);                                                
                    using (OleDbConnection conn1 = new OleDbConnection(connectionstring))
                    {
                        OleDbCommand odc1 = new OleDbCommand(string.Format(query1, conn1));
                        conn1.Open();
                        OleDbDataReader dr;
                        dr = odc1.ExecuteReader();
                        while (dr.Read())
                        {
                            list.Items.Add(dr[column.ColumnName].ToString());
                        }
                        dr.Close();
                        conn1.Close();
                    }

in this method i am geting an error at this line

    dr = odc1.ExecuteReader();Error:ExecuteReader: Connection property has not been initialized.

can any one help mw with this, thanks in advance

Dotnet ReSource
  • 191
  • 9
  • 29

3 Answers3

0

I think you are not passing Connection to OleDBCommand. So try,

OleDbCommand odc1 = new OleDbCommand(query1, conn1);
Nalaka526
  • 11,278
  • 21
  • 82
  • 116
0

The OleDbCommand class has two main parameters to be passed for a proper execution. That are Query and connection object. In your code you are passing the connection, but it is placed inside the String.Format(), so place it outside. See the code below....

OleDbCommand odc1 = new OleDbCommand(string.Format(query1, conn1)); 

TO

OleDbCommand odc1 = new OleDbCommand(query1, conn1); 
0

try this it may work.....

                  foreach (DataTable table in DS.Tables)
          {
               foreach (DataColumn column in table.Columns)
                {
                   DropDownList list = new DropDownList();
                   string connectionstring = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", Excelpath);
                   string query1 = String.Format("select * from [{0}]", DDlist.SelectedItem.Text);                                                                      
                   using (OleDbConnection conn1 = new OleDbConnection(connectionstring))
                   {
                       OleDbCommand odc1 = new OleDbCommand(query1, conn1);
                       conn1.Open();
                        DataTable dte = null;
                       DataSet ds = new DataSet();
                       dte = conn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                       OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
                       objAdapter1.SelectCommand = odc1;
                       objAdapter1.Fill(ds, "xldata");
                        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                       {
                           list.Items.Add(ds.Tables[0].Columns[i].ColumnName.ToString());
                       }
                       conn1.Close();
                    }
SoftwareNerd
  • 1,875
  • 8
  • 29
  • 58