【发布时间】: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