Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4332846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:18:44+00:00 2026-05-21T10:18:44+00:00

I have an Excel worksheet I want to read into a datatable – all

  • 0

I have an Excel worksheet I want to read into a datatable – all is well except for one particular column in my Excel sheet. The column, ‘ProductID’, is a mix of values like ########## and n#########.

I tried to let OleDB handle everything by itself automatically by reading it into a dataset/datatable, but any values in ‘ProductID’ like n###### are missing, ignored, and left blank. I tried manually creating my DataTable by looping through each row with a datareader, but with the exact same results.

Here’s the code :

// add the column names manually to the datatable as column_1, column_2, ...
for (colnum = 0; colnum < num_columns; colnum ++){
  ds.Tables["products"].Columns.Add("column_" +colnum , System.Type.GetType("System.String")); 
}
while(myDataReader.Read()){
  // loop through each excel row adding a new respective datarow to my datatable 
  DataRow a_row = ds.Tables["products"].NewRow();
  for (col = 0; col < num_columns; col ++){
    try {  a_row[col] = rdr.GetString(col);  }
    catch {  a_row[col] = rdr.GetValue(col).ToString(); }
  }
  ds.Tables["products"].Rows.Add(a_row);
}

I don’t understand why it won’t let me read in values like n######. How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:18:45+00:00Added an answer on May 21, 2026 at 10:18 am

    Using .Net 4.0 and reading Excel files, I had a similar issue with OleDbDataAdapter – i.e. reading in a mixed data type on a “PartID” column in MS Excel, where a PartID value can be numeric (e.g. 561) or text (e.g. HL4354), even though the excel column was formatted as “Text”.

    From what I can tell, ADO.NET chooses the data type based on the majority of the values in the column (with a tie going to numeric data type). i.e. if most of the PartID’s in the sample set are numeric, ADO.NET will declare the column to be numeric. Therefore ADO.Net will attempt to cast each cell to a number, which will fail for the “text” PartID values and not import those “text” PartID’s.

    My solution was to set the OleDbConnection connectionstring to use Extended Properties=IMEX=1;HDR=NO to indicate this is an Import and that the table(s) will not include headers. The excel file has a header row, so in this case tell ado.net not to use it. Then later in the code, remove that header row from the dataset and voilà you have mixed data type for that column.

    string sql = "SELECT F1, F2, F3, F4, F5 FROM [sheet1$] WHERE F1 IS NOT NULL";
    
    OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + PrmPathExcelFile + @";Extended Properties=""Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text""");
    
    OleDbCommand cmd = new OleDbCommand(sql, connection);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    
    DataSet ds = new DataSet();
    ds.Tables.Add("xlsImport", "Excel");
    da.Fill(ds, "xlsImport");
    
    // Remove the first row (header row)
    DataRow rowDel = ds.Tables["xlsImport"].Rows[0];
    ds.Tables["xlsImport"].Rows.Remove(rowDel);
    
    ds.Tables["xlsImport"].Columns[0].ColumnName = "LocationID";
    ds.Tables["xlsImport"].Columns[1].ColumnName = "PartID";
    ds.Tables["xlsImport"].Columns[2].ColumnName = "Qty";
    ds.Tables["xlsImport"].Columns[3].ColumnName = "UserNotes";
    ds.Tables["xlsImport"].Columns[4].ColumnName = "UserID";
    
    connection.Close(); 
    

    // now you can use LINQ to search the fields

        var data = ds.Tables["xlsImport"].AsEnumerable();
        var query = data.Where(x => x.Field<string>("LocationID") == "COOKCOUNTY").Select(x =>
                    new Contact
                    {
                        LocationID= x.Field<string>("LocationID"),
                        PartID = x.Field<string>("PartID"),
                        Quantity = x.Field<string>("Qty"),
                        Notes = x.Field<string>("UserNotes"),
                        UserID = x.Field<string>("UserID")
                    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Excel worksheet where the user enters certain data, which I want
I have an excel worksheet with 2 comboboxes, and 3 scrollbars. I want to
I have an Excel worksheet with an image (logo). If I right-click on the
I have a chart in a Worksheet in Excel and I have a macro
I have an Excel application in which I want to present the user with
I have an Excel spreadsheet with 1 column, 700 rows. I care about every
I have an Excel worksheet that has the following macro. I'd like to loop
I have an excel worksheet with columns A, B, C...columns A and B have
I have an Excel worksheet in XML format which contains <Cell ss:StyleID=s127><Data ss:Type=String>Replace Me</Data></Cell>
I have an excel 2007 worksheet open with 5 colums and +/-5000 rows of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.