2012-04-12 14 views
0

私は、ユーザーがテキストを編集できるWebページを作成しようとしています。編集が完了すると、保存され、入力された新しいテキストがデータベースに保存されます。ContentEditableを使用してASP.NETでDBに保存しますか?

私のコードでエラーは発生していませんが、何らかの理由で古いテキストが新しいテキストの代わりにdbに書き直されています。

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

答えて

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

私はそれを試み、私は同じ結果を得た。ページは単にリロードされ、dbの値は変わりません。 – user1305075

+0

接続文字列は正しいですか?コードをデバッグして、変数の値を参照して、コードが実行しなかった箇所を確認してください。 – MarcoM

関連する問題