2016-12-11 6 views
1

私はVisual Studioを使用してC#でasp.net Webアプリケーションを作成しています。私のページの1つは、データベースのテーブルの情報を表示し、エントリを削除するオプションを与える必要があります。ここで別の質問をするとき、私はこれに触れました。それを見て、データベーステーブルのエントリを削除する方法を示した良い記事がうまくいきました。gridviewを使用してデータベースエントリを削除する

私は著者の結果を複製することができるように、この記事に忠実に従っていますが、私は混在した結果を受け取り、私が間違っているところを誰かが指摘できるかどうか疑問に思っていました。私はこれをやっている他の様々な方法を試してみましたが、いつもエラーに遭遇してしまいます。私はガイダンスのために使用する記事へのリンク、アプリケーションと記事との比較、削除ボタンをクリックすると表示されるエラーのスクリーンショットを提供しました。私のコード、明らかに:)。前もって感謝します。

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 " + 
      "[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> 

答えて

0

このエラーメッセージが表示される最も一般的な理由は、GridView1に作成されていないイベントがあるためです。コードをコピーして貼り付けた場合は、GridView1に移動してそのイベントを削除します。 GridView1_RowDeletingのようになります。それを削除すると、問題は解決されます。これはソースビューではなく、コードの背後にあります。

+0

こんにちは、私のソースをチェックしましたが、削除するその説明のコードはありません。元の質問にソースコードを含めて紹介しました。助けてくれてありがとう! – ACostea

+0

Visual Studioで、ソースコードに移動し、キーワード「RowDeleting」を入力して、そのようなものが強調表示されるかどうかを確認します。 – Auguste

+0

RowDeletingを検索すると、ソースコードに何も返されません。元の質問にソースコードを提供していますが、RowDeletingに関する何もないことがわかります。私はそれに関連する私のソースには何も明白でないときに処理されていないRowDeletingイベントのためにエラーがなぜ発生するのか、とても混乱しています。 RowDeletingコードが見つかった場所がありますか? – ACostea

関連する問題