如何处理“NullReferenceException未被用户代码处理” for C中的FileUpload [英] how to handle "NullReferenceException was unhandled by user code" for FileUpload in C#

查看:71
本文介绍了如何处理“NullReferenceException未被用户代码处理” for C中的FileUpload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个公司实习的示例项目。

根据项目,新用户必须在注册屏幕上注册他们的账户..

我们可以选择上传我们的个人资料图片...

我已经提到了下面的照片上传编码....



< b> protected void NextLinkbutton3_OnClick(object sender,EventArgs e)

{

Session [ProfilePictureBytes] = 0;

if(ProfilePictureFileUploader。 HasFile == true)

{



byte [] br = File.ReadAllBytes(D:/ Projects / SampleProject / Images / Default Image.jpg);

Session [ProfilePictureBytes] = br;



}

else < br $>
{



System.IO.Stream fs = ProfilePictureFileUploader.PostedFile.InputStream;

System.IO.BinaryReader br = n ew System.IO.BinaryReader(fs);

byte [] profilepictureBytes = br.ReadBytes((Int32)fs.Length);

Session [ProfilePictureBytes] = profilepictureBytes;

}



这里的ProfilePictureFileUploader是我在aspx页面中提到的F​​ileUpload。

我已经应用了一个名为Session [ProfilePictureBytes]的会话,它将保存图像的字节。 [Session [ProfilePictureBytes]的目的是在其他屏幕中检索这些字节。]

如果用户上传图片,那么该工具将假设否则它将执行更多的事情......但是如果用户没有上传任何图片,那么图片将从我的本地D盘中获取并且它将图片安全地保存在日期库中...但是如果用户上传任何图片然后其他部分显示一些错误,如 NullReferenceException未被用户代码处理 ..对于这个问题的朋友有什么想法吗? ....



非常抱歉我不成熟的英语并提前感谢:)

I am doing a sample project for an internship in a company.
According to the project the New user have to register their account in the registration screen..
there we have an option to upload our profile picture...
I have mentioned the photo upload codings below....

protected void NextLinkbutton3_OnClick(object sender, EventArgs e)
{
Session["ProfilePictureBytes"] = 0;
if (ProfilePictureFileUploader.HasFile == true)
{

byte[] br = File.ReadAllBytes("D:/Projects/SampleProject/Images/Default Image.jpg");
Session["ProfilePictureBytes"] = br;

}
else
{

System.IO.Stream fs = ProfilePictureFileUploader.PostedFile.InputStream;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
byte[] profilepictureBytes = br.ReadBytes((Int32)fs.Length);
Session["ProfilePictureBytes"] = profilepictureBytes;
}


here the ProfilePictureFileUploader is the FileUpload that i have mentioned in my aspx page.
I have applied a Session named Session["ProfilePictureBytes"] which will save the bytes of the image. [the purpose of Session["ProfilePictureBytes"] is to retrieve these bytes in some other screen.]
here if the user upload a picture,then the tool will assume the else part and it will do the further things....but if the user is not uploading any picture,then the picture will be taken from my local D drive and it's saving the picture safely in the datebase...but if the user upload any picture then the else part is showing some error like "NullReferenceException was unhandled by user code" ..any idea for this problem friends? ....

Very sorry for my immature english and thanks in advance :)

推荐答案

As我知道你已经实现了你的条件错误



如果(ProfilePictureFileUploader.HasFile == true)然后从上传者中选择图像并在会话中设置,否则从本地硬盘中选择默认图像开车位置。



在您的情况下,您正在检查



如果上传者有档案

然后

从本地硬盘挑选图片



其他

从上传者中挑选图片即

ProfilePictureFileUploader.PostedFile



所以在其他部分你会得到null异常,因为没有已发布文件。



重新编写代码

As i understand you have implemented your condition wrong

if (ProfilePictureFileUploader.HasFile == true) then pick image from uploader and set in session, otherwise pick default image from local hard drive location.

In your case you are checking as

if uploader has file
then
pick image from local hard drive

else
pick image from uploader i.e.
ProfilePictureFileUploader.PostedFile

so in else part you will get null exception, as there is no Posted File.

ReOrder your code as
if (ProfilePictureFileUploader.HasFile == true)
{
 System.IO.Stream fs = ProfilePictureFileUploader.PostedFile.InputStream;
 System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
 byte[] profilepictureBytes = br.ReadBytes((Int32)fs.Length);
 Session["ProfilePictureBytes"] = profilepictureBytes;

}
else
{

 byte[] br = File.ReadAllBytes("D:/Projects/SampleProject/Images/Default Image.jpg");
 Session["ProfilePictureBytes"] = br;

}


这篇关于如何处理“NullReferenceException未被用户代码处理” for C中的FileUpload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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