-1

i am getting error of Object reference not set to an instance of an object.

the error is coming on the following line

cmd.Parameters.AddWithValue("empname", empname);

and this is how i did the coding of findcontrol

    GridViewRow grdupd = grdemp.Rows[e.RowIndex];
    studentclass std = new studentclass();
    Label empid = (Label)grdupd.FindControl("lblempid");
    TextBox empname = (TextBox)grdupd.FindControl("txtname");

and here is my gridview

            <asp:TemplateField HeaderText="Employee Name">
                <ItemTemplate>
                    <asp:Label ID="lblname" runat="server" Text='<%#Eval("empname") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("empname") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>

what is wrong in my code and why i am getting the error of null reference and why i am not able to pass data from gridview to backend

  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – J. Steen Jul 08 '17 at 10:58
  • correct your title –  Jul 08 '17 at 12:00

1 Answers1

0

If the error is in below line as you say then definitely you haven't declared SqlCommand object saying SqlCommand cmd = new SqlCommand(...)and so the error

cmd.Parameters.AddWithValue("empname", empname);

Also, as commented by Steen; you are actually adding the TextBox control as parameter whereas you should use the Text property to do so like

cmd.Parameters.AddWithValue("empname", empname.Text);
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • There's also the matter of them trying to add a textbox as a parameter to a sql command, rather than the *value* of the textbox. But that's hardly their biggest issue. – J. Steen Jul 08 '17 at 10:59