C# - 更新按钮未更新
如上所述。在更改完成后单击“更新”按钮时,它会重新加载页面,但不会更新到SQLDB中。我刚刚进入编程,希望得到一些帮助。
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BiodiversityProject"].ConnectionString);
conn.Open();
//See above comments
string image_folder = Server.MapPath("~\\uploadedimages\\");
string dbfilename1 = GridView1.SelectedRow.Cells[6].Text.ToString();
string dbfilename2 = GridView1.SelectedRow.Cells[7].Text.ToString();
if (VideoFileUpload.HasFile)
{
string filename1 = VideoFileUpload.FileName.ToString();
VideoFileUpload.SaveAs(image_folder + filename1);
dbfilename1 = filename1;
}
string multimedia_folder = Server.MapPath("~\\uploaded_multimedia\\");
if (VideoFileUpload.HasFile)
{
string filename2 = VideoFileUpload.FileName.ToString();
VideoFileUpload.SaveAs(multimedia_folder + filename2);
dbfilename2 = filename2;
}
//string connectionString = null;
//int userId = 0;
SqlCommand UpdateCommand = new SqlCommand("UPDATE LeagueTable SET BirdName = @p1, Quantity = @p2, Place = @p3, Location =@p4, Image = @p5, mediaclip = @p6, Username = @p7, datetime = @p8, Active Status= @p9 WHERE Bird_ID = " + GridView1.SelectedDataKey.Value.ToString());
UpdateCommand.Parameters.Add(new SqlParameter("@p1", System.Data.SqlDbType.VarChar)).Value = TextBox1.Text.ToString();
UpdateCommand.Parameters.Add(new SqlParameter("@p2", System.Data.SqlDbType.VarChar)).Value = TextBox2.Text.ToString();
UpdateCommand.Parameters.Add(new SqlParameter("@p3", System.Data.SqlDbType.VarChar)).Value = DropDownList2.SelectedValue.ToString();
UpdateCommand.Parameters.Add(new SqlParameter("@p4", System.Data.SqlDbType.VarChar)).Value = txtlocation.Text.ToString();
UpdateCommand.Parameters.Add(new SqlParameter("@p5", System.Data.SqlDbType.VarChar)).Value = dbfilename1.ToString();
UpdateCommand.Parameters.Add(new SqlParameter("@p6", System.Data.SqlDbType.VarChar)).Value = dbfilename2.ToString();
UpdateCommand.Parameters.Add(new SqlParameter("@p7", System.Data.SqlDbType.VarChar)).Value = User.Identity.Name;
UpdateCommand.Parameters.Add(new SqlParameter("@p8", System.Data.SqlDbType.VarChar)).Value = DateTime.Now.ToShortDateString().ToString();
UpdateCommand.Parameters.Add(new SqlParameter("@p9", System.Data.SqlDbType.VarChar)).Value = DropDownList1.SelectedValue.ToString();
try
{
conn.Open();
UpdateCommand.ExecuteNonQuery();
conn.Close();
}
catch (Exception)
{
}
GridView1.DataBind();