Click here to Skip to main content
12,405,868 members (66,766 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
hello,

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
Comments
Rahul Rajat Singh 12-Jul-12 7:00am
   
Show us the stack trace. It comes in the catch block.
Gaurangraval 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" >


aspx.cs-----


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();
try
{
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.";
else
lblMessage.Text = "Record couldn't updated";
}
catch (Exception ee)
{
lblMessage.Text = ee.Message.ToString();
}
finally
{
employee = null;
pBAL = null;
}
Rate this: bad
 
good
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.
  Permalink  
Comments
Member 10048130 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
 
good
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" >
                                       <ItemTemplate>
                                           <%# Eval("EmpID")%>
                                       </ItemTemplate>
                                       <EditItemTemplate>
                                           <asp:TextBox ID="txtEmpID" runat="Server" Text='<%# Eval("EmpID") %>'></asp:TextBox>
                                       </EditItemTemplate>
                    </asp:TemplateField>
 

thanks,
Gaurang R.
  Permalink  
v2
Comments
SajboySajid 21-Feb-15 11:00am
   
Imports System.Data.SqlClient
Imports System.Data
Imports System.Text.RegularExpressions
Public Class AddUser

Public con As New SqlConnection
Public cmd As New SqlCommand
Public sdr As SqlDataReader
Public sda As SqlDataAdapter
Public ds As New DataSet
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim matchRegex As New Regex("^[a-zA-Z0-9_]{3,16}$")
Dim matches As MatchCollection = matchRegex.Matches(tbUsername.Text)
Try
If tbUsername.Text = "" Then
MsgBox("PLEASE ENTER USERNAME ", MsgBoxStyle.Information, "Login Message")
tbUsername.Focus()

ElseIf tbnewpwd.Text = "" And tbcnfmpwd.Text = "" Then
MsgBox("PLEASE ENTER PASSWORD AND CONFIRM PASSWORD FIELD", MsgBoxStyle.Information, "Login Message")
tbnewpwd.Focus()

ElseIf tbUsername.Text.Length < 3 Then
MsgBox("PLEASE ENTER USERNAME OF MORE THAN THREE CHARACTERS", MsgBoxStyle.Information, "Login Message")
tbUsername.Focus()

'ElseIf tbnewpwd.TextLength < 6 Then
' MsgBox("PLEASE ENTER PASSWORD ATLEAST OF 6 CHARACTER", MsgBoxStyle.Information, "Login Message")
' tbnewpwd.Text = ""
' tbcnfmpwd.Text = ""
' tbnewpwd.Focus()

ElseIf tbcnfmpwd.Text <> tbnewpwd.Text Then
MsgBox(" PASSWORD AND RE-ENTER PASSWORD DOES NOT MATCH", MsgBoxStyle.Information, "LOGIN MESSAGE")
tbnewpwd.Text = ""
tbcnfmpwd.Text = ""
tbnewpwd.Focus()

ElseIf matches.Count = 0 Then
MsgBox("USERNAME CAN NOT CONTAINS SPECIAL CHARACTER ", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Invalid Entry")
tbUsername.Focus()
tbUsername.Text = ""

ElseIf tbnewpwd.Text = tbcnfmpwd.Text Then
If Add_Login() Then
MsgBox("USER ADDED SUCESSFULLY", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "SUCCESSFUL")
tbUsername.Text = ""
tbcnfmpwd.Text = ""
tbnewpwd.Text = ""
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
DBConnection.CloseCon()
End Try
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
cbtypeofuser.SelectedIndex = -1
tbUsername.Clear()
tbnewpwd.Clear()
tbcnfmpwd.Clear()
cbtypeofuser.Focus()
End Sub

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Dim str As String
str = MsgBox("Do you really want to exit", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "Exit")
If str = vbYes Then
Me.Close()
Else
Me.Refresh()
End If
End Sub
Public Function Add_Login() As Boolean
Dim str As String
str = "INSERT INTO Login(Username,Password,User_Type) VALUES('" & tbUsername.Text & "','" & tbnewpwd.Text & "','" & cbtypeofuser.SelectedItem.ToString & "')"
DBConnection.OpenCon()
cmd = New SqlCommand(str, con)
sda.UpdateCommand = cmd
DBConnection.RetrieveDatabaseQuery(str)
DBConnection.CloseCon()
sda.Dispose()
cmd.Dispose()
Return True

DBConnection.CloseCon()
sda.Dispose()
ds.Dispose()
Return False



End Function

Private Sub AddUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

this is my code when i am trying to add the value an error shows "Object reference not set to instance of an object".
Please help me out

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 12 Jul 2012
Copyright © CodeProject, 1999-2016
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