2011-07-12 13 views
0

私は、CheckboxProcess_CheckedChangedイベントでデータベースにビット値を書き込みます。しかし、実際には何も書き出されていません。そこにアダプターを充填するコードがありましたが、取り出しました。誰がこれが間違っているのか分かりますか?データがデータベースに書き込まれない

あなたが持っているあなたの更新ステートメントについては
protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      using (SqlConnection connection = new SqlConnection(connectionString.ToString())) 
      { 
       connection.Open(); 
       dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection); 
       dataSet = new DataSet();    
       dataAdapter.Fill(dataSet, "SecureOrders"); 
       DataView source = new DataView(dataSet.Tables[0]); 
       DefaultGrid.DataSource = source; 
       DefaultGrid.DataBind(); 
       connection.Close(); 
      } 
     } 
    } 

    protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e) 
    { 
     bool update; 
     string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'"; 
     string uncheckedString = "UPDATE SecureOrders SET processed = 0 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'"; 
     CheckBox cb = (CheckBox)sender; 
     GridViewRow gvr = (GridViewRow)cb.Parent.Parent; 
     DefaultGrid.SelectedIndex = gvr.RowIndex; 
     update = Convert.ToBoolean(DefaultGrid.SelectedValue); 

     orderByString = orderByList.SelectedItem.Value; 
     fieldString = searchTextBox.Text; 



     connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"]; 

     using (SqlConnection connection = new SqlConnection(connectionString.ToString())) 
     { 
      connection.Open(); 
      SqlCommand checkedCmd = new SqlCommand(checkedString, connection); 
      SqlCommand uncheckedCmd = new SqlCommand(uncheckedString, connection); 

      if (cb.Checked == true) 
      { 
       checkedCmd.ExecuteNonQuery(); 

      } 
      else 
      { 
       uncheckedCmd.ExecuteNonQuery(); 
      } 

      connection.Close(); 
     } 
+0

私は毎回それをしています...人々はもっと欲しいです...しかし、私はそれを打つつもりです –

+0

なぜコメントされたコードとスペースがたくさんあるのですか? – Reniuz

+0

ソート済みです。私は急いでいた。 –

答えて

3

string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'"; 

あなたが最後の%記号の後にスペースを削除する必要がある場合、私は思ったんだけど:

string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '%" + DefaultGrid.SelectedRow.Cells[3].Text + "%'"; 
+1

あなたの深刻な答えをありがとうございます。私はそれを感謝します。私はそのトリックをしたと確信しています - 私はちょうど –

+2

お互いに別々の質問を投稿してもよろしいですか?:) – NickHeidke

2

私はあなたが試すことをお勧め次のステップ:

  • 実際のデータベースのコードから(テキストボックスとグリッドとのものを読み書き)UIコードを分離 - ある時点で、あなたは別のアセンブリへのそれらを分離する場合があります、でも

  • 使用パラメータ化を更新するためのを問い合わせますあなたのデータ! SQLインジェクション攻撃を防ぎ、処理時間を短縮します。それらを使用しない - 常に - 言い訳

その後、次のようになりますあなたのコード:今すぐ

protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e) 
{ 
    bool update = Convert.ToBoolean(DefaultGrid.SelectedValue); 

    // determine your first name and last name values 
    string firstName = .......; 
    string lastName = .......; 

    UpdateYourData(update, firstName, lastName); 
} 

private void UpdateYourData(bool isProcessed, string firstName, string lastName) 
{ 
    Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/Cabot3"); 
    ConnectionStringSettings connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"]; 

    string updateStmt = "UPDATE dbo.SecureOrders SET processed = @Processed " + 
         "WHERE fName LIKE @firstName AND lName LIKE @lastName"; 

    using (SqlConnection connection = new SqlConnection(connectionString.ToString())) 
    using (SqlCommand _update = new SqlCommand(updateStmt, connection)) 
    { 
     _upate.Parameters.Add("@Processed", SqlDbType.Bit).Value = isProcessed; 
     _upate.Parameters.Add("@firstName", SqlDbType.VarChar, 100).Value = firstName; 
     _upate.Parameters.Add("@lastName", SqlDbType.VarChar, 100).Value = lastName; 

     connection.Open(); 
     _update.ExecuteNonQuery(); 
     connection.Close(); 
    } 
} 

それは本当にあなたの問題を解決するために起こっている場合、私は知らない - 私は見ることができませんでした一見すると何か....しかしそれを試してみてください - 多分それはあなたの問題を隔離するための頭のスタートを与えるでしょう!

+0

あなたのお手伝いをありがとう - 私はそれを撃つようにしようとしています! –

関連する問題