更新查询不更新我的项目中的表中的数据
这是我的代码。它不更新表中的数据。我不能看到任何错误。该代码正在执行,并向我显示“已成功更新”。更新查询不更新我的项目中的表中的数据
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;
}
什么这个????
哎呀!您的问题无法提交,因为:
您的帖子没有太多的上下文来解释代码段; 请更清楚地解释你的情况。
很难得到这样的问题。然而,我怀疑你的代码在这条线上:
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+"'";
你是否得到正确的值才能成功更新你的查询?在调试之后制作一个断点并检查它。
或者
作出这样一个非常简单的更新语句:更新RegisterMaster设置名称=“+ txtName.Text +”,并确保你的表得到更新。
而且当然你的查询对于Sql-Injection来说很脆弱,正如Leland Richardson提到的那样。 你可以在这里了解更多关于此:http://www.codeproject.com/Articles/9378/SQL-Injection-Attacks-and-Some-Tips-on-How-to-Prev
是的,谢谢。我会研究。 –
更改值您的意思是文本框的值?你在你的PageLoad()中使用if(!IsPostBack)吗? –
不,我没有使用它。并且它不更新页面上的任何值。 –
你的更新查询在哪里? –
c1.ExecuteMyQuery(updateQuery)的实现在哪里? – Habib
我更新了这个问题。我已经正确定义了SQLCommand cmd; SqlConnection con; –