Object reference not set to an instance of an object.

Posted on 2013-05-22
Last Modified: 2013-06-03
In button click event, I got this error. I am not sure why.
Object reference not set to an instance of an object.

  Dim filename As String
  filename = Me.FileUpload1.PostedFile.FileName
Question by:VBdotnet2005
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39191321

Try the following code:
        filename = CType(Page.FindControl("FileUpload1"), FileUpload).PostedFile.FileName

Open in new window

LVL 13

Accepted Solution

Jitendra Patil earned 500 total points
ID: 39193921

The problem lies in your aspx code.

You need to add a trigger to your update panel as given below
              <asp:PostBackTrigger ControlID="btnUpload" />

view the complete code here
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:UpdatePanel ID="UpdatePanel_1" runat="server">
                            Upload File
                            <asp:FileUpload ID="FileUpload1" runat="server" Size="100" />&nbsp;&nbsp;
                            <asp:Button ID="btnUpload" runat="server" Text="Upload" />
                            &nbsp;<br />
                            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                                DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="Vertical"
                                HorizontalAlign="Center" Width="100%">
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                            <asp:CheckBox ID="CheckBox1" runat="server" />
                                    <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
                                    <asp:TemplateField HeaderText="filename">
                                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# string.format("test.aspx?={0}", Databinder.Eval(container.Dataitem, "filename")) %>'
                                                Text='<%# Databinder.Eval(container.dataitem, "filename") %>'></asp:HyperLink>
                                    <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="false" />
                                    <asp:BoundField DataField="map_client" HeaderStyle-HorizontalAlign="Center" HeaderText="Map Columns"
                                        ItemStyle-HorizontalAlign="Center" Visible="true" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                                <EditRowStyle BackColor="#999999" />
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                    <table bgcolor="#5D7B9D" style="width: 100%; border-style: solid; border-width: 1px">
                                            <td style="color: #FFFFFF">
                                                    No Data Found</center>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myconConnectionString %>"
                                ProviderName="System.Data.SqlClient" SelectCommand="mysp"
                            <asp:Button ID="Button1" runat="server" Text="Button" Visible="False" />
                            <asp:LinkButton ID="LinkButton1" runat="server">Clear</asp:LinkButton>
                            <br />
              <asp:PostBackTrigger ControlID="btnUpload" />
        <asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel_1">
                <div id="progress" align="center" valign="middle" runat="server" style="border-width: thin;
                    border-color: black; position: absolute; left: 0%; top: 4%; visibility: visible;
                    vertical-align: middle; background-color: White; z-index: 40;">
                    <table width="1054px" height="510px">
                                Please wait...<br />
                                <br />
                                <img src="image/test.gif" />

Open in new window

your backend code is working fine.

once you add a trigger to your  update panel you will get the desired result without performing a full postback, it will be a partial post back and you update panel will know that this button needs an action to be done.

hope this helps.

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question