[英]RowDeleting not working, no errors, but not deleting the fields in database table
我正在嘗試在Visual Studio中使用c#創建一個asp.net Web應用程序。 我有一個頁面,要求打開時要顯示數據庫表中的某些字段。 在這種情況下,以前注冊的孩子的名字,名字和用戶名。 我在頁面上放置了一個gridview控件,編寫了一些代碼以在加載頁面時連接到數據庫,並使用上述信息填充gridview。
一切正常,我的問題是我啟用了gridview上的delete按鈕,因為這樣做的任務是使您能夠使用Delete按鈕從數據庫中完全刪除子級。 用我當前的代碼,這不會發生。 我嘗試了各種冗長的代碼,但是最終遇到了多個錯誤,因此又重新開始了,並試圖保持簡單。 我在下面粘貼了我的代碼,有人可以指出為什么刪除按鈕沒有從數據庫中刪除子項嗎? 提前致謝!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
using System.Collections;
using System.Configuration;
namespace Coursework
{
public partial class view_remove : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection connect = new SqlConnection("Data Source=THEBEAST;Initial Catalog=newregDB;Integrated Security=True;Pooling=False"))
using (SqlCommand cmd = new SqlCommand("SELECT [firstname], [dob], [ChildID] FROM [children]", connect))
{
connect.Open();
SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
connect.Close();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void Gridview1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
SqlCommand cmd = new SqlCommand("Delete From children (firstname, dob, childID)");
GridView1.DataBind();
}
}
}
您的刪除語句應看起來像“從孩子中刪除”,而無需指定任何字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.