Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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 :)
Posted
Updated 10-Aug-18 2:33am

1 solution

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
C#
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;

}
 
Share this answer
 
Comments
SRK90 20-Jul-13 11:42am    
I have got what you are trying to mean friend...yeah actually that is true...now i have solved this problem by myself...the problem occured due to link button,because the postback is happening when i click the respective link button[which is making the FileUpload into empty]...i have used ScriptManager and UpdatePanel to overcome this...anyways thanks a lot for your comment :)
Lalyka 26-Aug-15 7:35am    
Hi Rajesh
can you update your code ,I have the same problem
Member 12648331 10-Aug-16 7:50am    
NullReferenceException was unhandled by user code
I have error in this line
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}

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



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