Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have a file-up-loader inside grid view. I want to update y database. I want to upload image into database. While using the given below code showing an error. The error is : Object reference not set to an instance of an object. Help me to find a proper solution. Thank you.


C#
protected void gvEdit_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
     byte[] imageData = ((FileUpload)gvEdit.Rows[e.RowIndex].Cells[9].FindControl("FileUpload1")).FileBytes;
      
     string Id = gvEdit.DataKeys[e.RowIndex].Value.ToString();
     int ID = Convert.ToInt32(Id);

     string userName = Page.User.Identity.Name; 

      ShadingAnalysisDataSetTableAdapters.tbl_SiteLayOutUploadTableAdapter sl;
      sl = new ShadingAnalysisDataSetTableAdapters.tbl_SiteLayOutUploadTableAdapter();
      DataTable dt = new DataTable();

      sl.UpdateImageData(DateTime.Now, userName, imageData, ID);

      gvEdit.EditIndex = -1;
      BindData();
        
    }



ASP.NET
<asp:GridView ID="gvEdit" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="Id" Width="966px" OnRowCancelingEdit="gvEdit_RowCancelingEdit" OnRowDeleting="gvEdit_RowDeleting" OnRowEditing="gvEdit_RowEditing" OnRowUpdating="gvEdit_RowUpdating" OnRowDataBound="gvEdit_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Sl No" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" SortExpression="State" />
<asp:BoundField DataField="District" HeaderText="District" ReadOnly="True" SortExpression="District" />
<asp:BoundField DataField="Cluster" HeaderText="Cluster" ReadOnly="True" SortExpression="Cluster" />
<asp:BoundField DataField="Site_Name" HeaderText="Site Name" ReadOnly="True" SortExpression="Site_Name" />
<asp:BoundField DataField="Site_ID" HeaderText="Site ID" ReadOnly="True" SortExpression="Site_ID" />
<asp:BoundField DataField="Created_Date" HeaderText="Created Date" ReadOnly="True" SortExpression="Created_Date" />
<asp:BoundField DataField="Created_By" HeaderText="Created By" ReadOnly="True" SortExpression="Created_By" />
<asp:BoundField DataField="Version_No" HeaderText="Version No" ReadOnly="True" SortExpression="Version_No" />
<asp:TemplateField HeaderText="Image FIle" SortExpression="ImgData">
 <EditItemTemplate>
   <asp:FileUpload ID="FileUpload2" runat="server" />
 </EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Image">
 <EditItemTemplate>
   <asp:Image ID="Image1" runat="server" Width="150px" Height="150px"/>
 </EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
 </ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>
Posted
Comments
Mukesh Pr@sad 2-Dec-14 2:28am    
In which line you are getting error

Have a look at past answers of Sergey Alexandrovich Kryukov[^]. He explain very well, how to resolve this kind of errors.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Dec-14 3:42am    
My 5; and could you please tell us where those CodeProject search keywords (like author:"*") are documented. The query syntax looks like Google's, but where the keys come from?

...ah, I can see it on the query input control tooltip, but there are only two keys, "author" and "tag". I wish it would be everything, which would be way more efficient than clicking on those check boxes and list boxes... Anyway, thank you for the idea.

—SA
Maciej Los 2-Dec-14 5:40am    
Thank you, Sergey ;)
And you're very welcome ;)
The error is generating by the fact that you are using for your FileUpload control an ID namedFileUpload2, but in your code behind you are searching by a different Id FileUpload1 ==> so because the control is not find and null object will be returned.
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 2-Dec-14 3:22am    
perfect

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