1
我正在使用Visual Studio在c#中创建一个asp.net web应用程序的过程。我的一个页面需要显示数据库中表格的信息,并提供删除任何条目的选项。当在这里问一个不同的问题时,我触及了这个问题,并很好地给了一篇好文章来看看如何删除数据库表条目。使用gridview删除数据库条目
我现在正在跟随上述文章,尽我所能复制作者的结果,但我收到了不同的结果,我在想如果有人能指出我出错的地方。我也尝试过各种其他的方式来做这件事,但最终总会碰到错误。我提供了文章I a的链接,用于指导,我的应用程序与文章相比如何,以及单击任何删除按钮时出现的错误的屏幕截图。也是我的代码,显然:)。提前致谢。
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 " +
"[email protected];" +
"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 [email protected],[email protected] " +
"where [email protected];" +
"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)
{
}
}
}
源代码:
<%@ 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>
嗨,我检查了我的源代码,但没有任何描述的代码要删除。我在原始问题中包含了源代码以向您展示。谢谢您的帮助! – ACostea
在您的Visual Studio上,转到源代码,键入关键字“RowDeleting”以查看是否会突出显示类似内容。 – Auguste
搜索RowDeleting不会在我的源代码中返回任何内容。我在我的原始问题中提供了我的源代码,您可以看到RowDeleting没有任何内容。我很困惑,为什么这个错误是由于RowDeleting事件没有得到处理,当我的源代码中没有任何明确的相关信息时。是否有其他地方可以找到RowDeleting代码? – ACostea