I am getting an error when querying a bool value column in a SQL query. Nothing I try is right. I need help on this.
OleDbCommand cmd1 = new OleDbCommand("select tamamlandi,okulno,adisoyadi from ogrencigorusme WHERE Convert.ToBoolean(tamamlandi) = 1 and okulno = '" + txt_Noara.Text + "'", conn);
OleDbDataReader dr1 = cmd1.ExecuteReader();
my other attempts : tamamlandi=true or tamamlandi= 1
Naimish MakwanaPosted Mar 8, 2024, 7:40 AM
In SQL, boolean values are often represented as
0and1, with1representingtrueand0representingfalse. However, the way you’re trying to convert and compare might be causing the issue.In your SQL query, you’re trying to use
Convert.ToBoolean(tamamlandi) = 1, which is not a valid SQL syntax. Instead, you should directly comparetamamlandiwith1ortrue(depending on your database).Here’s how you can modify your command:
Or if
tamamlandiis stored as an integer:Please replace
trueor1with the correct representation of boolean in your database.Also, please note that concatenating strings to form SQL queries can lead to SQL injection attacks. Consider using parameterized queries to avoid this. Here’s how you can do it:
Again, replace
truewith the correct representation of boolean in your database.Thanks
Jignesh KumarPosted Mar 8, 2024, 3:45 AM
Hello What is data-type of this column, "tamamlandi", Could you please run this query on database and share result here,
Mehmet FatihPosted Mar 7, 2024, 9:36 PM
I am sorry Jignesh, it was my fault. I have found the error. I had written the textbox name incorrectly..
Mehmet FatihPosted Mar 7, 2024, 8:51 PM
It gives this error. function convert.toInt32 not defined in expression
Jignesh KumarPosted Mar 7, 2024, 8:30 PM
Mehmet FatihPosted Mar 7, 2024, 8:15 PM
I am sorry to say that I have tried all of them but the result is the same.
Jignesh KumarPosted Mar 7, 2024, 6:18 PM
Hello Mehmat,
Please use this one,
OR
Rohini ParadePosted Mar 7, 2024, 4:34 PM
You are using c# function in query which will not support because while executing query SQL server don't know definition of c# convert function. If that is table column you can directly use ColumnName=1. Casting not required