Saturday, March 24, 2012

Object reference not set to an instance of an object (problem using fileupload in codebehi

I am using file upload and when tying to get the uploaded file it is saying "object reference not set to an instance of an object" I even tried to get the control but still no luck. Can anybody give the solution for this. I have searched many topics related to this forum and even in web but nothing seems to working. Posting my code below,

upload.aspx

<

asp:ViewID="loadingData"runat="server"><asp:ImageID="Image2"onmouseover="changeTabPicture(id)"onmouseout="changeTabPictureBack(id)"runat="server"ImageUrl="~/Images/SelectOff.gif"/><asp:ImageID="Image4"runat="server"ImageUrl="~/Images/LoadOn.gif"/><asp:ImageID="Image9"onmouseover="changeTabPicture(id)"onmouseout="changeTabPictureBack(id)"runat="server"ImageUrl="~/Images/EditOff.gif"/><asp:ImageID="Image10"onmouseover="changeTabPicture(id)"onmouseout="changeTabPictureBack(id)"runat="server"ImageUrl="~/Images/FinalizeOff.gif"/><br/><asp:WebPartZoneID="LoadingWebPart"runat="server"BorderColor="#CCCCCC"Font-Names="Verdana"Padding="6"><ZoneTemplate><asp:PanelID="uploadPanel"runat="server"Height="50px"Width="125px"Enabled=true>"align="left"bgcolor="lemonchiffon"border="1"bordercolor="firebrick"style="height: 158px"visible="true"width="180">

<asp:FileUploadID="OFDSelectFile"runat="server"Width="286"Enabled=true/>

<asp:button id="button1" runat=server onClick="Submit_Click"/>

</zonetemplate>

upload.aspx.cs

protected void submit_click(object sender, evenetargs e)

{

string filepath = OFDSelectFile.PostedFile.FileName; //it gives error in this place

}

Let me know as soon as possible I have a demo to do on monday.

Are you declaring your OFDSelectFile at the top of your code-behind page? When you add a control to your HTML page it isn't added to the code-behind unless you flip your HTML to design-view. It's a little gotcha thing. If you are not declaring your OFDSelectFile at the top then add it manually, or try the design view trick.

I tried to define soemthing like this "protected System.Web.UI.WebControls.FileUpload OFDSelectFile;" on the code-behind but it says the page already has the definition for it. Can you please elaborate on how to use it or if there is any other trick please let me know.


dev_discuss:

I tried to define soemthing like this "protected System.Web.UI.WebControls.FileUpload OFDSelectFile;" on the code-behind but it says the page already has the definition for it. Can you please elaborate on how to use it or if there is any other trick please let me know.

You may want to turn on Tracing and look int he "Control Tree" to confirm that the generated ID of the control indeed is *just* "OFDSelectFile"

for info on ID's and Naming Containers, check out this good article:
http://odetocode.com/Blogs/scott/archive/2006/03/12/3091.aspx


Hi,

I am not sure completely how to get the id of the control, I had this code worked on other aspx page but I integrated my code with other and it is at that point it started not to work. One difference I found when I moved the code in the code behind I had something like "public partialclass Import : System.Web.UI.UserControl" for which the above code was working but when I copied and pasted my code in another page which had "public partial class _Default:Syste.web.ui.page" and the above code is not working .

I am not sure whether this is the reason it is not working because I had this code working in other pages.

Please let me know if you have any idea about this.


File upload was not working because I was usign it inside atlas update panel. If I use it outside then everything works fine.

0 comments:

Post a Comment