2011-12-27 29 views
2

私はasp.netで新しく、asp.net.iのデータグリッドビューで作業しています。グリッドview.theグリッドは非常によくデータを示しているが、私は、グリッド内の任意の行を編集しようとしているとき、それは例外を投げている:System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません:

System.NullReferenceException:オブジェクト参照ANのインスタンスに設定されていません対象:次の行で:

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

変数がグローバルに宣言されたか、null値が割り当てられましたか?このような 'string UpdateQuery =" ";' – RajeshKdev

+0

の可能な複製[.NETのNullReferenceExceptionは何ですか?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) –

+0

また、なぜString.Formatで文字列連結を使用するのですか?使用して 'string.Format(" UPDATE tbl_PaperRateList設定された率= companyId = {1}とpaperId = {2} "、rate1.Text、company_id.Text、paper_id.Text); ' –

答えて

3

変数がグローバルに宣言されたか、null値が割り当てられましたか?このstring UpdateQuery="";

よう

はpaper_id.Textプロパティが値を取得するかどうか、rate1.Text、company_id.Textを確認してください。

ファンクションBindGridData(string Query)をデバッグするとエラーが発生すると思われます。その機能のパラメータを確認してください。いくつかのパラメータは、例外が来るものであるnull値を取得します。見てみな。

・ホープ、このかもしれ役に立つ....

+0

ありがとうございます私は関数BindGridData(文字列のクエリ)をデバッグし、私はrate1.Textが値を取得していることがわかったが、company_id.Text、paper_id.Textはnull値を取得しています。 –

+0

kここで前に解くようにしてください。発生した場合は、Null Exceptionのためにそれぞれの行をバグしてください。 – RajeshKdev

+0

ありがとう、私は問題を解決しました。 私はfacebukアカウントで[email protected] –

2

uは、グローバルに宣言または変数にNULL値を割り当てられましたか..?

この文字列は次のとおりです。UpdateQuery = "";

rate1.Text、company_id.Text、paper_id.Textプロパティが値を取得しているかどうかを確認します。

関数BindGridData(string Query)をデバッグするときにエラーが発生していると思われます。その機能のパラメータを確認してください。いくつかのパラメータは、例外が来るものであるnull値を取得します。見てみな。

関連する問題