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