Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

This is my program.
C#
try
{
con.open();
{
da=new oledbdataadapter("Select modelname from table where type='gate' and min<='"+textbox1.text+"' and max>='"+textbox1.text+"'",con);
ds.Fill(da,"main");
if(tables[0].rows.count>1)
{
datagridview1.datasource=ds;
datagridview1.datamember=main;
}
else
{
datagridview1.datasource=null;
}



Here i given min and max range for every model.If i give any number in textbox1 , it should compare the values between min and max range and gives the result..

For example;

Model Name : zeta,
min=200
max=250
type=gate

If i give 210, it should give respective model name which have comes in that range.

but it displays all fields which have 2,1,0,21,20,210,10.

please help me for this issue.
Posted
Updated 24-Sep-13 22:23pm
v2
Comments
Thanks7872 25-Sep-13 4:17am    
Don't repost the question. Wait for some one to answer your question and follow the original thread only.

General tips/hints:

1) In MS Access min and max is the name of function. It is strongly recommended to not use it as a name of fields, because some of words (for example type, table) are reserved words.
SQL Reserved Words (Access Developer Reference[^]
Problem names and reserved words in Access[^]


2) Do not concatenate query string in code. Use parameters!
PARAMETERS Declaration (Microsoft Access SQL)[^]
Everything About Using Parameters from Code (ADO & ADODB)[^]

Finally, your query should looks like:
SQL
Parameters [myVal] Integer
SELECT modelname
FROM [table] 
WHERE [type]='gate' and [min]<=[myVal] and [max]>=[myVal]

But, as i mentioned above, i would strongly recommend you to change the name of fields!

How to call above query? Have a look at OleDbCommand.Parameters[^] property.
 
Share this answer
 
Try this way. it should work assuming that data types for min & max are integer

da=new oledbdataadapter("Select modelname from table where type='gate' and " + textbox1.text +" between min and max",con);
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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