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

This is my program.
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

1 solution

Hi there,

This behaviour could be caused because you're asking the database to return an integer value (the return value min/max from the database) based on a string (text) input. You could try to parse the textbox value to an integer like :

C#
int i = Convert.ToInt32(textbox1.Text.Trim());

and then feed that value to the database using an sql query (mind the quotes) :
C#
da=new oledbdataadapter("Select modelname from table where type='gate' and min <= i and max >= i",con);


also I notice a typo in your original query, not sure if that's a copy/paste issue but it will certainly return an inproper value if you use it like that. You forgot a couple of quotes :

da = new oledbdataadapter("SELECT modelname FROM table WHERE type='gate' AND min <= '" + textbox1.text + "' AND max >= '" + textbox1.text + "'",con);

The inaccurate use of quotes is responsible for 90% of invalid results of database queries or database query failures. Hope this helps
 
Share this answer
 
v2

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