简体   繁体   English

我无法在gridview中进行行删除以删除数据库条目

[英]I can't get rowdeleting in gridview to delete database entry

I'm currently in the process of creating an asp.net web app in C# using Visual Studio. 我目前正在使用Visual Studio在C#中创建asp.net Web应用程序。 I have been stuck on the same problem for a while now and have tried so many solutions. 我已经在同一个问题上停留了一段时间,并尝试了许多解决方案。 I am required to have a page that upon loading, displays some of the information stored in a database table (first name, dob & username (PK) for children that have registered on a previous page. 我需要有一个页面,该页面在加载时会显示存储在数据库表中的某些信息(第一个孩子的名字,dob和用户名(PK))已在上一页中注册。

I have no problem populating the grid view with this data, but I am required to have delete button/links next to each child, that delete the child completely from the database table. 我用此数据填充网格视图没有问题,但是我需要在每个子项旁边都具有删除按钮/链接,以便从数据库表中完全删除该子项。 This is what I can not get to work. 这是我无法上班的。 My code was a complete mess, full of commented out sections where I was trying new things. 我的代码完全混乱,我尝试新事物时充满了注释掉的部分。 As of right now, I have scaled it back to a simple section of code, and I am getting an error when clicking delete next to any of the children in gridview. 截至目前,我已经将其缩放到简单的代码部分,并且在gridview中单击任何子项旁边的“删除”时遇到错误。

Is it really this difficult to enable the delete function in gridview? 在gridview中启用删除功能真的难吗? I have supplied screenshots, along with my code. 我提供了屏幕截图以及代码。 If anyone can point out where i'm going wrong, I would be ever so grateful! 如果有人能指出我要去哪里,我将非常感激! Thanks in advance. 提前致谢。

删除时我遇到的错误的屏幕截图

.CS CODE: .CS代码:

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 viewandremove : 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)
    {

    }      
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }

    protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {          
        using (SqlConnection connect = new SqlConnection("Data Source=THEBEAST;Initial Catalog=newregDB;Integrated Security=True;Pooling=False"))
        using (SqlCommand cmd = new SqlCommand("DELETE FROM children where childID=", 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();
        }
    }
}

} }

SOURCE CODE: 源代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="viewandremove.aspx.cs" Inherits="Coursework.viewandremove" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<p>
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDeleting="Gridview1_RowDeleting" DataKeyNames="childID" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
    </asp:GridView>
</p>
<p>
</p>
<p>
</p>

You can achieve the delete functionality without coding and only by using the designer. 您可以仅通过使用设计器来实现删除功能,而无需进行编码。 So add an SqlDataSource for your GridView and in its configuration window click on Advance and then check the checkbox of Generate INSERT, UPDATE, and DELETE statements . 因此,为您的GridView添加一个SqlDataSource ,并在其配置窗口中单击Advance,然后选中Generate INSERT,UPDATE和DELETE语句的复选框。 That is all. 就这些。 Later, you can modify the code generated. 以后,您可以修改生成的代码。

Your cs code and image are different from each other. 您的cs代码和图像彼此不同。 if you use delete command you should change to 'delete from xxx where yyy' format. 如果使用delete命令,则应更改为“从xxx yyy处删除”格式。 but your select command seems ok. 但是您的选择命令似乎还可以。

when you use delete you must not use columns name. 当使用删除时,一定不能使用列名。 it is wrong. 这是错误的。 the correct format is : delete from "tablename" where .... 正确的格式是:从“表名”中删除...。

Clear Bookmarks before 之前清除书签

 string ChildID=GridView1.DataKeys[e.RowIndex].Value.ToString();

and update request 和更新请求

DELETE FROM [children] where [ChildID]='"+ChildID+"'

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

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