Click here to Skip to main content
15,917,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read excel file and save it into dataset then put into database.
The code is work fine when debugging, means I can upload the file,read it and save into database.
But after I publish the project.
It show me error System.NullReferenceException: Object reference not set to an instance of an object.

C#
string path = System.IO.Path.GetFullPath(vault + "/" + FileUpload1.FileName);
         DataSet ds = new DataSet();
         ds = sctt.utility.GetValue.ReadExcelFile(path, "SELECT * FROM [Sheet1$]");
         string statusid = UCA.Common.Utility.GetValue.getSQLValue("SELECT TOP (1) Asset_Status_ID FROM Asset_Status WHERE (Name = 'Available')");
         if (ds.Tables[0].Rows.Count > 0)


The error message is at this line if (ds.Tables[0].Rows.Count > 0)
Posted
Comments
hueikar 17-Dec-13 2:32am    
Hi, I had solve the problem using another method : TextFieldParser. Thnx for all reply.

That probably means that either the method sctt.utility.GetValue.ReadExcelFile returned null, or you have not read any tables into your DataSet. Take a look at Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^] for information on reading Excel files in .NET.
 
Share this answer
 
Comments
BillWoodruff 16-Dec-13 4:46am    
Any time I post a response, and, then, find I composed the response while Richard MacCutchan was composing his, and then find our responses were ... in general ... along the same lines: I feel both humbled ... and that there is some hope for me, technically :)
Richard MacCutchan 16-Dec-13 4:49am    
Strange, I feel exactly the same when I see your answers.
hueikar 16-Dec-13 8:46am    
Hi, Yup. I try put (if dataset is null then label will show it)
So how can i debug it? Because it works well when I upload excel file in debug mode.
Then after I publish the system then its not working.
Richard MacCutchan 16-Dec-13 9:26am    
You need to add some more code to list out the items tha may be causing the failure. There are quite a few varibles in the code, so any one of them could be the root cause. Take a look at this CodeProject article for useful guidance on logging information from your release version.
I think there's a high probability that the code in the call to a method of the "sctt" return null. Why not check that by wrapping the code in something like:
C#
ds = sctt.utility.GetValue.ReadExcelFile(path, "SELECT * FROM [Sheet1$]");
// check for null dataset
if (ds == null)
{
     MessageBox.Show("ds is null);
     
     // code to abort ... or whatever
{
else
{
     string statusid = UCA.Common.Utility.GetValue.getSQLValue("SELECT TOP (1) Asset_Status_ID FROM Asset_Status WHERE (Name = 'Available')");
     if (ds.Tables[0].Rows.Count > 0) // ?
}
Or, use a try/catch/finally block.
 
Share this answer
 
dont do anything just write your code in try catch and see some value is getting null value

think and do bro it will solve automatically just try!
 
Share this answer
 
Comments
Ron Beyer 16-Dec-13 23:52pm    
Exceptions don't get solved automatically just because you put them in try/catch blocks, it just suppresses the exception message if you are not handling it correctly.
Gaurav Makwana 17-Dec-13 3:19am    
before automatically i wrote think and do its not big bug or error for the beginner also so plz just think and do

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900