简体   繁体   中英

RowDeleting not working, no errors, but not deleting the fields in database table

I am attempting to create an asp.net web app using c# in Visual Studio. I have a page that required some fields from a database table to be displayed when opening. In this case, the first name, dob and username for children that have registered previously. I placed a gridview control on my page, wrote some code to connect to my database upon loading the page, and populating the gridview with the information mentioned above.

That all works fine, my problem is that I enabled the delete button on the gridview, as I am tasked with making it so that you are able to completely delete the children from the database using the delete buttons. With my current code, this doesn't happen. I have tried various long-winded pieces of code but I end up running in to multiple errors so have started again, and am trying to keep it simple. I have pasted my code below, could someone point out why the delete buttons aren't deleting the children from the database? Thanks in advance!

该图显示了我运行应用程序时gridview当前的呈现方式

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();
    }
}

}

您的删除语句应看起来像“从孩子中删除”,而无需指定任何字段。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM