我試圖刪除網格視圖中的行。對於那個我在下面的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