Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#4.0
when i am trying to update Employee detail i got
Object reference not set to an instance of an object.
this error
i am trying update detail in gridview.
Posted 12-Jul-12 0:57am
Rahul Rajat Singh at 12-Jul-12 7:00am
Show us the stack trace. It comes in the catch block.
Gaurangraval at 12-Jul-12 7:05am
html code----
<asp:GridView ID="dtgGetEmployList" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
DataKeyNames="EmpID" AutoGenerateEditButton="True" AutoGenerateColumns="False"
OnRowEditing="EditRecord" OnRowUpdating="UpdateRecord" OnRowCancelingEdit="CancelRecord" OnRowDeleting="DeleteRecord" AllowPaging="True" AllowSorting="true" PageSize="5" OnPageIndexChanging="ChangePage" >


protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
int EmpID = Int32.Parse(dtgGetEmployList.DataKeys[e.RowIndex].Value.ToString());
int intResult = 0;
GridViewRow row = dtgGetEmployList.Rows[e.RowIndex];

TextBox tEmpID = (TextBox)row.FindControl("txtEmpID");
TextBox tFN = (TextBox)row.FindControl("txtFirstName");
TextBox tLN = (TextBox)row.FindControl("txtLastName");
TextBox tEmail = (TextBox)row.FindControl("txtEmail");
DropDownList tDepartment = (DropDownList)row.FindControl("ddlDepartment");
DropDownList tEmployee = (DropDownList)row.FindControl("ddlEmployee");
// DropDownList
// instantiate BAL
EmployeeBAL pBAL = new EmployeeBAL();
Employee employee = new Employee();
employee.EmpID = Int32.Parse(tEmpID.Text);
employee.FirstName = tFN.Text;
employee.LastName = tLN.Text;
employee.EmailID = tEmail.Text;
employee.DeptID = Int32.Parse(tDepartment.Text);
employee.ReportionPersonID = Int32.Parse(tEmployee.Text);
intResult = pBAL.Update(employee);
if (intResult > 0)
lblMessage.Text = "Record Updated Successfully.";
lblMessage.Text = "Record couldn't updated";
catch (Exception ee)
lblMessage.Text = ee.Message.ToString();
employee = null;
pBAL = null;
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This error is caused because you are trying to access a property of an object that is null.
Whenever you get this sort of error, you can try to step through and debug your source code.
This will help you get to the line that is throwing the error.
You can take a look at which object is null and then fix this error by figuring out why this object is null in your code.
Member 10048130 at 14-Jun-13 1:42am
Line 47: {
Line 48: //check for derived output value with no value assigned
Line 49: if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
object refrence not set to an instence please help me
Line 50: {
Line 51: p.Value = DBNull.Value;

Source File: g:\backup\hotel1\App_Code\Dl\SQLHelper.cs Line: 49
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

when i have use this code in my GridView so now i can update employee detail...and Object reference not set to an instance of an object solved this error
 Collapse | Copy Code
<asp:TemplateField HeaderText="Emp  ID" >
                                           <%# Eval("EmpID")%>
                                           <asp:TextBox ID="txtEmpID" runat="Server" Text='<%# Eval("EmpID") %>'></asp:TextBox>

Gaurang R.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,631
1 Sergey Alexandrovich Kryukov 6,345
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,522

Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 12 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100