You must Sign In to post a response.
  • Category: ASP.NET

    Object reference not set to an instance of an object

    Hello,
    I had a grid view with edit item template with drop down lists and textboxes. When I am clicking "Update" button , I am getting an error "Object reference not set to an instance of an object" pointing at "UpdateRow " method. When I tried to debug I am getting null values in textboxes and dropdownlist .


    Code behind:
    protected void grdSupList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    string id = grdSupList.DataKeys[e.RowIndex].Value.ToString();
    TextBox txt_SupId = (TextBox)grdSupList.FooterRow.FindControl("txtSupId");
    TextBox txt_SupName = (TextBox)grdSupList.FooterRow.FindControl("txtSupName");
    TextBox txt_City = (TextBox)grdSupList.FooterRow.FindControl("txtCity");
    TextBox txt_State = (TextBox)grdSupList.FooterRow.FindControl("txtState");
    TextBox txt_Country = (TextBox)grdSupList.FooterRow.FindControl("txtCountry");
    DropDownList txt_act = (DropDownList)grdSupList.FooterRow.FindControl("ddlActivity");
    DropDownList txt_loc = (DropDownList)grdSupList.FooterRow.FindControl("ddlLocal");

    int sqlid = Convert.ToInt32(id);
    UpdateRow(sqlid, txt_SupId.Text, txt_SupName.Text, txt_City.Text, txt_State.Text, txt_Country.Text, txt_act.SelectedItem.Text, txt_loc.SelectedItem.Text);
    grdSupList.EditIndex = -1;
    FillGrid();
    lblMsg.Text = "Record updated successfully!";
    }

    private void UpdateRow(int id, string SupId, string SupName, string city, string state, string country, string status, string location)
    {
    string connString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;

    int bitstatus = 0;
    if (status == "Yes")
    {
    bitstatus = 1;
    }
    else
    {
    bitstatus = 0;
    }

    string sql = String.Format("update tblTBT_Vendors set SupplierID ='" + SupId + "', SupplierName= '" + SupName + "', City='" + city + "', StateOrProvince='" + state + "', Country='" + country + "', Inactive = '" + bitstatus + "', LocalRegion='" + location + "' where ID='" + id + "' ");
    using (SqlConnection conn = new SqlConnection(connString))
    {
    conn.Open();
    using (SqlCommand cmd = new SqlCommand(sql, conn))
    {
    cmd.ExecuteNonQuery();
    }
    }
    }
  • #766169
    Hi

    Debug trace line by line in your code.

    The issue is Object reference not set to an instance of an object

    Reason
    ==========

    1.we need instance that object.
    2.your control name not correct
    3.your control name C# code did not recognize.

    Best trace step by step thats best way.

    when you post the issue . Please attached table script with 2 records temp data then only Reply members easily find out fixed in your issue.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766170
    Hi
    where is FillGrid method?

    Step1
    ===========
    1.Share your table script with Data

    Step2:
    ======
    2.Share Proper all code in your c#,aspx

    then only easily fixed in your issue

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766173
    Object reference error is thrown by compiler when there is NULL object you are trying to access it. Now as per your code, it looks textboxes and dropdownlist are null, first you need to use 'GridView1_RowUpdating' event
    see below sample, here I have access controls from gridviewRow and update them

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
    string tname = ((TextBox)row.Cells[0].Controls[0]).Text;
    string tques = ((TextBox)row.Cells[1].Controls[0]).Text;
    MySqlCommand cmd = new MySqlCommand("update exam set name1=@name,ques=@ques where id = @id", con);
    cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id;
    cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
    cmd.Parameters.Add("@ques", MySqlDbType.VarChar, 40).Value = tques.Text.Trim();
    con.Open();
    cmd.ExecuteNonQuery();
    GridView1.EditIndex = -1;
    bind();
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766177
    Hi,

    If you are trying to access null object and perform action against that null object that time you will get this type of error, for example you are trying to assign some control value, but control is not able to find that means control id is null you are trying to use that control value that time you will get this type of error, in the same way many places we will get this type of issue, but the reason behind this is accessing null object. As you said textbox and dropdown values are null, may be because of that you will get this error, I suggest you to recheck why it is null? then you will get the solution for this.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766179
    I am sure this issue is because some of the value coming as null values. It is trying to assing the null value into the typed value.

    By using the debugger you can find the exception line. If you find the exact line you can easily identify the issue value

    I am suspecting any one of the following value may be null
    int id, string SupId, string SupName, string city, string state, string country, string status, string location

    By Nathan
    Direction is important than speed

  • #766194
    Hi Everyone,

    Thanks for responding. It is my mistake , I want to update textboxes in rows but in code I written to update the textboxes in footer row , problem is solved.

    I will make sure to post my data table and entire code while posting a question next time.


Sign In to post your comments