Solved

Assigning headers to a datatable

Posted on 2009-05-12
3
264 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now