【问题标题】:update not executing with query string?更新不使用查询字符串执行?
【发布时间】:2011-04-19 13:59:50
【问题描述】:

我正在为我的用户使用一个编辑页面来查看和更改他们在文本框中的数据,他们可以从主页上的 gridview 访问这些数据。我在自动增量列 ProductId 上使用数据键,并且行数据显示完美。不幸的是,当我触发单击按钮事件以使用这些文本框中所做的更改更新行时,它们不会注册。我已经包含了下面的代码,但请注意,这是一个培训项目,为了首先学习基础知识,我被明确禁止参数化。我意识到这是一项安全要求,但目前还没有参数。为了澄清和重申我的问题,当我单击提交按钮时,行数据不受输入到文本框中的更改的影响,而是恢复为原始值。我知道这可能与查询字符串有关,但我不知道是什么。想法?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ViewEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string x = Request.QueryString["ProductId"];
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        string editQuery = "SELECT CustId, CustName, SicNaic, CustCity, CustAdd, CustState, CustZip, BroName, BroId, BroAdd, BroCity, BroState, BroZip, EntityType, Coverage, CurrentCoverage, PrimEx, Retention, EffectiveDate, Commission, Premium, Comments, ProductId FROM ProductInstance WHERE ProductId =" + x;



    using (SqlConnection editConn = new SqlConnection(connectionString))
    {
        editConn.Open();

        using (SqlCommand command = new SqlCommand(editQuery, editConn))
        {

            SqlDataReader dr = command.ExecuteReader();
            dr.Read();
            TextBox1.Text = dr.GetInt32(0).ToString();
            TextBox2.Text = dr.GetString(1);
            TextBox3.Text = dr.GetString(2);
            TextBox4.Text = dr.GetString(3);
            TextBox5.Text = dr.GetString(4);
            TextBox6.Text = dr.GetString(5);
            TextBox7.Text = dr.GetInt32(6).ToString();
            TextBox8.Text = dr.GetString(7);
            TextBox9.Text = dr.GetInt32(8).ToString();
            TextBox10.Text = dr.GetString(9);
            TextBox11.Text = dr.GetString(10);
            TextBox12.Text = dr.GetString(11);
            TextBox13.Text = dr.GetInt32(12).ToString();
            TextBox14.Text = dr.GetString(13);
            TextBox15.Text = dr.GetInt32(14).ToString();
            TextBox16.Text = dr.GetInt32(15).ToString();
            TextBox17.Text = dr.GetInt32(16).ToString();
            TextBox18.Text = dr.GetInt32(17).ToString();
            TextBox19.Text = dr.GetDateTime(18).ToString();
            TextBox20.Text = dr.GetInt32(19).ToString();
            TextBox21.Text = dr.GetInt32(20).ToString();
            TextBox22.Text = dr.GetString(21);



        }
        editConn.Close();
    }   
}

protected void Button1_Click(object sender, EventArgs e)
{
    string x = Request.QueryString["ProductId"];
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    using (SqlConnection updateConn = new SqlConnection(connectionString))
    {
        updateConn.Open();
        {
            string updateQuery = "UPDATE ProductInstance SET CustId = '" + TextBox1.Text + "', CustName = '" + TextBox2.Text + "', SicNaic = '" + TextBox3.Text + "', CustCity =  '" + TextBox4.Text + "', CustAdd = '" + TextBox5.Text + "', CustState =  '" + TextBox6.Text + "', CustZip =  '" + TextBox7.Text + "', BroName = '" + TextBox8.Text + "', BroId =  '" + TextBox9.Text + "', BroAdd =  '" + TextBox10.Text + "', BroCity = '" + TextBox11.Text + "', BroState =  '" + TextBox12.Text + "', BroZip =  '" + TextBox13.Text + "', EntityType =  '" + TextBox14.Text + "', Coverage =  '" + TextBox15.Text + "', CurrentCoverage =  '" + TextBox16.Text + "', PrimEx = '" + TextBox17.Text + "', Retention = '" + TextBox18.Text + "', EffectiveDate =  '" + TextBox19.Text + "', Commission = '" + TextBox20.Text + "', Premium =  '" + TextBox21.Text + "', Comments = '" + TextBox22.Text + "' WHERE ProductId =" + x;



            using (SqlCommand command = new SqlCommand(updateQuery, updateConn))
            {
                command.ExecuteNonQuery();
            }
        }
    }
}

}

