对象引用未设置为对象的实例 - 在ASP.NET中的fileupload1期间 [英] Object reference not set to an instance of an object - during fileupload1 in ASP.NET
问题描述
在FileUpload1期间,我得到对象引用未设置为对象的实例。我知道fileuplod1中的问题是null值。但是,如果我在另一个项目上运行相同的代码,那么它工作正常。但是当我在我的实际项目上运行此代码时,它会收到错误对象引用未设置为对象的实例。
请帮助一些...
我尝试过:
string filename = Path.GetFileName(FileUpload1 .PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using(Stream fs = FileUpload1.PostedFile.InputStream)
{
使用(BinaryReader br = new BinaryReader(fs))
{
byte [] bytes = br.ReadBytes((Int32)fs .Length);
string constr = ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString;
using(SqlConnection con = new SqlConnection(constr))
{
string query =插入tblFiles值(@Name,@ Conten tType,@ data);
使用(SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue(@ Name,filename);
cmd.Parameters.AddWithValue(@ ContentType,contentType);
cmd.Parameters.AddWithValue(@ Data,bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
BindGrid();
}
Response.Redirect(Request.Url.AbsoluteUri);
}
I get "Object reference not set to an instance of an object" - during FileUpload1. I know the issue is null value in fileuplod1. But if I run the same code on another project then it is working fine. But when I run this code on my actual project it gets error "Object reference not set to an instance of an object".
Please help some...
What I have tried:
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles values (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
BindGrid();
}
Response.Redirect(Request.Url.AbsoluteUri);
}
推荐答案
在执行回发/上传按钮cli之前,请检查控件中的文件ck
Check the File in the control before doing postback/upload button click
if (FileUpload1.HasFile)
{
// your code
}
添加带异常的try / catch并获取堆栈跟踪以进行调试。
add try / catch with exception and get stack trace to debug.
这篇关于对象引用未设置为对象的实例 - 在ASP.NET中的fileupload1期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!