?
Solved

Assigning headers to a datatable

Posted on 2009-05-12
3
Medium Priority
?
293 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 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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