无法在ASP.NET中更新数据库

时间:2014-11-26 18:55:36

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

我想将记录更新到数据库,但它只是保持恢复到原始值。 以下是我的代码。我也没有显示任何错误。

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CandidateConnectionString"].ConnectionString);
conn.Open();

string updateData = "UPDATE Resume SET [Work_Experience] = @work_exp, [Educational_Level] = @edu_level, [Field_Of_Study] = @field_study, [University_Name] = @uni_name, [University_Location] = @uni_locate, [Graduation_Year] = @gra_year WHERE Cand_ID = (SELECT Cand_ID FROM Candidate WHERE Cand_Username = '"+ usernamelbl.Text +"')";
SqlCommand cmd = new SqlCommand(updateData, conn);
cmd.Parameters.AddWithValue("@work_exp", Work_Exp.Text);
cmd.Parameters.AddWithValue("@edu_level", Edu_Level.SelectedItem.Text);
cmd.Parameters.AddWithValue("@field_study", Field_Study.SelectedItem.Text);
cmd.Parameters.AddWithValue("@uni_name", Uni_Name.Text);
cmd.Parameters.AddWithValue("@uni_locate", Uni_Locate.Text);
cmd.Parameters.AddWithValue("@gra_year", Year.Text);

cmd.ExecuteNonQuery();

conn.Close();

代码有问题吗?

1 个答案:

答案 0 :(得分:0)

我发现了一个愚蠢的错误..

在page_Load事件中,我有一行代码从数据库中获取值

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CandidateConnectionString"].ConnectionString);
                conn.Open();
                string getdata = "SELECT * FROM Resume WHERE Cand_ID = (SELECT Cand_ID FROM Candidate WHERE Cand_Username = '" + usernamelbl.Text + "')";
                SqlCommand com = new SqlCommand(getdata, conn);
                SqlDataAdapter sda = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                sda.Fill(ds, "Resume");
                Work_Exp.Text = ds.Tables["Resume"].Rows[0]["Work_Experience"].ToString();
                Edu_Level.Text = ds.Tables["Resume"].Rows[0]["Educational_Level"].ToString();
                Field_Study.Text = ds.Tables["Resume"].Rows[0]["Field_Of_Study"].ToString();
                Uni_Name.Text = ds.Tables["Resume"].Rows[0]["University_Name"].ToString();
                Uni_Locate.Text = ds.Tables["Resume"].Rows[0]["University_Location"].ToString();
                Year.Text = ds.Tables["Resume"].Rows[0]["Graduation_Year"].ToString();
                conn.Close();

但我没有在if(!IsPostBack)下包含它,这就是我提交时的原因,它会覆盖我当前的值并将其恢复到原始状态。感谢你们,他们试图帮助我整理并教会我新的东西。 :)