1

在我使用 asp.net 的 Web 应用程序中,我有一个带有编辑按钮的 gridview。此网格视图是通过连接两个表创建的。当我单击那些编辑按钮时,我需要更新这两个表。我的网格视图包含 3 个字段 - 名称、nric 和状态。其中表 1 中的名称和 nric 以及表 2 中的状态。更新两个表的查询将如何?请帮忙

 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
   GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

    TextBox id = (TextBox)row.FindControl("s_id");
    TextBox name = (TextBox)row.FindControl("s_name");
    TextBox nric = (TextBox)row.FindControl("s_nric");
    TextBox status = (TextBox)row.FindControl("s_status");
    SqlConnection con = obj.getcon(); 
    con.Open();
    SqlCommand cmd = new SqlCommand("update e.student_details ,f.student_vs_testsession_details  set e.student_id='" + id.Text+ "',e.student_name='" + name.Text + "',e.student_nric='" + nric.Text + "',f.testsession_status='" + status.Text + "' where e.student_id=f.student_id", con);
    cmd.ExecuteNonQuery();
    con.Close();


}
4

3 回答 3

0

@Divya Hari:您可以通过存储过程来完成此任务。

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
    GridViewRow row = GridView1.Rows[e.RowIndex];

    TextBox txtName = (TextBox)row.FindControl("txtName");
    TextBox txtstatus = (TextBox)row.FindControl("txtstatus");

    //     From here you can fire Update query on database.
    // Here you write code for store procedure.


 }

存储过程。

 in SP you need to write update query for both table. Something like this.


 SET ANSI_NULLS ON
 GO
 SET QUOTED_IDENTIFIER ON
 GO
 CREATE PROCEDURE UpdateBothTable 
    @ID int,
    @Name varchar(50),
    @Status varchar(10)
 AS
 BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

     -- Insert statements for procedure here
    Update table1 set Name=@Name where ID=@ID

    Update Table2 set Status=@Status where ID=@ID


 END
 GO

希望对你有帮助....:)

于 2013-04-16T05:31:07.447 回答
0

您的联接查询将与原来相同。[根据您未指定的数据库表结构。] 对于 GridView 中的编辑操作有以下内容:

您必须处理 3 个事件:

RowEditing, RowCancelingEdit,RowUpdating

以下是代码:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
   GridView1.EditIndex = e.NewEditIndex;

}


protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
   e.Cancel = true;
   GridView1.EditIndex = -1;

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   GridViewRow row = GridView1.Rows[e.RowIndex];

   TextBox txtProductName = (TextBox)row.FindControl("txtProductName");
   TextBox txtUnitPrice = (TextBox)row.FindControl("txtUnitPrice");

   //From here you can fire Update query on database.

}

参考链接:

http://www.ezzylearning.com/tutorial.aspx?tid=1993146

希望它有帮助。

于 2013-04-16T04:37:51.640 回答
0

Divya,这是可能的。当您单击更新按钮时,您的网格变量将更新的数据传递到数据库,但在这里您需要触发不同的更新语句。如果您有进一步的疑问,请告诉我......试试这个......!

于 2013-04-16T04:49:03.803 回答