Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I am getting the Following Error


"Object reference not set to an instance of an object."


in Insert Command In grid



How to rectify it..
Posted
Comments
Sergey Alexandrovich Kryukov 15-Oct-12 2:37am    
It's not nice to ask such questions without showing any code.
--SA

A simple use of Visual studio DEBUGGER can tell you the cause. Just look at the stack trace and put a debugger on that line where you're getting this error.

This will easily help you identify and solve such type of errors.

Refer Microsoft Support:
"Object reference not set to an instance of an object"[^]
"Object reference not set to an instance of an object" [^]

And a search on Microsoft support[^]
 
Share this answer
 
v2
use break point to debug then u can know which value is passing in the variable
 
Share this answer
 
Please use the breakpoint and debug the code which tell you that from which line you get the error. might be useful in this senario and we will let you know that what exact issue was occured. remember freind this is genric error.

thanks,
Ambesha
 
Share this answer
 
please check the variable value i thinks its not getting the proper value
 
Share this answer
 
Comments
zaid Qureshi 15-Oct-12 2:38am    
The Following is the Code...



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Drawing;
using System.Data;


public partial class _Default : System.Web.UI.Page
{

SqlConnection con = new SqlConnection(@"Data Source=RMM-PC-343\SQLEXPRESS1;Initial Catalog=Employee_Details;Integrated Security=True");
private int result;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
}
}

private void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Employees_Details", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvDetails.DataSource = ds;
gvDetails.DataBind();
int coloumcount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = coloumcount;
gvDetails.Rows[0].Cells[0].Text = "No Record Found";


}
}

protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{

}
protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Add New"))

{
TextBox txtUsrName = (TextBox)gvDetails.FooterRow.FindControl("txtUsrName");
TextBox txtCity = (TextBox)gvDetails.FooterRow.FindControl("txtCity");
TextBox txtdesignation = (TextBox)gvDetails.FooterRow.FindControl("txtdesignation");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Employees_Details(UserName,City,Designation) values('"+txtUsrName.Text+"','"+txtCity.Text+"','"+txtdesignation.Text+"')",con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
LblResult.ForeColor = Color.Green;
LblResult.Text = txtUsrName.Text + "Details Inserted Successfully";

}

else
{

LblResult.ForeColor = Color.Red;
LblResult.Text = txtUsrName.Text + "Details Not Inserted";

}


}
}
protected void gvDetails_RowDeleted(object sender, GridViewDeletedEventArgs e)
{

}
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
string UserName = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("Delete From Employees_Details where UserId=+UserId", con);
con.Close();
if (result == 1)
{
BindEmployeeDetails();
LblResult.ForeColor = Color.Red;
LblResult.Text = UserName + "Details Deleted Successfully";


}

}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
BindEmployeeDetails();

}
protected void gvDetails_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{

}
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
string UserName = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
TextBox txtcity = (TextBox)gvDet
AshishChaudha 15-Oct-12 2:57am    
at which line you are getting the error?? have you debugged your code??
itskvini 15-Oct-12 3:06am    
In gvDetails_RowCommand, may be you are getting any one of these text boxes txtUsrName, txtCity, txtdesignation as null.
Please check.
You are trying to dereference some reference object by using some of its instance members (some member which is not a static), but the object is actually null. To resolve the problem, you either need to make sure than the object is always initialized (to some object initially created with the constructor), or just check the member/variable for null and, in case this is null, do something else.

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900