【问题讨论】:

  • 仅供参考 - 您的代码容易受到 sql 注入的影响。

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


【解决方案1】:

布拉索斯,

发生这种情况是因为当进行更改并将其发回页面时,Page_Load 正在执行再次,然后您才能将从表单收集的值保存回数据库。相反,文本框会加载数据库中的值,并且任何更改都会被覆盖。稍后,当 Button1_Click 事件发生并且您确实将数据保存到数据库时,您使用文本框中的值更新该行,这些值现在首先反映了数据库中的内容,而不是原来的内容以表格形式提交。

在此处查看 ASP.NET 页面生命周期中的事件顺序: http://msdn.microsoft.com/en-us/library/ms178472.aspx

解决此问题的一种简单方法是仅运行加载和更新文本框(在 Page_Load 中)的查询(如果它不在回发中):

public partial class ViewEdit : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            string x = Request.QueryString["ProductId"];
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
            string editQuery = "SELECT CustId, CustName, SicNaic, CustCity, CustAdd, CustState, CustZip, BroName, BroId, BroAdd, BroCity, BroState, BroZip, EntityType, Coverage, CurrentCoverage, PrimEx, Retention, EffectiveDate, Commission, Premium, Comments, ProductId FROM ProductInstance WHERE ProductId =" + x;



        using (SqlConnection editConn = new SqlConnection(connectionString))
        {
            editConn.Open();

            using (SqlCommand command = new SqlCommand(editQuery, editConn))
            { [...]

但是,这也意味着在更改完成后,您将不再从数据库重新加载更改,因为此后的每个页面事件都将是回发(除非您进行重定向)。由于您正在学习 ASP.NET,因此我建议您查看页面生命周期并探索不同的解决方案。祝你好运!

【讨论】:

  • 谢谢你,KTF,太好了!我现在正在读这篇文章。为了澄清起见,我应该在我的原始查询上执行 if 序列(这只是为了显示信息),还是应该在查询上更新?我会假设是后者,但我提出问题是有原因的......
  • 您的问题暗示了对 ASP.NET 模型的误解。页面上的几乎所有事件都是“回发”,因为它会由页面触发,可以这么说。因此,您希望 Button Click 事件始终更新。 (如果页面不是回发,它将永远不会被调用)。这篇文章也可能对您有所帮助:msdn.microsoft.com/en-us/library/59t350k3(v=vs.71).aspx
  • 触摸。感谢您不遗余力地教我如何钓鱼。我基本上是在匆匆忙忙地完成这项工作,这相当于尝试在一年而不是 10 年内在家完成医学院学习。没有捷径可以精通这个专业。
【解决方案2】:

在您的 Page_Load 检查回发

protected void Page_Load(object sender, EventArgs e)     
{         
    if (Page.IsPostBack == false)
    {
        string x = Request.QueryString["ProductId"];         
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;         string editQuery = "SELECT CustId, CustName, SicNaic, CustCity, CustAdd, CustState, CustZip, BroName, BroId, BroAdd, BroCity, BroState, BroZip, EntityType, Coverage, CurrentCoverage, PrimEx, Retention, EffectiveDate, Commission, Premium, Comments, ProductId FROM ProductInstance WHERE ProductId =" + x;
    }
}

【讨论】:

  • 谢谢你,瑞恩。您和 KTF 的回答解决了我的问题。非常感谢。
【解决方案3】:

在这样的页面上,您应该始终做的第一件事是检查 !IsPostBack,然后执行在代码中呈现页面的标准流程:

protected void Page_Load(object sender, EventArgs e)
{         
    if (!IsPostBack)
    {
        // Add your normal code in here
    }
}

然后在你的点击事件中,当你完成插入数据库时​​重新绑定数据

protected void Button1_Click(object sender, EventArgs e)
{
    // Do Insertion here
    lstView.DataSource = sqlVals;
    lstView.DataBind();
}

还可以阅读 EF4 或 LinqToSql,因为它将使从 SQL 中的数据调用更容易、更安全

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2011-05-07
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    相关资源
    最近更新 更多