1
Answer

Object reference not set to an instance of an object.

Sneha Sneha

Sneha Sneha

9y
302
1
Grid.aspx
 
<asp:GridView ID="statusgrdview" runat="server" AutoGenerateColumns="false" CellSpacing="10" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1016px" OnSelectedIndexChanged="statusgrdview_SelectedIndexChanged" OnRowDataBound="statusgrdview_RowDataBound" OnRowCommand="statusgrdview_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
      <asp:TemplateField ItemStyle-Width="20px">
               <ItemTemplate>
                        <a href="JavaScript:divexpandcollapse('div<%# Eval("ID") %>');">
                        <img id='imgdiv<%# Eval("ID") %>' width="70px" height="25px" border="0" src="Image/command.PNG"/></a>
                        <asp:TextBox ID="txtcommt" runat="server"></asp:TextBox>
                        <asp:Button ID="btncmd" runat="server" Text="Post" OnClick="txtcmd_Click" />
                        <asp:ImageButton ID="imglike" runat="server" ImageUrl="~/Image/like.png" Height="25px" Width="25px" OnClick="imglike_Click" />
                        <asp:Label ID="lbllike" runat="server"></asp:Label>
            </ItemTemplate>
         <ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>
   </asp:TemplateField>
<asp:TemplateField>
         <ItemTemplate>
               <asp:Label ID="lblID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
         </ItemTemplate>
</asp:TemplateField>
 
<asp:BoundField DataField="Post_data"/>
         <asp:TemplateField>
                  <ItemTemplate>
                              <tr>
                                    <td colspan="100%" style="background:#F5F5F5">
                                             <asp:DetailsView id="DetailsView1" DataKeyNames="ID" Runat="server" Width="300px" Font-Names="Calibri" OnItemInserting="DetailsView1_ItemInserting"/>
                                    </td>
                              </tr>
               </ItemTemplate>
         </asp:TemplateField>
   </Columns>
</asp:GridView>
 
Grid.aspx.cs
    
protected void txtcmd_Click(object sender, EventArgs e)
{
         string status = Convert.ToInt32(statusgrdview.DataKeys[e.RowIndex].Value);             
         string comments = ((TextBox)statusgrdview.FindControl("txtcommt")).Text;         Getting ERROR: Object reference not set to an instance of an object.       
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
         SqlCommand cmd = new SqlCommand();
         int ID = int.Parse(Session["Id"].ToString());
         TextBox box1 = (TextBox)statusgrdview.Rows[0].Cells[0].FindControl("txtcommt");
         string k = box1.Text;
         cmd.CommandType = CommandType.Text;
         cmd.CommandText = "insert into TBL_Comments(ID,comments) " + "values(" + ID + ",@comments);" + "select * from TBL_Comments";
         cmd.Parameters.Add("@comments", SqlDbType.VarChar).Value = comments;
         statusgrdview.DataSource = GetData(cmd);
         statusgrdview.DataBind();
}
 

Answers (1)