我试图删除网格视图中的行。对于那个我在下面的rowdeleting事件中写代码。无法在网格视图中的rowdeleting事件中获取单元值
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string UserId;
SqlTransaction tran;
using (con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["EMASFBAConnectionString"].ConnectionString;
cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
tran = con.BeginTransaction();
TableCell cell = GridView1.Rows[e.RowIndex].Cells[1];
string username = cell.Text;
cmd.Transaction = tran;
cmd.CommandText = "Delete from aspnet_Users where UserName='" + username + "'";
UserId = cmd.ExecuteScalar().ToString();
if (UserId.Length!=0)
{
//delete user from membership table.
errorLabel.Text = "User is ready to delete";
}
tran.Commit();
con.Close();
}
}
当调试单元值即将为空。我在这里犯了什么错误? 此网格视图在每行前面都有编辑和删除按钮。 我尝试使用Cells[0],Cells[2]
,但只给出空值。任何人都可以给我解决方案吗?
你确定你已经编写了代码删除,如在“commandtext”你有写选择命令????? – 2013-04-24 06:31:51
多数民众赞成。我刚刚从我之前的块中复制了它的文本。但是这里的问题是细胞价值不会到来。它是作为空值而来的。现在查看已编辑的问题。 – Mihir 2013-04-24 06:34:37