System.NullReferenceException:未将对象引用设置为对象的实例:

时间:2011-12-27 05:11:30

标签: asp.net nullreferenceexception

我是asp.net的新手,并且在asp.net.i中处理数据网格视图已编写以下代码用于在网格视图中插入和更新列。网格显示数据非常好,但是当我我试图编辑网格中的任何行然后它抛出异常:

  

System.NullReferenceException:对象引用未设置为对象的实例:在以下行中:

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
}

2 个答案:

答案 0 :(得分:2)

您是否已全局声明或为变量指定了空值..?

喜欢这个string UpdateQuery="";

检查rate1.Text,company_id.Text,paper_id.Text属性是否获取值。

调试函数BindGridData(string Query)我认为发生的错误。检查该功能的参数。一些参数获得null值,这就是Exception即将发生的事情。看看吧。

希望这可能有帮助......

答案 1 :(得分:2)

您是否已全局声明或为变量指定了空值..?

喜欢这个字符串UpdateQuery ="&#34 ;;

检查rate1.Text,company_id.Text,paper_id.Text属性是否获取值。

调试函数BindGridData(字符串查询)我认为发生的错误。检查该功能的参数。一些参数获得空值,这是异常的来源。看看吧。