簡體   English   中英

ASP.NET從gridview獲得價值

[英]ASP.NET Getting value from gridview

這是我的代碼

protected void GridView1_DeletingRow(object sender, EventArgs e)
    {
        Functions con = new Functions();
        SqlConnection con1 = con.get();
        con1.Open();
        TextBox1.Visible = true;
        Button1.Visible = true;
        string z = TextBox1.Text;
        GridView1.EnableViewState = true;
        string name = GridView1.SelectedRow.Cells[1].Text;
        SqlCommand com3 = new SqlCommand("select id from products where product 
        = '" + name + "' ", con1);
        SqlDataReader read1 = com3.ExecuteReader();
        read1.Read();
        string pro = read1["id"].ToString();
        SqlCommand com = new SqlCommand("UPDATE CartItems SET quantity = '" + z 
        + "' where productid = '" + pro + "' ", con1);
    }

錯誤:

Line 92:  string name = GridView1.SelectedRow.Cells[1].Text;

異常詳細信息:System.NullReferenceException:對象引用未設置為對象的實例。

到底是什么錯誤? ,我該如何解決?

您應該將EventArgs參數更改為更特定的GridViewDeleteEventArgs類型,並使用該類型來查找要刪除的行:

protected void GridView1_DeletingRow(object sender, GridViewDeleteEventArgs e)
{
    // ...your code
    string name = GridView1.Rows[e.RowIndex].Cells[1].Text;
    // ...the rest of your code

NullReferenceException的最可能原因是因為在激發RowDeleting事件時未設置SelectedRow屬性。

注意:如果網格只有一列,“ Cells[1].Text ”也有可能拋出該異常。 您應該閱讀這篇文章中的建議, 建議可以幫助您調試NullReferenceExceptions。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM