错误:对象引用未设置为对象的实例

时间:2011-11-27 10:10:18

标签: c# asp.net

我使用了2个gridview,我使用了这个代码:

protected void SAVE_bt_Click(object sender, EventArgs e)
{
    if (Hidden_txt.Text == Convert.ToString(1))
    {

        for (int i = 0; i < FirstDD_gv.Rows.Count; i++)
        {
            GridViewRow row = FirstDD_gv.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

            if (isChecked)
            {

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = new SqlConnection(Class1.CnnStr);
                cmd.CommandText = "insert into table values(@Po_Number,@Line_No,@DelDate,@First_Sec)";
                cmd.Connection.Open();
                cmd.Parameters.AddWithValue("@Po_Number", PoNumber_lbl.Text);
                cmd.Parameters.AddWithValue("@Line_No", FirstDD_gv.Rows[i].Cells[1].Text);
                cmd.Parameters.AddWithValue("@DelDate", DeliveryDate_txt.Text);
                cmd.Parameters.AddWithValue("@First_Sec", Hidden_txt.Text);
                cmd.ExecuteNonQuery();

            }
        }
    }
    else
    {

        for (int i = 0; i < SecondDD_gv.Rows.Count; i++)
        {
            GridViewRow row = SecondDD_gv.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

            if (isChecked)
            {

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = new SqlConnection(Class1.CnnStr);
                cmd.CommandText = "insert into table values(@Po_Number,@Line_No,@DelDate,@First_Sec)";
                cmd.Connection.Open();
                cmd.Parameters.AddWithValue("@Po_Number", PoNumber_lbl.Text);
                cmd.Parameters.AddWithValue("@Line_No", SecondDD_gv.Rows[i].Cells[1].Text);
                cmd.Parameters.AddWithValue("@DelDate", DeliveryDate_txt.Text);
                cmd.Parameters.AddWithValue("@First_Sec", Hidden_txt.Text);
                cmd.ExecuteNonQuery();

            }
        }
    }


}

取决于我的hidden_​​txt.text,它只显示其中一个 当我使用第一个gridview时,我没有任何问题,但是当我使用第二个网格视图时,它向我显示了这个错误:

Object reference not set to an instance of an object

我该怎么办?

1 个答案:

答案 0 :(得分:0)

疯狂猜测 - 我怀疑你的第二个gridview没有声明“chkSelect”控件。