Error System.NullReferenceException:未将对象引用设置为对象的实例 [英] Error System.NullReferenceException: Object reference not set to an instance of an object

查看:278
本文介绍了Error System.NullReferenceException:未将对象引用设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取excel文件并将其保存到数据集中然后放入数据库。

调试时代码工作正常,意味着我可以上传文件,读取并保存到数据库中。

但是在我发布项目之后。

它向我显示错误System.NullReferenceException:对象引用未设置为对象的实例。



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.

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)





错误信息位于此行if(ds.Tables [0] .Rows.Count> 0)



The error message is at this line if (ds.Tables[0].Rows.Count > 0)

推荐答案

);
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
"); 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)





错误信息位于此行if(ds.Tables [0] .Rows。计数> 0)



The error message is at this line if (ds.Tables[0].Rows.Count > 0)


这可能意味着方法 sctt.utility.GetValue.ReadExce lFile 返回 null ,或者您没有在 DataSet 中读取任何表。请查看使用MS Excel(xls) / xlsx)使用MDAC和Oledb [ ^ ]有关在.NET中读取Excel文件的信息。
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.


我认为代码的可能性很高对sctt方法的调用返回null。为什么不通过将代码包装成以下内容来检查:
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:
ds = sctt.utility.GetValue.ReadExcelFile(path, "SELECT * FROM [Sheet1


这篇关于Error System.NullReferenceException:未将对象引用设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