2011-12-27 185 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的來臨。一探究竟。

相關問題