简体   繁体   English

RowDeleting不起作用,没有错误,但不删除数据库表中的字段

[英]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. 我正在尝试在Visual Studio中使用c#创建一个asp.net Web应用程序。 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. 我在页面上放置了一个gridview控件,编写了一些代码以在加载页面时连接到数据库,并使用上述信息填充gridview。

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. 一切正常,我的问题是我启用了gridview上的delete按钮,因为这样做的任务是使您能够使用Delete按钮从数据库中完全删除子级。 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();
    }
}

} }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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