2

I am new in asp.net and working on the data grid view in asp.net.i have written the following code for inserting and updating the columns in grid view.the grid is showing the data very well,but when i am trying to edit any row in the grid then it is throwing an Exception:

System.NullReferenceException: Object reference not set to an instance of an object: in the following line:

string UpdateQuery = string.Format("UPDATE tbl_PaperRateList set rate=" + rate1.Text + " where companyId=" + company_id.Text + " and paperId=" +paper_id.Text+ "");


SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ConnectionString);

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

protected void gridRateList_RowEditing(object sender, GridViewEditEventArgs e)
{
    gridRateList.EditIndex = e.NewEditIndex;
    BindGridData();
}

protected void gridRateList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    gridRateList.EditIndex = -1;
    BindGridData();
}
#endregion

#region  Binding the RateList Grid
public void BindGridData()
{

    {
        conn.Open();
        SqlCommand cmdCompanyId = new SqlCommand("Select max(companyId) from tbl_PaperRateList", conn);
        int c_id = Convert.ToInt32(cmdCompanyId.ExecuteScalar());

        using (SqlCommand comm = new SqlCommand("select p.paperId,"
           + "p.Rate,pm.PaperName from tbl_PaperRatelist p,tbl_papermaster pm where p.paperId=pm.paperId and p.companyId=" + c_id + "", conn))
        {
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            gridRateList.DataSource = ds;
            gridRateList.DataBind();
        }
    }
}
#endregion

#region    /*Updating the Row in Grid View*/
protected void gridRateList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    string s = gridRateList.DataKeys[e.RowIndex].Value.ToString();

    Label company_id = (Label)gridRateList.Rows[e.RowIndex].FindControl("CompanyId");
    Label paper_id = (Label)gridRateList.Rows[e.RowIndex].FindControl("paperId");
    TextBox rate1 = (TextBox)gridRateList.Rows[e.RowIndex].FindControl("txtRate");
    string UpdateQuery = string.Format("UPDATE tbl_PaperRateList set rate=" + rate1.Text + " where companyId=" + company_id.Text + " and paperId=" +paper_id.Text+ "");


    gridRateList.EditIndex = -1;
    BindGridData(UpdateQuery);

}
#endregion

#region    Bind the Gridview after updating the row
private void BindGridData(string Query)
{
    string connectionstring = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connectionstring))
    {
        conn.Open();
        SqlCommand cmdCompanyId = new SqlCommand("Select max(companyId) from tbl_PaperRateList", conn);
        int cid = Convert.ToInt32(cmdCompanyId.ExecuteScalar());
        using (SqlCommand comm = new SqlCommand(Query +
                ";select p.paperId,"
+ "p.Rate,pm.PaperName from tbl_PaperRatelist p,tbl_papermaster pm where p.paperId=pm.paperId and p.companyId=" + cid + "", conn))
        {
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            gridRateList.DataSource = ds;
            gridRateList.DataBind();
        }
    }
}
#endregion
}
3
  • Did u declared globally or assigned null value to the variable..? like this string UpdateQuery=""; Commented Dec 27, 2011 at 5:23
  • possible duplicate of What is a NullReferenceException in .NET? Commented Dec 27, 2011 at 5:34
  • Also, why use string concatenation with String.Format? Use string.Format("UPDATE tbl_PaperRateList set rate={0} where companyId={1} and paperId={2}", rate1.Text, company_id.Text, paper_id.Text); Commented Dec 27, 2011 at 5:37

2 Answers 2

2

Did u declared globally or assigned null value to the variable..?

like this string UpdateQuery="";

Check the rate1.Text,company_id.Text ,paper_id.Text properties getting values or not.

Debug the Function BindGridData(string Query) the error happening there i think. check the parameters of that function. some parameters getting null value that's what the Exception is coming. check it out.

Hope this may helpful....

Sign up to request clarification or add additional context in comments.

3 Comments

thanks sir,i hve debugged the function BindGridData(string Query) and i found that rate1.Text is getting the values but company_id.Text ,paper_id.Text are getting the null values.
k try to solve before here asking.Dubug each and every line for Null Exception if it is occured.It will be useful to you.
Thanks a lot sir i have solved the problems. i would like to join u in my facebuk account at [email protected]
2

Did u declared globally or assigned null value to the variable..?

like this string UpdateQuery="";

Check the rate1.Text,company_id.Text ,paper_id.Text properties getting values or not.

Debug the Function BindGridData(string Query) the error happening there i think. check the parameters of that function. some parameters getting null value that's what the Exception is coming. check it out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.