ACostea
ACostea

Reputation: 141

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!

Picture showing how my gridview currently renders when I run my app

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

}

Upvotes: 0

Views: 116

Answers (1)

Johan Filipsson
Johan Filipsson

Reputation: 11

Your delete statement should look like "delete from children" without specyfing any fields.

Upvotes: 1

Related Questions