Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am Getting The Following Error on my Code..

Object reference not set to an instance of an object.
My Code is :
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)
    {
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();
    }
    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtUserName = (TextBox)gvDetails.FooterRow.FindControl("txtUserName");
            TextBox txtCity = (TextBox)gvDetails.FooterRow.FindControl("txtCity");
            TextBox txtdesignation = (TextBox)gvDetails.FooterRow.FindControl("txtdesignation");
            con.Open();
            if (txtCity.Text == "" || txtdesignation.Text == "" || txtUserName.Text == "")
            {
   // Response.Write(@"<Script language='JavaScript'> alert('Enter The Details Properly');void ('');</script">);

                string textForMessage = @"<script language='javascript'> alert('Enter The Complete Details');</script>";

    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UserPopup", textForMessage);
            }

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

                }

                else
                {

                    LblResult.ForeColor = Color.Red;
                    LblResult.Text = txtUserName.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)gvDetails.Rows[e.RowIndex].FindControl("txtcity");
        TextBox txtDesignation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDesigantion");
        con.Open();
        SqlCommand cmd = new SqlCommand("update Employees_Details set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where UserId=" + UserId, con);
        cmd.ExecuteNonQuery();
        con.Close();
        LblResult.ForeColor = Color.Green;
        LblResult.Text = UserName + "Details Updated Successfully";
        gvDetails.EditIndex = -1;
        BindEmployeeDetails();
    }
    protected void gvDetails_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }
    protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvDetails_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }
}



Please help me With the Same...
Posted
Updated 16-Oct-12 1:58am
v2
Comments
Lakshmimsridhar 16-Oct-12 7:50am    
hi,
where you are getting this error?
zaid Qureshi 16-Oct-12 8:03am    
on update Statement
bbirajdar 16-Oct-12 8:03am    
line number ????
zaid Qureshi 16-Oct-12 8:05am    
line no 144
zaid Qureshi 16-Oct-12 8:11am    
did u rectify the Error..

1.Put a brake point
2.keep on "Steping over/F10 " then try to see if your text boxes, username and userId have values or are not empty.

Because one of the values you passing to the SqlCommand is empty.
So find which one is...
 
Share this answer
 
As par my knowledge error is in following line :

int UserId = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());


so the problem is of Datakey.
Try to pass proper DataKey.
 
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