473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C# Problem reading numbers from excel file

I'm trying to read rows from an Excel file and display them in an
ASP.NET DataGridview. I am using C# for the code file. I am using
OleDb to read from the Excel file. The columns that contain text load
into the grid fine, but the columns that contain just numbers don't
show up at all. I tried converting the text of the cells to an
integer first, but I get an error for converting from a type DBNull.
Anybody who has any help at all, I would greatly appreciate it. Here
is the code I am using to read from the Excel and load the
Datagridview:

private void popGrid(String filename)
{
String sConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filename + ";" +
"Extended Properties=Excel 8.0;";
OleDbConnection objConn = new
OleDbConnection(sConnectionString);

objConn.Open();

OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM
[Sheet1$]", objConn);

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;

DataSet dset = new DataSet();

objAdapter1.Fill(dset);

objConn.Close();

DataSet inSet = new DataSet();
DataTable inTable = new DataTable();
inTable.Columns.Add("BLnum");
inTable.Columns.Add("Pieces");
inTable.Columns.Add("Weight");
inTable.Columns.Add("Size");
inTable.Columns.Add("custRel");
inTable.Columns.Add("ssRel");
inTable.Columns.Add("palletEx");
inTable.Columns.Add("pckDate");
inTable.Columns.Add("ldFee");

for (int i = 11; i < dset.Tables[0].Rows.Count; i++)
{

DataRow inRow = inTable.NewRow();
DataRow row = dset.Tables[0].Rows[i];
if (row[0].ToString() != "")
{
inRow["BLnum"] = row[0].ToString();//A
inRow["Pieces"] = row[2].ToString();//C
inRow["Weight"] = row[3].ToString();//D
inRow["Size"] = row[4].ToString();//E
inRow["custRel"] = row[5].ToString();//F
inRow["ssRel"] = row[6].ToString();//G
inRow["palletEx"] = row[6].ToString();//H
inRow["pckDate"] = row[8].ToString();//I
inRow["ldFee"] = row[10].ToString();//K
inTable.Rows.Add(inRow);
}
}
inSet.Tables.Add(inTable);

loadGrid.DataSource = inSet;
loadGrid.DataBind();

}

May 1 '07 #1
2 5593
Are you sure you are looking at the right column? You are getting a
DBNull, which when coming from Excel, more than likely means that the cell
was blank/empty.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rw*****@iastate.eduwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
I'm trying to read rows from an Excel file and display them in an
ASP.NET DataGridview. I am using C# for the code file. I am using
OleDb to read from the Excel file. The columns that contain text load
into the grid fine, but the columns that contain just numbers don't
show up at all. I tried converting the text of the cells to an
integer first, but I get an error for converting from a type DBNull.
Anybody who has any help at all, I would greatly appreciate it. Here
is the code I am using to read from the Excel and load the
Datagridview:

private void popGrid(String filename)
{
String sConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filename + ";" +
"Extended Properties=Excel 8.0;";
OleDbConnection objConn = new
OleDbConnection(sConnectionString);

objConn.Open();

OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM
[Sheet1$]", objConn);

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;

DataSet dset = new DataSet();

objAdapter1.Fill(dset);

objConn.Close();

DataSet inSet = new DataSet();
DataTable inTable = new DataTable();
inTable.Columns.Add("BLnum");
inTable.Columns.Add("Pieces");
inTable.Columns.Add("Weight");
inTable.Columns.Add("Size");
inTable.Columns.Add("custRel");
inTable.Columns.Add("ssRel");
inTable.Columns.Add("palletEx");
inTable.Columns.Add("pckDate");
inTable.Columns.Add("ldFee");

for (int i = 11; i < dset.Tables[0].Rows.Count; i++)
{

DataRow inRow = inTable.NewRow();
DataRow row = dset.Tables[0].Rows[i];
if (row[0].ToString() != "")
{
inRow["BLnum"] = row[0].ToString();//A
inRow["Pieces"] = row[2].ToString();//C
inRow["Weight"] = row[3].ToString();//D
inRow["Size"] = row[4].ToString();//E
inRow["custRel"] = row[5].ToString();//F
inRow["ssRel"] = row[6].ToString();//G
inRow["palletEx"] = row[6].ToString();//H
inRow["pckDate"] = row[8].ToString();//I
inRow["ldFee"] = row[10].ToString();//K
inTable.Rows.Add(inRow);
}
}
inSet.Tables.Add(inTable);

loadGrid.DataSource = inSet;
loadGrid.DataBind();

}

May 1 '07 #2
Thanks for the response. I'm sure I'm looking in the correct column,
because when I change the value in excel column to have any non-
numerical characters it displays the column properly. Any other
ideas??

May 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
1
by: Turner, GS \(Geoff\) | last post by:
> -----Original Message----- > From: siliconwafer > Posted At: 19 August 2005 15:20 > Posted To: c > Conversation: reading an excel file in C? > Subject: reading an excel file in C? > > >...
4
by: Phoebe. | last post by:
Hi, Good Day! Reading 1 excel file into a dataset is fine. How can I read multiple excel with the same data structure into 1 dataset? How can I append those data? Can someone help? Thanks in...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
6
by: simchajoy2000 | last post by:
Hi, So I do actually know how to read excel into VB.NET using the following approach: With oConn2 .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & FilePath & ";Extended...
11
by: Girish Sahani | last post by:
I wrote the following code to concatenate every 2 keys of a dictionary and their corresponding values. e.g if i have tiDict1 = tiDict1 = {'a':,'b':} i should get tiDict2={'ab':} and similarly for...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
1
by: Omar | last post by:
Hello Allo, I am creating a page that allows a user to upload an excel file, and display the contents in a datagrid. The problem I am having now is that, some of the columns have both numeric and...
1
by: sachinkale123 | last post by:
Hi, I am reading excel file and reading values from that I am using provider As : "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Filename + ";Extended Properties=\"Excel 8.0;Hdr=No;IMEX=1\"";...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.