Solved

Assigning headers to a datatable

Posted on 2009-05-12
3
281 Views
Last Modified: 2012-05-06
I am reading a table of data into excel using ado.net.  Unfortunately my data and the headers for that data are separated on the excel sheet(the headers are actually several hundred rows below the start of the data)  I use the below code to import the table into a datatable and it works wonderfully.  How would I get the header data from the "header" table to become the header for the "data" table?
myDataSet = new DataSet();
            filename = "";
 
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
            dlg.DefaultExt = ".xls"; // Default file extension
            dlg.Filter = "Excel documents (.xls)|*.xls"; // Filter files by extension
 
            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();
 
            string file;
            if (result == true)
            {
                // Open document
                file = dlg.FileName;
                int filedel = file.LastIndexOf("\\");
                filename = file.Remove(0, filedel+1);
            }
            //else end function
            else
            {
                return;
            }
 
 
            string connectString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties= ";
        connectString = connectString+ @"""Excel 8.0;HDR=YES;""";
 
 
 
            OleDbConnection connection =
                    new OleDbConnection(connectString);
 
            string query = "SELECT * FROM [Lap Sim$B24:FG724] "; //where (NOT ([Bank] IS NULL))
            string queryheader = "SELECT * FROM [Lap Sim$B751:FG751] "; //where (NOT ([Bank] IS NULL))
            
            
            OleDbConnection myAccessConn = null;
            try
            {
                myAccessConn = new OleDbConnection(connectString);
            }
            catch(Exception ex)
            {
                  Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                  return;
            }
 
            try
            {
 
                OleDbCommand myAccessCommand = new OleDbCommand(query, myAccessConn);
                  OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
 
                  myAccessConn.Open();
                  myDataAdapter.Fill(myDataSet,"data");
 
 
 
                  myAccessCommand = new OleDbCommand(queryheader, myAccessConn);
                  myDataAdapter = new OleDbDataAdapter(myAccessCommand);
 
                  //myAccessConn.Open();
                  myDataAdapter.Fill(myDataSet, "header");
 
            }
            catch (Exception ex)
            {
                  Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
                  return;
            }
            finally
            {
                myAccessConn.Close();
                connection.Close();
            }
 
  
            cmbXAxis.Items.Clear();
            cmbYAxis.Items.Clear();
            foreach (DataColumn column in myDataSet.Tables["data"].Columns)
            {
                string colname = column.ColumnName.ToString();
                cmbXAxis.Items.Add(colname);
                cmbYAxis.Items.Add(colname);
            }
            
 
        }

Open in new window

0
Comment
Question by:perrytaylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Rimvis earned 500 total points
ID: 24371843
Like this?
...
            finally
            {
                myAccessConn.Close();
                connection.Close();
            }
            
            //Rename columns
            for (int i = 0; i < myDataSet.Tables["header"].Columns.Count; i++)
            {
                DataColumn dc = myDataSet.Tables["data"].Columns[i]; 
                dc.Caption = dc.ColumnName = myDataSet.Tables["header"].Columns[i].ColumnName;
            }
...

Open in new window

0
 

Author Closing Comment

by:perrytaylor
ID: 31580631
This is actually what I came up with that seems to work,  what is the dc.caption all about?
thanks

string name;
            for(int b = 0; b
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 24373694
"dc.Caption = dc.ColumnName =" sets same value for both Caption and ColumnName properties. I was not sure which property you are referring as "header "
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question