2012-11-23 73 views
0

我有一個填充了從數據庫檢索的值的表單。該表單有一個提交按鈕,應該在點擊數據庫時更新數據庫中的相應字段。更新表格保留原始值

問題是無論我做什麼值都不會在數據庫中更新。我正在使用一個存儲過程,並檢查了兩次,它工作正常,我也嘗試了硬編碼插入值的技巧 - 也可以。

我的猜測是,一旦該字段從Page_Load上的數據庫填充,即使我改變它的值,它的值仍然存在。如何解決此問題,以便我可以使用相同的表單來檢索和更新數據?

protected void Page_Load(object sender, EventArgs e) 
    { 
     lblMsg.Visible = false; 

     string bookingid = Request.QueryString["bookingid"]; 
     Booking b = BookingAccess.GetBooking(Int32.Parse(bookingid)); 
     if (b != null) 
     { 
      var start_date_formatted = ((DateTime)b.StartDate).ToString("dd-MM-yyyy"); 
      var end_date_formatted = ((DateTime)b.EndDate).ToString("dd-MM-yyyy"); 

      pet_name.Text = b.PetName; 
      species.Text = b.Species; 
      start_date.Text = start_date_formatted; 
      end_date.Text = end_date_formatted; 
     } 


    } 

    protected void btnEditBooking_Click(object sender, EventArgs e) 
    { 
     string bookingid_raw = Request.QueryString["bookingid"]; 
     int bookingid = int.Parse(bookingid_raw); 

     var start_date_formatted = DateTime.ParseExact(start_date.Text, "dd-MM-yyyy", null); 
     var end_date_formatted = DateTime.ParseExact(end_date.Text, "dd-MM-yyyy", null); 

     string pet_name = "a"; 
     string species = "b"; 
     lblMsg.Visible = true; 
     string msg = BookingAccess.UpdateBooking(bookingid, pet_name, species, start_date_formatted, end_date_formatted); 
     if (msg == null) 
      lblMsg.Text = "Updated booking details successfully!"; 
     else 
      lblMsg.Text = "Error -> " + msg; 
    } 
+2

請添加您的代碼。並確保你的郵件沒有填充字段。 – MikeSmithDev

回答

1

如果你有在Page_Load功能將數據裝載,確保您的IsPostBack檢查,以確保當用戶提交頁面不重新設置數據。

private void Page_Load() 
{ 
    if (!IsPostBack) 
    { 
     // Load the data from database 
    } else { 
     // Validate the data and save to database 
    } 
}