Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
OleDbCommand cmd = new OleDbCommand("select * from patientdetail where refby=@refby AND datep BETWEEN @date1 AND @date2)", con);           
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = cmd;

            cmd.Parameters.Add(new OleDbParameter("@date1", dateTimePickerstart.Value.ToShortDateString()));
            cmd.Parameters.Add(new OleDbParameter("@date2", dateTimePickerend.Value.ToShortDateString()));
            cmd.Parameters.Add(new OleDbParameter("@refby", cmbboxdocname.Text)); 


I am trying to execute this code.. but this code give me error "Extra ) in query expression 'refby=@refby AND datep BETWEEN @date1 AND @date2)'."

'refby' is a doctor name in 'patientdetail' table. i want to search all records between date where doctor name (refby) match.

Please give me a proper solution for searching.

Sorry for my bad english Hope you understand what i am saying.

Thanks in advance.
Posted

The error message is straight forward. Your query has an extra ) towards the end.

This should be correct.
C#
OleDbCommand cmd = new OleDbCommand("select * from patientdetail where refby=@refby AND datep BETWEEN @date1 AND @date2", con);
 
Share this answer
 
Comments
varuncodee 24-Feb-12 8:33am    
Thank you very much sir.

But this query is not giving me desire result when i select dateTimePickerstart as 2/7/2011 and dateTimePickerend as 2/8/2011 and it return all records between Feb 8 to Feb 9 without matching doctor name (refby). can we use two AND operator in a query?
[no name] 24-Feb-12 20:27pm    
Absolutely
Try to remove ")" form:
C#
OleDbCommand cmd = new OleDbCommand("select * from patientdetail where refby=@refby AND datep BETWEEN @date1 AND @date2)", con); 

to look like this:
C#
OleDbCommand cmd = new OleDbCommand("select * from patientdetail where refby=@refby AND datep BETWEEN @date1 AND @date2", con); 
 
Share this answer
 
Comments
varuncodee 24-Feb-12 8:44am    
Thank you very much sir.

But this query is not giving me desire result when i select dateTimePickerstart as 2/7/2011 and dateTimePickerend as 2/8/2011 and it return all records between Feb 8 to Feb 9 without matching doctor name (refby). can we use two AND operator in a query?
Drazen Pupovac 24-Feb-12 9:05am    
From my experience your query is good.
You can improve him like this (but i think this will not change nothing):

select * from patientdetail where (refby=@refby) AND (datep BETWEEN @date1 AND @date2)

my recommendation is to use Management Studio (or some other tool) and execute query directly and you will see what's happening.
varuncodee 24-Feb-12 9:49am    
yes sir query is correct, actually datatype of "date" Field is set to "Text" that's why query was not giving me desired result but now it's working.
Thank alot for helping me :)
Your code:
OleDbCommand cmd = new OleDbCommand("select * from patientdetail where refby=@refby AND datep BETWEEN @date1 AND @date2)", con);


You seem to have added an extra ')' at the end of you selection. Remove it and it should work.

Try:
OleDbCommand cmd = new OleDbCommand("select * from patientdetail where refby=@refby AND datep BETWEEN @date1 AND @date2", con);
 
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