ACostea
ACostea

Reputation: 141

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. 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.

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.

Is it really this difficult to enable the delete function in 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.

Screenshot of error I get when hitting delete

.CS CODE:

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>

Upvotes: 0

Views: 517

Answers (4)

KreminT
KreminT

Reputation: 179

Clear Bookmarks before

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

and update request

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

Upvotes: 0

Mahdi
Mahdi

Reputation: 3349

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. That is all. Later, you can modify the code generated.

Upvotes: 1

atabrizi
atabrizi

Reputation: 918

when you use delete you must not use columns name. it is wrong. the correct format is : delete from "tablename" where ....

Upvotes: 0

tserdas
tserdas

Reputation: 67

Your cs code and image are different from each other. if you use delete command you should change to 'delete from xxx where yyy' format. but your select command seems ok.

Upvotes: 0

Related Questions