【问题标题】:Update query is not updating the data in table in my project更新查询未更新我项目中表中的数据
【发布时间】:2012-06-24 10:13:55
【问题描述】:

这是我的代码。它不会更新表中的数据。我看不到任何错误。代码正在执行并显示“已成功更新”。

 protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string val = ddlCountry.SelectedValue;
        Response.Write(val); // just to check that the value is changed or not.

        string val2 = txtName.Text;
        Response.Write(val2);

        if (ddlCity.SelectedValue == "--Select--")
        {

            Response.Redirect("updateProfile.aspx");
            lblCountry.Text = "Select your country";
        }
        else if(ddlYear.SelectedValue=="--Select--")
        {

            Response.Redirect("updateProfile.aspx");
            lblCountry.Text = "Select Appropriate Experience";
        }
        else if (ddlMonth.SelectedValue == "--Select--")
        {

            Response.Redirect("updateProfile.aspx");
            lblCountry.Text = "Select Appropriate Experience";
        }
        else if(ddlIndustry.SelectedValue=="--Select--")
        {

            Response.Redirect("updateProfile.aspx");
            lblCountry.Text = "Select Your Current Industry";
        }
        else if(ddlFunction.SelectedValue=="--Select--")
        {

            Response.Redirect("updateProfile.aspx");
            lblCountry.Text = "Select your functional Area";
        }
        else
        {
            string fName = Convert.ToString(Session["fname"]);
            string updateQuery = "Update RegisterMaster set Name='" + txtName.Text + "',Nationality='" + ddlCountry.SelectedValue + "',CurrentLocation='" + ddlCity.SelectedValue + "',MobNumber='"+txtNumber.Text+"',Experience='"+ddlYear.SelectedValue+" "+ddlMonth.SelectedValue+"',CurrentIndustry='"+ddlIndustry.SelectedValue+"',FunctionalArea='"+ddlFunction.SelectedValue+"',KeySkills='"+txtSkills.Text+"',ResumeTitle='"+txtResTitle.Text+"',Resume='"+resFileUpload.ToString()+"' where Name='"+fName+"'";

           int i = c1.ExecuteMyQuery(updateQuery);
           if (i == 1)
           {
               lblUpdation.Text = "Successfully Updated.";
           }
           else
           {
               lblUpdation.Text = "Try Again";
           }

        }
    }

它显示更新成功,但是当我检查数据库时,它没有更新。 updateProfile.aspx 是完成此编码的同一页面。如果这也很重要,它在一个框架集中。

实现

c1.ExecuteMyQuery(updateQuery);


 public int ExecuteMyQuery(String sql)
        {
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = sql;
            int i = cmd.ExecuteNonQuery();
            con.Close();
            return i;
        }

这是什么????

哎呀!无法提交您的问题,因为:

您的帖子没有太多上下文来解释代码部分; 请更清楚地解释您的情况。

【问题讨论】:

  • c1.ExecuteMyQuery(updateQuery)的实现在哪里
  • 我更新了问题。而且我已经正确定义了 SQLCommand cmd; SqlConnection con;
  • 在命令执行之前发布updateQuery 的值怎么样?由于您没有使用参数,因此您的代码很容易出错。如果其中任何一个值中有特殊字符,则您的 SQL 语句可能会变得无效。由于这个原因,您应该使用 SQL 参数,因为现在您非常容易出现SQL Injection
  • @LelandRichardson 实际上我刚刚开始学习 ASP.NET,所以我只是在做一个随机项目来消除我的疑虑。在我的项目中,此级别不需要安全性。我正在制作一个工作门户网站。如果有人想更新他的个人资料,那么这个代码就会执行。但它没有采用更新的值。它采用数据库中已经存在的先前值。我可能应该发送那个...(2分钟)的快照。
  • @NehaChoudhary 理解是否安全不是问题,但是您应该尝试在查询中使用参数,因为它会使代码更容易理解并避免这样的错误(假设这是原因对于错误)

标签: asp.net


【解决方案1】:

很难像这样找出问题所在。但是,我对您在这一行的代码有疑问:

string fName = Convert.ToString(Session["fname"]);
        string updateQuery = "Update RegisterMaster set Name='" + txtName.Text + "',Nationality='" + ddlCountry.SelectedValue + "',CurrentLocation='" + ddlCity.SelectedValue + "',MobNumber='"+txtNumber.Text+"',Experience='"+ddlYear.SelectedValue+" "+ddlMonth.SelectedValue+"',CurrentIndustry='"+ddlIndustry.SelectedValue+"',FunctionalArea='"+ddlFunction.SelectedValue+"',KeySkills='"+txtSkills.Text+"',ResumeTitle='"+txtResTitle.Text+"',Resume='"+resFileUpload.ToString()+"' where Name='"+fName+"'";

您是否获得了正确的值来成功更新您的查询?下断点,调试后检查。

或者

制作一个非常简单的更新语句,例如:Update RegisterMaster set Name="+txtName.Text+",并确保您的表格得到更新。

当然,正如 Leland Richardson 所说,您的查询容易受到 Sql-Injection 的攻击。 你可以在这里了解更多信息:http://www.codeproject.com/Articles/9378/SQL-Injection-Attacks-and-Some-Tips-on-How-to-Prev

【讨论】:

  • 更改的值是指文本框的值吗?你在 PageLoad() 中使用 if (!IsPostBack) 吗?
  • 不,我没有使用它。并且它不会更新页面上的任何值。
  • 在您的查询中,您从 texbox 获取值,对吗?请尝试在 PageLoad() 中使用 if(!IsPostBack)。 msdn.microsoft.com/en-us/library/…
  • 嗯...是的.. 非常感谢。我不知道为什么我总是忘记这一点。 :)
  • 还有一个疑问。当我选择当前位置为“--select--”时,我的代码无法正常工作。它不会将消息显示为“选择您的城市”。它没有转到 if else 语句,而是转到具有更新查询的 else 部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 2013-10-19
  • 2020-07-19
相关资源
最近更新 更多