Resolved bool Wrong Data type

PDS8475

Active member
Joined
Jun 25, 2019
Messages
41
Programming Experience
Beginner
Hi I'm trying to pass a bool to a table in access amongst other values.
The bool is just set when the user clicks a button rather than from a checkbox.
The query works if I take the bool value out.
The code that I use to set and add as a parameter is:
C#:
            bool s = true;
            OleDbParameter opa = new OleDbParameter("@S", OleDbType.Boolean);
            opa.Value = s;
            cmdd.Parameters.Add(opa);


I'm not sure how to set the parameter as true. As you can see I have tried setting a bool variable as true and passing it to the variable, but this didn't work
Any help would be much appreciated.
 
Seems that I didn't have to bother with parameters since it will never change, All I needed to do is change the bool field(sold) in the query to sold = True and it works
 
It would be worthwhile determining why it didn't work because you may find yourself in the same position with a value that isn't constant. I can't see an issue with the code you've posted other than its being a bit verbose. I would have written it like this:
C#:
cmdd.Parameters.Add("@S", OleDbType.Boolean).Value = true;
That's functionally equivalent to what you have though, so it should still work the same way. I'm wondering whether you have another parameter and you were adding them in the wrong order. Although you can name your parameters when using an Access database, those names are actually ignored at run time and only the positions matter. That means, if you don't add parameters to the command in the same order as they appear in the SQL code, you're actually not executing the SQL you intend. If the parameters are different data types then an exception may be thrown. Worse still, if the parameters are compatible data types then no exception will be thrown and the code will execute with unintended consequences.
 
Back
Top Bottom