Click here to Skip to main content
15,918,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
OleDbCommand cmd = new OleDbCommand("select * from Salary where GPFCPF=? AND  NameOfUnit=? AND GPFcategory=? AND SDATE BETWEEN @date1 and @date2 AND PoNo1 <> ? OR  PoNo2 <> ? OR PoNo3 <> ? OR PoNo4 <> ? OR PoNo5 <> ? OR PoNo6 <> ? OR PoNo7 <> ? OR PoNo8 <> ?  ORDER BY SlipNo ASC", con);


Hi everyone,
I am passing parameter to this code and why i am getting result only on the basis of other condition except SDATE BETWEEN @date1 and @date2.
please help me to find out error or give me some solution or idea.
thank you.
Posted
Updated 21-Jun-15 6:56am
v2
Comments
Michael_Davies 21-Jun-15 12:49pm    
Please show the rest of the code, particularly the addparameters.
PIEBALDconsult 21-Jun-15 13:08pm    
There is no need to see the rest of the code; only the SQL statement is required.
varuncodee 21-Jun-15 13:16pm    
Sir i want to get result on the basis of date (SDATE BETWEEN @date1 and @date2)
other conditions are working fine expect date condition
PIEBALDconsult 21-Jun-15 12:58pm    
You will often find that when using both AND and OR, you will need to use parentheses to specify your required precedence.
https://msdn.microsoft.com/en-us/library/ms190276.aspx
varuncodee 21-Jun-15 13:36pm    
sir this code works, but i don't know why my previous query was not working, please tell me if you found what was the problem. thanks

OleDbCommand cmd = new OleDbCommand("select * from Salary where (GPFCPF=? AND NameOfUnit=? AND GPFcategory=? AND PoNo1 <> ? OR PoNo2 <> ? OR PoNo3 <> ? OR PoNo4 <> ? OR PoNo5 <> ? OR PoNo6 <> ? OR PoNo7 <> ? OR PoNo8 <> ? ) AND (SDATE BETWEEN @date1 and @date2) ORDER BY SlipNo ASC", con);

Yipee! I solved it through your help friends

I modify my code like this

OleDbCommand cmd = new OleDbCommand("select * from Salary where (GPFCPF=? AND NameOfUnit=? AND GPFcategory=? AND PoNo1 <> ? OR PoNo2 <> ? OR PoNo3 <> ? OR PoNo4 <> ? OR PoNo5 <> ? OR PoNo6 <> ? OR PoNo7 <> ? OR PoNo8 <> ? ) AND (SDATE BETWEEN @date1 and @date2) ORDER BY SlipNo ASC", con);

And it works.
Though i don't understand what was the problem with previous code in my question. if someone can tell me that would be very useful for future.
THanks to you Friends.
 
Share this answer
 
Comments
PIEBALDconsult 21-Jun-15 15:22pm    
Please don't answer your own question. Just use Improve Question to add the new query.
The parentheses are the BETWEEN are unnecessary; but it's the parentheses around the other part that matter. Your intent with this query is still unclear, so I recomend you add more parentheses to make it clear.
OleDbCommand cmd = new OleDbCommand("select * from Salary where GPFCPF=? AND NameOfUnit=? AND GPFcategory=? AND (SDATE BETWEEN @date1 and @date2 )AND (PoNo1 <> ? OR PoNo2 <> ? OR PoNo3 <> ? OR PoNo4 <> ? OR PoNo5 <> ? OR PoNo6 <> ? OR PoNo7 <> ? OR PoNo8 <> ?) ORDER BY SlipNo ASC", con);

This would work as analyzer requires seperation of conditions for reading purpose
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900