Data not writing out to Database

后端 未结 2 1611
无人及你
无人及你 2021-01-26 13:56

I\'m writing a bit value to a database in the CheckboxProcess_CheckedChanged event. However, nothing is actually being written out. I had some code in there filling an adapter,

相关标签:
2条回答
  • 2021-01-26 14:12

    I would suggest you try the following steps:

    • separate the UI code (reading and writing the textboxes and grid and stuff) from the actual database code - at some point, you might want to separate those out to separate assemblies, even

    • use parametrized queries for updating your data! Prevents SQL injection attacks, and makes your stuff go faster, too! Use them - always - no excuses

    Your code would then look something like this:

    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();
        }
    }
    

    Now I don't know if that's really going to solve your problem - I couldn't see anything at first sight.... but try it - maybe that'll give you a head start towards isolating your issues!

    0 讨论(0)
  • 2021-01-26 14:26

    For your update statements you have:

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

    I'm wondering if you need to remove the space after the last % mark:

    string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '%" + DefaultGrid.SelectedRow.Cells[3].Text + "%'";
    
    0 讨论(0)
提交回复
热议问题