473,654 Members | 3,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 sConnectionStri ng =
"Provider=Micro soft.Jet.OLEDB. 4.0;" +
"Data Source=" + filename + ";" +
"Extended Properties=Exce l 8.0;";
OleDbConnection objConn = new
OleDbConnection (sConnectionStr ing);

objConn.Open();

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

OleDbDataAdapte r objAdapter1 = new OleDbDataAdapte r();

objAdapter1.Sel ectCommand = objCmdSelect;

DataSet dset = new DataSet();

objAdapter1.Fil l(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.Ad d(inRow);
}
}
inSet.Tables.Ad d(inTable);

loadGrid.DataSo urce = inSet;
loadGrid.DataBi nd();

}

May 1 '07 #1
2 5613
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.co m

<rw*****@iastat e.eduwrote in message
news:11******** **************@ y80g2000hsf.goo glegroups.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 sConnectionStri ng =
"Provider=Micro soft.Jet.OLEDB. 4.0;" +
"Data Source=" + filename + ";" +
"Extended Properties=Exce l 8.0;";
OleDbConnection objConn = new
OleDbConnection (sConnectionStr ing);

objConn.Open();

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

OleDbDataAdapte r objAdapter1 = new OleDbDataAdapte r();

objAdapter1.Sel ectCommand = objCmdSelect;

DataSet dset = new DataSet();

objAdapter1.Fil l(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.Ad d(inRow);
}
}
inSet.Tables.Ad d(inTable);

loadGrid.DataSo urce = inSet;
loadGrid.DataBi nd();

}

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
4674
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 written the driver prg in c#. The code opens the file, but it is not writting the values from text file into excel file. I can not figure out the problem here. This is my code: StreamReader dataFileReader = null; FileInfo file = null;...
1
2027
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? > > > Hi All, > I want to know,How can one read an Excel file into C,cell by cell?
4
5198
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 advanced. rgds,
5
8935
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 headers). I used ODBC in my VB.NET program to read that spreadsheet into a dataset, to make it easy to manipulate. The code I use to read it is as the bottom of this posting.
6
7031
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 Properties=Excel 8.0;" .Open() End With
11
6069
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 dicts with larger no. of features. Now i want to check each pair to see if they are connected...element of this pair will be one from the first list and one from the second....e.g for 'ab' i want to check if 1 and 3 are connected,then 1 and...
9
22488
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 namespaces and classes should I use and how? -- dba123
1
3735
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 alphanumeric data. How can I upload the file into a datagrid as it is? Is it possible? I do not want ASP.NET to automatically determine the data type. Is it possible to just upload the numbers as strings as well? What are my other options?...
1
4698
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\""; Column Name 'Sol 1' and 'Sol 2' is missing even though it is reading the readings. Just not able to read column names. I am not sure about what is exact problem. so please help me in solving this. Thanx in advance.
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.