我正在使用Visual Studio在c#中创建一个asp.net Web应用程序。我的一个页面需要显示数据库中表格的信息,并提供删除任何条目的选项。当在这里问一个不同的问题时,我触及了这个并且很好地给出了一篇很好的文章来展示如何删除数据库表条目。
我现在正在尽可能地跟踪上述文章来复制作者的结果,但我收到了不同的结果,我想知道是否有人可以指出我哪里出错了。我还尝试了其他各种方法,但最终总是遇到错误。我提供了指向我用于指导的文章的链接,我的应用程序与文章的比较,以及点击任何删除按钮时出现的错误的屏幕截图。我的代码,显然:)。提前谢谢。
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)
{
}
}
}
源代码:
<%@ 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>
答案 0 :(得分:0)
您收到此错误消息的最可能原因是因为您的GridView1上存在未创建的事件。如果您复制并粘贴了代码,请转到GridView1并删除该事件。它应该看起来像GridView1_RowDeleting。删除它,您的问题将得到解决。这应该在源视图上找到,而不是后面的代码。