Reputation: 59
I have a winform C# SQL app in that i'm trying to fetch data from a table using following query
fth.CommandText = "
Select Enrollment_No, Student_Name
from Final_Result_Master
where Sub_1=@Sub
and Sub_1_stat=@sta Or Sub_2=@Sub
and Sub_2_stat=@sta Or Sub_3=@Sub
and Sub_3_stat=@sta Or Sub_4=@Sub
and Sub_4_stat=@sta Or Sub_5=@Sub
and Sub_5_stat=@sta Or Sub_6=@Sub
and Sub_6_stat=@sta Or Sub_7=@Sub
and Sub_7_stat=@sta Or Sub_8=@Sub
and Sub_8_stat=@sta Or Sub_9=@Sub
and Sub_9_stat=@sta Or Sub_10=@Sub
and Sub_10_stat=@sta Or Sub_11=@Sub
and Sub_11_stat=@sta Or Sub_12=@Sub
and Sub_12_stat=@sta";
after wards i'm displaying it in a data gridview.
But the problem is that the data grid view only has column names i.e Enrollment_No and Student_Name and there is no other data withing the grid refer image
I tried to execute the same query in SSMS and it gave me output e.g 4 enrollment No's and 4 Names.
But c# commandtext does not return any rows... What could be the problem???
Upvotes: 0
Views: 99
Reputation: 18737
You need to use brackets properly when combining AND
and OR
in WHERE
clause:
Try this:
fth.CommandText = "Select Enrollment_No, Student_Name
from Final_Result_Master
where (Sub_1=@Sub and Sub_1_stat=@sta)
or (Sub_2=@Sub and Sub_2_stat=@sta)
Or (Sub_3=@Sub and Sub_3_stat=@sta)
Or (Sub_4=@Sub and Sub_4_stat=@sta)
Or (Sub_5=@Sub and Sub_5_stat=@sta)
Or (Sub_6=@Sub and Sub_6_stat=@sta)
Or (Sub_7=@Sub and Sub_7_stat=@sta)
Or (Sub_8=@Sub and Sub_8_stat=@sta)
Or (Sub_9=@Sub and Sub_9_stat=@sta)
Or (Sub_10=@Sub and Sub_10_stat=@sta)
Or (Sub_11=@Sub and Sub_11_stat=@sta)
Or (Sub_12=@Sub and Sub_12_stat=@sta)";
When combining AND
and OR
conditions, it is important to use brackets so that the database knows what order to evaluate each condition. (Just like when you were learning the order of operations in Math class!).
Some examples here.
Upvotes: 2