1

I am in the process of creating an asp.net web application in c# using Visual Studio. One of my pages needs to display information from a table in my database, and to give the option to delete any of the entries. When asking a different question on here, I touched on this and was nicely given a good article to look at that shows how to delete database table entries.

I am now following said article as closely as I can to replicate the authors results but I am receiving mixed results and I was wondering if somebody could point out where I am going wrong. I have also tried various other ways of doing this but always end up running in to errors. I have provided links to the article I a using for guidance, how my application looks compared to the article, and a screenshot of the error I get when clicking on any of the delete buttons. Also my code, obviously :). Thanks in advance.

Link to the article I am following

How my gridview looks when I run my application

Error when clicking any of the delete button links

namespace Coursework
{
    public partial class View_remove_children : System.Web.UI.Page
    {
        private String strConnString = ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            string strQuery = "select firstname, dob, childID" +
                               " from children";
            SqlCommand cmd = new SqlCommand(strQuery);
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }

        public DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        protected void OnPaging(object sender, GridViewPageEventArgs e)
        {
            BindData();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
        protected void DeleteCustomer(object sender, EventArgs e)
        {
            LinkButton lnkRemove = (LinkButton)sender;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "delete from  children where " +
            "childID=@childID;" +
             "select firstname, dob, childID from children";
            cmd.Parameters.Add("@childID", SqlDbType.VarChar).Value = lnkRemove.CommandArgument;
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }
        protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
        {
            string firstname = ((Label)GridView1.Rows[e.RowIndex].FindControl("firstnameLbl")).Text;
            string dob = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("dobLbl")).Text;
            string childID = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("childIDlbl")).Text;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update children set childID=@childID,firstname=@firstname " +
             "where childID=@childID;" +
             "select firstname, dob, childID from children";
            cmd.Parameters.Add("@firstname", SqlDbType.VarChar).Value = firstname;
            cmd.Parameters.Add("@dob", SqlDbType.VarChar).Value = dob;
            cmd.Parameters.Add("@childID", SqlDbType.VarChar).Value = childID;
            GridView1.EditIndex = -1;
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }
    }
}

Source code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"     AutoEventWireup="true" CodeBehind="View_remove_children.aspx.cs"    Inherits="Coursework.View_remove_children" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"  runat="server">
<p>
    <br />
</p>
<p>
    <asp:GridView ID="GridView1" runat="server"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" Width="255px">
        <Columns>
            <asp:ButtonField CommandName="Delete" Text="Delete" />
        </Columns>
    </asp:GridView>
</p>
<p>
</p>

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ACostea
  • 141
  • 2
  • 18

1 Answers1

0

The most likely reason why you receive this error message is because on your GridView1 there's an event that is not created. If you copied and pasted the code, go to your GridView1 and delete that event. It should look like GridView1_RowDeleting. Remove that and your problem will be resolved. This should be found on the Source View, not code behind.

Auguste
  • 2,007
  • 2
  • 17
  • 25
  • Hi, I checked my source but there isn't any code of that description to delete. I included the source code in my original question to show you. Thanks for the help! – ACostea Dec 11 '16 at 17:18
  • On your Visual Studio, go to the source code, type the keyword "RowDeleting" to see if something like that will be highlighted. – Auguste Dec 11 '16 at 17:22
  • searching for RowDeleting returns nothing on my source code. I provided my source code in my original question and you can see there is nothing regarding RowDeleting. I'm so confused as to why the error is due to a RowDeleting event not being handled when there is clearly nothing in my source relating to it. Is there somewhere else that the RowDeleting code could be found? – ACostea Dec 11 '16 at 17:25
  • Go back to your source code and check to see if there's an event for RowDeleting. On the property, click on the icon that looks like a lightning ball. This is an example of what you would see if the RowDeleting even is used: – Auguste Dec 11 '16 at 17:27
  • Also, make sure you go to Source code, not code behind. The code you show above are CodeBehind, not source code. You should go to example.aspx, not example.aspx.cs – Auguste Dec 11 '16 at 17:29
  • When I click the lightning bolt, it takes me to a drop down menu next to 'SelectedIndexChanged' and the options in the drop down menu are as follows; SelectedIndexChanged1, Page_Load & DeleteCustomer. Again, nothing regarding RowDeleting! I feel as though i'm being extremely stupid and missing something simple?? – ACostea Dec 11 '16 at 17:32
  • Based on what I'm seeing, you don't miss anything. Try to add the RowDeleting event to see if that will clear the error message. – Auguste Dec 11 '16 at 17:34
  • I think I am showing both the .cs code and the source code. The source code is the html/asp stuff, correct? – ACostea Dec 11 '16 at 17:34
  • I see it now. I don't see any RowDeleting event on it. – Auguste Dec 11 '16 at 17:35
  • Thank you Auguste, I added the event and it removed the error when I click the 'delete' link button. My problem now is that nothing happens when I click delete, the entry is not deleted from my database table. Is there an error elsewhere in the code? Thanks! – ACostea Dec 11 '16 at 17:38
  • The event will fire, but it won't delete, because there's no code for that. For help with that, either open another question related to that or browse the web. – Auguste Dec 11 '16 at 17:41
  • I have no issue opening a new question/looking around on the web. But with the code I used from the article I provided, there is a whole Delete section, you can see it in the code I provided, under 'DeleteCustomer'. Is this not the code required to delete the entry from the database? It includes a delete sql command etc? – ACostea Dec 11 '16 at 17:46
  • Yes there is. What you can do, you can apply the logic that is under DeleteCustomer() on the Gridview1_RowDeleting event. – Auguste Dec 11 '16 at 17:50
  • Just before you replied, I tried that, moving the code from DeleteCustomer to the RowDeleting event. The issue now is that when I hit delete on one of the entries, I get the following error message: An exception of type 'System.InvalidCastException' occurred in Coursework.dll but was not handled in user code Additional information: Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'. – ACostea Dec 11 '16 at 17:56
  • Is it something to do with the button code in source? – ACostea Dec 11 '16 at 17:58
  • You don't have to use the first line of code on the instruction. – Auguste Dec 11 '16 at 18:30
  • http://stackoverflow.com/questions/7226548/how-to-delete-row-in-gridview-using-rowdeleting-event – Auguste Dec 11 '16 at 18:33
  • I commented out the first line, along with the cmd.Parameters.Add line (as it included the 'lnkRemove' code. But I then get a different error (An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Must declare the scalar variable "@childID".) Sorry if i'm driving you crazy, whilst waiting for your replies, I am watching/reading other stuff and trying different fixes but so far, none are working! – ACostea Dec 11 '16 at 18:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130353/discussion-between-acostea-and-auguste). – ACostea Dec 11 '16 at 19:12