Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Source Error: 


Line 60:         protected void btnUpload_Click(object sender, EventArgs e)
Line 61:         {
Line 62:             string filePath = FileUpload1.PostedFile.FileName;
Line 63:             string filename = Path.GetFileName(filePath);
Line 64:             string ext = Path.GetExtension(filename);
 

Source File: c:\Users\KT\Documents\Visual Studio 2010\WebSites\OMC1\Manager\Upload_Report.aspx.cs    Line: 62 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   Upload_Report.btnUpload_Click(Object sender, EventArgs e) in c:\Users\KT\Documents\Visual Studio 2010\WebSites\OMC1\Manager\Upload_Report.aspx.cs:62
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint

please help me on this problem.i have try so many ways but still display the error.i really appreciate if someone can solve this problem.

thanks in advance
musiw.
Posted
Updated 30-May-12 18:45pm
v2

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line (Potentially: FileUpload1.PostedFile.FileName). Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same. Use FileUpload1.HasFile as a check bfore accessing PostedFile and it's properties.
 
Share this answer
 
Comments
johannesnestler 31-May-12 4:07am    
good advice
Sandeep Mewara 31-May-12 4:11am    
Thanks.
hi , this might solve your problem.
C#
if (FileUpload1.HasFile)
    try
    {
        string filePath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
    }
    catch (Exception ex)
    {
        Label1.Text = "ERROR: " + ex.Message.ToString();
    }
else
{
    Label1.Text = "You have not specified a file.";
}
 
Share this answer
 
Comments
musiw 31-May-12 1:19am    
i already try your solution, but the error still appear.it seems like fileupload.hasfile return false. how to solve this problem??
tanweer 31-May-12 1:39am    
it seems that your upload control is inside a UpdatePanel.
You cannot upload files using AJAX => you should not be placing a FileUpload control inside an UpdatePanel because this UpdatePanel sends an AJAX request to the server.
tanweer 31-May-12 1:41am    
if you still want to use UpdatePanel then try like in this
link
musiw 31-May-12 1:55am    
my web page is using contentplaceholder.which has site master and the file css in there.
musiw 31-May-12 2:48am    
the contentplaceholder is in the master page.then the other page will follow the master' content.im using vs2010.
try this
before that check whether there is file in fileuplod using if case
then use the code below
string filename = Path.GetFileName(FileUpload1.FileName);
 
Share this answer
 

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

  Print Answers RSS


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