Protected void Update(object sender, GridViewCommandEventsArg e)
{
if(e.CommandName == "Change")
{
        int index = GridViewemp.EditIndex;
        GridViewRow grid = GridViewemp.Rows[index];

        TextBox salary = (TextBox)GridViewemp.FindControl("TextBoxsalary");

         sqlcon.Open();
         string query = "Update Employee set Salary="+salary.Text+"where EmpID     ="+GridViewemp.SelectedDataKey.Value.ToString();
         sqlcom = new SqlCommand(query, sqlcon);
         sqlcom.ExecuteNonQuery();
         sqlcon.Close();
}

}

I am getting the error Error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.

Please help me to get the value from TextBoxsalary

I think there should be a miss match off datatype, if your Empid is a string type so it must be in single quote

string query = "Update Employee set Salary="+salary.Text+"where EmpID     ='"+GridViewemp.SelectedDataKey.Value.ToString()+"'";

I hope it will help you

Thanks for the reply prvnkmr194 but my EmpID is not a string. I forgot to mention that this grid is in a panel.

Could you tell us which line is throwing the exception so we don't have to validate that every object in your code exists?

line 8.. the error message is in the first post. textboxsalary does exist

Where is the code that defines TextBoxsalary?

You only instantiated TextBox, but its not created yet.

Textbox is in the edit template of the gridview

<EditItemTemplate>
                     <asp:Label ID="Labelfname" runat="server" Text='<%#Eval("Fname") %>'></asp:Label>
                        &nbsp;
                        <asp:Label ID="Labelsurname" runat="server" Text='<%#Eval("Surname") %>'></asp:Label>
                        &nbsp; appointed&nbsp;
                        <asp:Label ID="Labelappoint" runat="server" 
                            Text='<%# Eval("Appointmentdate") %>'></asp:Label>
                        <br />
                        change salary&nbsp;
                        <asp:TextBox ID="TextBoxsalary" runat="server" Text='<%#Bind("Salary") %>'></asp:TextBox>
                        &nbsp;<asp:LinkButton ID="LinkButtonupdate" runat="server" CommandName="Update">Update</asp:LinkButton>
                        &nbsp;
                        <asp:LinkButton ID="LinkButtoncancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                    </EditItemTemplate>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.