2012-04-12 30 views
0

我試圖創建一個用戶可以編輯文本的網頁,當他們完成後,他們點擊保存並輸入的新文本保存到數據庫中。使用ContentEditable保存到DB與ASP.NET?

我沒有在我的代碼中發現任何錯誤,但由於某種原因,舊文本只是被重寫入數據庫而不是新文本。

這裏是我的代碼隱藏:

protected void saveBtn_Click(object sender, EventArgs e) 
{ 
    string newName; 
    string newIntro; 
    string newEduc; 
    string newWork; 

    h1New.Text = h1.Text; 

    newName = h1New.Text; 
    newIntro = intro.Text; 
    newEduc = educ.Text; 
    newWork = employ.Text; 

    string connectionInfo = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; 

    using (SqlConnection connection = new SqlConnection(connectionInfo)) 
    { 

     connection.Open(); 
     SqlCommand myCommand = new SqlCommand("UPDATE simpleContent SET userName = @newName, infoContent = @newIntro, educContent = @newEduc, workContent = @newWork WHERE userID = @userName", connection); 

     try 
     { 

      string username = HttpContext.Current.User.Identity.Name; 
      myCommand.Parameters.AddWithValue("@userName", username.ToString()); 
      myCommand.Parameters.AddWithValue("@newName", newName.ToString()); 
      myCommand.Parameters.AddWithValue("@newIntro", newIntro.ToString()); 
      myCommand.Parameters.AddWithValue("@newEduc", newEduc.ToString()); 
      myCommand.Parameters.AddWithValue("@newWork", newWork.ToString()); 
      myCommand.ExecuteNonQuery(); 
      connection.Close(); 
     } 
     catch 
     { 
      Response.Redirect("http://www.google.co.uk"); 
     } 


    } 
} 

我將不勝感激,你可能有任何指針。

+0

你可以發佈你目前擁有的代碼在你的「的Page_Load」的方法? – Reinaldo 2012-04-12 16:11:42

回答

0

儘量把你的格式代碼:

protected void saveBtn_Click(object sender, EventArgs e) 
{  
// add variables 
    string connectionInfo = (...) 
    string commandText = (...) 

    using (...){ 
     SqlCommand myCommand = (...) 
     // add parameters 

    try 
    { 
     connection.Open(); 
     myCommand.ExecuteNonQuery(); 
     connection.Close(); 
    } 

    catch (Exception ex) 
      { 
       (...) 
      } 
} 
+0

我試過了,得到了同樣的結果。該頁面只是重新加載,而數據庫中的值仍然相同。 – user1305075 2012-04-12 20:41:32

+0

你連接字符串是否正確?嘗試調試您的代碼並查看變量的值,以查看代碼未執行的操作。 – MarcoM 2012-04-13 07:39:34