Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Object reference not set to an instance of an object.

Posted on 2013-05-22
2
Medium Priority
?
594 Views
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
code.txt
0
Comment
Question by:VBdotnet2005
2 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39191321
Hi,

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

Open in new window


Giannis
0
 
LVL 13

Accepted Solution

by:
Jitendra Patil earned 2000 total points
ID: 39193921
Hi

The problem lies in your aspx code.

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


view the complete code here
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
   
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel_1" runat="server">
            <ContentTemplate>
                <table>
                    <tr>
                        <td>
                            Upload File
                            <asp:FileUpload ID="FileUpload1" runat="server" Size="100" />&nbsp;&nbsp;
                            <asp:Button ID="btnUpload" runat="server" Text="Upload" />
                            &nbsp;<br />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <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" />
                                <Columns>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="CheckBox1" runat="server" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
                                    <asp:TemplateField HeaderText="filename">
                                        <ItemTemplate>
                                            <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>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <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" />
                                </Columns>
                                <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" />
                                <EmptyDataTemplate>
                                    <table bgcolor="#5D7B9D" style="width: 100%; border-style: solid; border-width: 1px">
                                        <tr>
                                            <td style="color: #FFFFFF">
                                                <center>
                                                    No Data Found</center>
                                            </td>
                                        </tr>
                                    </table>
                                </EmptyDataTemplate>
                            </asp:GridView>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myconConnectionString %>"
                                ProviderName="System.Data.SqlClient" SelectCommand="mysp"
                                SelectCommandType="StoredProcedure"></asp:SqlDataSource>
                            <asp:Button ID="Button1" runat="server" Text="Button" Visible="False" />
                            <asp:LinkButton ID="LinkButton1" runat="server">Clear</asp:LinkButton>
                            <br />
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
             <Triggers>
              <asp:PostBackTrigger ControlID="btnUpload" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel_1">
            <ProgressTemplate>
                <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">
                        <tr>
                            <td>
                                Please wait...<br />
                                <br />
                                <img src="image/test.gif" />
                            </td>
                        </tr>
                    </table>
                </div>
            </ProgressTemplate>
        </asp:UpdateProgress>
    </div>
</asp:Content>

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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

885 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