2011-12-27 159 views
2

我是asp.net中的新成员,在asp.net.i中处理数据网格视图时编写了下列代码,用于插入和更新列中的列电网view.the网格显示的数据非常好,但是当我试图在网格中编辑任何行则抛出一个异常:System.NullReferenceException:未将对象引用设置为对象的实例:

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 
} 
+0

您是否全局声明或赋予变量空值?像这样'字符串UpdateQuery =“”;' – RajeshKdev 2011-12-27 05:23:11

+0

可能重复[什么是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – 2011-12-27 05:34:44

+0

另外,为什么要在String.Format中使用字符串连接?使用'string.Format('UPDATE tbl_PaperRateList set rate = {0} where companyId = {1} and paperId = {2}“,rate1.Text,company_id.Text,paper_id.Text);' – 2011-12-27 05:37:08

回答

3

你是全局声明还是赋值给变量..?

这样string UpdateQuery="";

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

调试函数BindGridData(string Query)发生在我认为的错误。检查该函数的参数。一些参数获得了空值,这就是Exception的来临。一探究竟。

但愿这很有帮助....

+0

谢谢先生,我要调试函数BindGridData(字符串查询),我发现rate1.Text获取值,但company_id.Text,paper_id.Text获取空值。 – 2011-12-27 05:36:52

+0

k在这里试着解决这个问题。如果它发生了,请为每个行添加Null异常。它对你有用。 – RajeshKdev 2011-12-27 05:42:32

+0

非常感谢先生,我解决了这些问题。 我想加入你在我的facebuk账户[email protected] – 2011-12-27 07:17:20

2

难道ü全局声明或赋值为null值的变量..?

像这样的字符串UpdateQuery =“”;

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

调试函数BindGridData(字符串查询)在那里发生的错误我认为。检查该函数的参数。一些参数获得了空值,这就是Exception的来临。一探究竟。

相关问题