0

一个数据列表更新命令可以对两个数据列表进行更新吗?

我有“datalist1”,其中嵌套了“datalist2”。

我在 datalist1 上有一个“更新”命令按钮(它只更新 datalist1 中的数据)。

我在 datalist2 上有一个单独的“更新”命令按钮(它只更新 datalist2 中的数据)。

是否可以使用 datalist1“更新”命令按钮来更新 datalist1 和 datalist2?不需要两个单独的更新按钮,也不需要接触表格和标准程序?

我有:

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
    HiddenField45.Value = "No";

    Label61.Text = HiddenField19.Value;
    Label91.Text = HiddenField20.Value;

    if (Label91.Text == "")
    {
        Label91.Text = "a1 Like '' OR ";
        GridView1.Columns[10].Visible = false;
    }
    else
    {

    }

    Label101.Text = Label91.Text.Remove(Label91.Text.Length - 4);


    //datalist1 attributes
    String a1 = ((Label)e.Item.FindControl("a1Label")).Text;
    String b1 = ((Label)e.Item.FindControl("b1Label")).Text;
    String c1 = ((DropDownList)e.Item.FindControl("c1DropDownList")).Text;
    String d1 = ((TextBox)e.Item.FindControl("d1TextBox")).Text;


    //datalist2 attributes
    String a2 = ((Label)e.Item.FindControl("a2Label")).Text;
    String b2 = ((Label)e.Item.FindControl("b2Label")).Text;
    String c2 = ((DropDownList)e.Item.FindControl("c2DropDownList")).Text;
    String d2 = ((TextBox)e.Item.FindControl("d2TextBox")).Text;




    string str = "UPDATE table1 SET a1=@a1, b1=@b1, c1=@c1, d1=@d1 WHERE a1=@a1 AND b1=@b1";

    using (MySqlConnection con = new MySqlConnection("Server=mysql.xxxxxxxx;Port=xxxx; Database=xxxxxxx; User=xxxxxxx; Password=xxxxxxx;"))
    {
        using (MySqlCommand cmd = new MySqlCommand(str, con))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@a1", a1);
            cmd.Parameters.AddWithValue("@b1", b1);
            cmd.Parameters.AddWithValue("@c1", c1);
            cmd.Parameters.AddWithValue("@d1", d1);
            con.Open();
            cmd.ExecuteNonQuery();

        }
    }


//datalist2 update()

    string str2 = "UPDATE table2 SET a2=@a2, b2=@b2, c2=@c2, d2=@d2 WHERE a2=@a2 AND b2=@b2";

    using (MySqlConnection con2 = new MySqlConnection("Server=mysql.xxxxxxx;Port=xxxx; Database=xxxxxxx; User=xxxx; Password=xxxxxx;"))
    {
        using (MySqlCommand cmd2 = new MySqlCommand(str2, con2))
        {
            cmd2.CommandType = CommandType.Text;
            cmd2.Parameters.AddWithValue("@a2", a2);
            cmd2.Parameters.AddWithValue("@b2", b2);
            cmd2.Parameters.AddWithValue("@c2", c2);
            cmd2.Parameters.AddWithValue("@d2", d2);
            con2.Open();
            cmd2.ExecuteNonQuery();

        }



    }



    for (int i = 0; i < GridView3.Rows.Count; i++)
    {
        GridView grid = (GridView)GridView3.Rows[i].Cells[21].FindControl("GridView8");
        grid.DataBind();
    } 
}

但是我收到错误“System.NullReferenceException:对象引用未设置为对象的实例”

对于 datalist2 中的属性

谢谢

4

1 回答 1

0

DataListCommandEventArgs e,这里e指向datalist1。因此它无法获取 datalist2 的引用。由于这个原因,你得到了 nullreference 异常。

尝试如下...我希望,这会奏效

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
HiddenField45.Value = "No";

Label61.Text = HiddenField19.Value;
Label91.Text = HiddenField20.Value;

if (Label91.Text == "")
{
    Label91.Text = "a1 Like '' OR ";
    GridView1.Columns[10].Visible = false;
}
else
{

}

Label101.Text = Label91.Text.Remove(Label91.Text.Length - 4);


//datalist1 attributes
String a1 = ((Label)e.Item.FindControl("a1Label")).Text;
String b1 = ((Label)e.Item.FindControl("b1Label")).Text;
String c1 = ((DropDownList)e.Item.FindControl("c1DropDownList")).Text;
String d1 = ((TextBox)e.Item.FindControl("d1TextBox")).Text;


//datalist2 attributes
 String a2 = ((Label)DataList2.Items[0].FindControl("a2Label")).Text; 
 String b2 = ((Label)DataList2.Items[1].FindControl("b2Label")).Text;
 String c2 = ((DropDownList)DataList2.Items[2].FindControl("c2DropDownList")).Text;
 String d2 = ((TextBox)DataList2.Items[3].FindControl("d2TextBox")).Text;
//here [0],[1],[2],[3] denote row numbers.



string str = "UPDATE table1 SET a1=@a1, b1=@b1, c1=@c1, d1=@d1 WHERE a1=@a1 AND b1=@b1";

using (MySqlConnection con = new MySqlConnection("Server=mysql.xxxxxxxx;Port=xxxx; Database=xxxxxxx; User=xxxxxxx; Password=xxxxxxx;"))
{
    using (MySqlCommand cmd = new MySqlCommand(str, con))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@a1", a1);
        cmd.Parameters.AddWithValue("@b1", b1);
        cmd.Parameters.AddWithValue("@c1", c1);
        cmd.Parameters.AddWithValue("@d1", d1);
        con.Open();
        cmd.ExecuteNonQuery();

    }
}


//datalist2 update()

string str2 = "UPDATE table2 SET a2=@a2, b2=@b2, c2=@c2, d2=@d2 WHERE a2=@a2 AND b2=@b2";

using (MySqlConnection con2 = new MySqlConnection("Server=mysql.xxxxxxx;Port=xxxx; Database=xxxxxxx; User=xxxx; Password=xxxxxx;"))
{
    using (MySqlCommand cmd2 = new MySqlCommand(str2, con2))
    {
        cmd2.CommandType = CommandType.Text;
        cmd2.Parameters.AddWithValue("@a2", a2);
        cmd2.Parameters.AddWithValue("@b2", b2);
        cmd2.Parameters.AddWithValue("@c2", c2);
        cmd2.Parameters.AddWithValue("@d2", d2);
        con2.Open();
        cmd2.ExecuteNonQuery();

    }



}



for (int i = 0; i < GridView3.Rows.Count; i++)
{
    GridView grid = (GridView)GridView3.Rows[i].Cells[21].FindControl("GridView8");
    grid.DataBind();
} 
}

在这里,DataList2 属性是使用 DataList 的行数访问的。

于 2013-10-23T09:47:39.450 回答