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

Rohini ParadeRank #1,014 · 1.9K reputationPosted Mar 7, 2024, 11:34 AM EST
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
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 7, 2024, 1:18 PM EST
Hello Mehmat,
Please use this one,
OleDbCommand cmd1 = new OleDbCommand("SELECT tamamlandi, okulno, adisoyadi FROM ogrencigorusme WHERE tamamlandi = True AND okulno = @OkulNo", conn); cmd1.Parameters.AddWithValue("@OkulNo", txt_Noara.Text); OleDbDataReader dr1 = cmd1.ExecuteReader();OR
OleDbCommand cmd1 = new OleDbCommand("select tamamlandi,okulno,adisoyadi from ogrencigorusme WHERE IIF(amamlandi,1,0) = 1 and okulno = '" + txt_Noara.Text + "'", conn); OleDbDataReader dr1 = cmd1.ExecuteReader();Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 8, 2024, 2:40 AM EST
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:
OleDbCommand cmd1 = new OleDbCommand("SELECT tamamlandi, okulno, adisoyadi FROM ogrencigorusme WHERE tamamlandi = true AND okulno = '" + txt_Noara.Text + "'", conn);Or if
tamamlandiis stored as an integer:OleDbCommand cmd1 = new OleDbCommand("SELECT tamamlandi, okulno, adisoyadi FROM ogrencigorusme WHERE tamamlandi = 1 AND okulno = '" + txt_Noara.Text + "'", conn);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:
OleDbCommand cmd1 = new OleDbCommand("SELECT tamamlandi, okulno, adisoyadi FROM ogrencigorusme WHERE tamamlandi = true AND okulno = ?", conn); cmd1.Parameters.Add(new OleDbParameter("okulno", txt_Noara.Text));Again, replace
truewith the correct representation of boolean in your database.Thanks
Mehmet FatihRank #1,474 · 1.1K reputationPosted Mar 7, 2024, 3:15 PM EST
I am sorry to say that I have tried all of them but the result is the same.
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 7, 2024, 3:30 PM EST
OleDbCommand cmd1 = new OleDbCommand("select tamamlandi,okulno,adisoyadi from ogrencigorusme WHERE Convert.ToInt32(tamamlandi) = 1 and okulno = '" + txt_Noara.Text + "'", conn); OleDbDataReader dr1 = cmd1.ExecuteReader();Mehmet FatihRank #1,474 · 1.1K reputationPosted Mar 7, 2024, 3:51 PM EST
It gives this error. function convert.toInt32 not defined in expression
Mehmet FatihRank #1,474 · 1.1K reputationPosted Mar 7, 2024, 4:36 PM EST
I am sorry Jignesh, it was my fault. I have found the error. I had written the textbox name incorrectly..
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 7, 2024, 10:45 PM EST
Hello What is data-type of this column, "tamamlandi", Could you please run this query on database and share result here,
select tamamlandi,okulno,adisoyadi from ogrencigorusme