Question

I have made a program in C# and now I want to update a boolean value in my Access database. This boolean value will always be updated to false. I have absolutely tried everything and there is no change in my value. Please help.

Here is my code :

The [In mail] column is my boolean value. I have tried this query in Access and it works perfectly there.

        String query = "UPDATE Ontwikkeldossier SET Ontwikkeldossier.[In mail] = @fals WHERE (((Ontwikkeldossier.[OntwikkeldossierID])=@ontwikkeldossierid));";
        using(OleDbConnection conn = new OleDbConnection(connstring))
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand(query, conn);
            cmd.Parameters.Add("@fals", OleDbType.Boolean, 1, "[In mail]").Value = false;
            cmd.Parameters.Add("@ontwikkeldossierid", OleDbType.Numeric).Value = Convert.ToInt32(newrow.Cells[0].Value.ToString());
            cmd.ExecuteNonQuery();
        }

Thanks in advance.

Was it helpful?

Solution 2

For what it's worth, I just tested the following code and it works for me:

using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\__tmp\testData.accdb;"))
{
    conn.Open();
    using (var cmd = new OleDbCommand(
            "UPDATE Ontwikkeldossier SET [In mail]=? WHERE OntwikkeldossierID=?",
            conn))
    {
        cmd.Parameters.AddWithValue("?", false);
        cmd.Parameters.AddWithValue("?", 1);
        cmd.ExecuteNonQuery();
    }
    conn.Close();
}

OTHER TIPS

updateCmd.Parameters.Add("@3", OleDbType.Boolean).Value = "true";

this worked for me! I have "Yes/No" column in Access. Update command is:

OleDbCommand updateCmd = new OleDbCommand("UPDATE email SET server = @1, port = @2, ssl = @3, utilizator = @4, parola = @5, subiect = @6, email = @7 WHERE ID = @8", connection);

Should this:

cmd.Parameters.Add("@fals", OleDbType.Boolean, 1, "[In mail]").Value = false;

Instead be:

cmd.Parameters.Add("@fals", OleDbType.Boolean).Value = false;  //or zero, since it's an object, the DB might care...

Is the source an issue? If you're supplying the value, why do you want a source for the value?

Of course, since it's always false for you, you could simply change the query and eliminate the parameter altogether:

String query = "UPDATE Ontwikkeldossier SET Ontwikkeldossier.[In mail] = 0 WHERE (((Ontwikkeldossier.[OntwikkeldossierID])=@ontwikkeldossierid));";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top