PC Review


Reply
Thread Tools Rate Thread

C# Problem reading numbers from excel file

 
 
rwiegel@iastate.edu
Guest
Posts: n/a
 
      1st May 2007
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();

}

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      1st May 2007
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]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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();
>
> }
>



 
Reply With Quote
 
rwiegel@iastate.edu
Guest
Posts: n/a
 
      1st May 2007
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??

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Reading File when Name contains character numbers 63 ("?"),164, 186, 243, 248 PHPBABY3 Windows Vista General Discussion 1 23rd Mar 2008 10:03 AM
Problem reading Excel 2003 file through OleDB Luismi Microsoft Excel Discussion 1 19th Mar 2007 10:44 PM
Re: URGENT: Problem in reading excel file in ASP :( - TRAILING SPACE problem Rob Bovey Microsoft Excel Programming 0 29th Mar 2004 05:47 PM
Re: URGENT: Problem in reading excel file in ASP :( - TRAILING SPACE problem Jim Rech Microsoft Excel Programming 0 29th Mar 2004 01:19 PM
Problem while reading excel file with mix datatypes =?Utf-8?B?UGF1cmF2aQ==?= Microsoft ASP .NET 1 4th Mar 2004 03:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:46 AM.