【问题标题】:My delete function is not removing entries and is being ignored我的删除功能没有删除条目并且被忽略
【发布时间】:2018-11-20 22:24:12
【问题描述】:

我在让我的删除功能在我的 ASP.net (C#) Web 应用程序中工作时遇到问题,我真的不知道下一步该去哪里。

在单击按钮时调用该函数,但好像Page_Load 方法完全忽略了该命令。我是新手,希望能得到一些帮助。 谢谢。

这里是Page_loadDisplayData 方法、Count 方法和通过按钮单击调用的delete 方法。

public partial class WebForm1 : System.Web.UI.Page
{
    SqlConnection cn;
    static int count = 1;
    static int max = 2;
    static String sqlQuery = "Select * from Footballer";
    static bool firstTime = true;

    protected void Page_Load(object sender, EventArgs e)
    {
        string str = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\David\\Desktop\\WebApplication5\\WebApplication5\\App_Data\\Database2.mdf\";Integrated Security=True";
        cn = new SqlConnection(str);
        SqlCommand command = cn.CreateCommand();

        cn.Open();
        mycount();

        if (firstTime == true)
        {
            displayData();
           firstTime = false;
        }
    }

    protected void mycount()
    {   // count no of els in table
        max = 0;
        var cmd = cn.CreateCommand();

        cmd.CommandText = sqlQuery;
        var reader = cmd.ExecuteReader();
        while (reader.Read()) max++;
        reader.Close();
    }

    protected void displayData()
    {
        var cmd = cn.CreateCommand();

        cmd.CommandText = sqlQuery;
        var reader = cmd.ExecuteReader();

        for (int i = 0; i < count; i++) 
            reader.Read();

        TextBox1.Text = "" + reader[0];
        TextBox2.Text = "" + reader[1];
        TextBox5.Text = "" + reader[2];
        TextBox6.Text = "" + reader[3];
        TextBox7.Text = "" + reader[4];
        TextBox8.Text = "" + reader[5];

        reader.Close();
    }

    protected void deleteData()
    {
        var cmd = cn.CreateCommand();
        string query = "DELETE FROM [Footballer] WHERE [PlayerName] = @name";
        cmd.CommandText = query;

        string name = TextBox4.Text;

        cmd.Parameters.AddWithValue("@name", name);

        cmd.ExecuteNonQuery();
    }
}

【问题讨论】:

  • 如果在deleteData()方法中设置了断点,是不是被命中了?
  • 嗨,埃文,是的,我现在就在那里尝试过,它的方法没问题。问题是什么都没有发生,它没有从 sql 数据库中删除任何东西。就好像我根本没有按下按钮一样。
  • 您已经证明它已到达您的 deleteData 方法。您是否确认您在 TextBox4.Text 中获得了有效值?并且该值与 [Footballer] 表中 [PlayerName] 的行值完全匹配?
  • @您是否尝试直接执行 sql 以确保我们在约束或其他方面没有任何问题?顺便说一句,您应该读取从 ExecuteNonQuery 中返回的值
  • 我在任何地方都没有看到你实际打电话给deleteData() 这是从哪里调用的?

标签: c# sql asp.net sql-server


【解决方案1】:

从代码中,看起来 Textbox4 值在删除之前被覆盖,Page_load 将在每次回发时调用,包括按钮单击事件。 bool firstTime = true 标志不适用于您要达到的目的。我相信您只想在页面第一次加载时才加载文本框数据。因此您应该修改 Page_load 事件以使用 IsPostBack 属性而不是 firstTime 标志,如下所示。

protected void Page_Load(object sender, EventArgs e)
{
    string str = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\David\\Desktop\\WebApplication5\\WebApplication5\\App_Data\\Database2.mdf\";Integrated Security=True";
    cn = new SqlConnection(str);
    SqlCommand command = cn.CreateCommand();

    cn.Open();
    mycount();

    if(!IsPostBack)
    {
        displayData();             
    }
}

这将确保您在 UI 中输入的 TextBox4 值在您单击删除按钮时不会被覆盖,并且删除代码应该按预期工作

【讨论】:

    【解决方案2】:

    不要在Page_Load 中打开连接,仅在需要时打开它。也可以使用using 来妥善处理您的资源。

    public partial class WebForm1 : System.Web.UI.Page
    {
        //You should really pull this from your web.config
        string connectionString = "(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\David\\Desktop\\WebApplication5\\WebApplication5\\App_Data\\Database2.mdf\";Integrated Security=True";;
        static int count = 1;
        static int max = 2;
        static String sqlQuery = "Select * from Footballer";
        static bool firstTime = true;
    
        protected void Page_Load(object sender, EventArgs e)
        {            
            mycount();
    
            if (firstTime == true)
            {
                displayData();
               firstTime = false;
            }
        }
    
        protected void mycount()
        {   // count no of els in table
            max = 0;
            using(SqlConnection con = new SqlConnection(connectionString))
            {
                con.open();
                using(var cmd = cn.CreateCommand())
                {
                   cmd.CommandText = sqlQuery;
                   var reader = cmd.ExecuteReader();
                   while (reader.Read()) max++;
                   reader.Close();
                }
            }
        }
    
        protected void displayData()
        {
            using(SqlConnection con = new SqlConnection(connectionString))
            {
                con.open();
                using(var cmd = cn.CreateCommand())
                {  
                   cmd.CommandText = sqlQuery;
                   var reader = cmd.ExecuteReader();
                   for (int i = 0; i < count; i++) reader.Read();
                     TextBox1.Text = "" + reader[0];
                     TextBox2.Text = "" + reader[1];
                     TextBox5.Text = "" + reader[2];
                     TextBox6.Text = "" + reader[3];
                     TextBox7.Text = "" + reader[4];
                     TextBox8.Text = "" + reader[5];
                   reader.Close();
             }
           }
        }
    
        protected void deleteData()
        {
            //Add A break point here to ensure the method is hit
            using(SqlConnection con = new SqlConnection(connectionString))
            {
                con.open();
                using(var cmd = cn.CreateCommand())
                {            
    
                  string query = "DELETE from [Footballer] where [PlayerName] = @name";
                  cmd.CommandText = query;
    
                  string name = TextBox4.Text;    
                  //When stepping through in debug mode, make sure
                  //name is what you expect.
                  cmd.Parameters.AddWithValue("@name", name);
    
                 cmd.ExecuteNonQuery(); 
                }
           }
        }
    

    注意,我已经在 SO 编辑器中完成了所有这些操作,所以我可能错过了一些结束 }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 2018-10-01
      • 2016-05-02
      • 2019-09-10
      • 1970-01-01
      相关资源
      最近更新 更多