Click here to Skip to main content
15,886,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to make a login in main page and pass it to a querystring to another page..

i am wrong in generating a query

the logic i am using is as below..

C#
while (dr.Read())
{
   if(proid == String.Empty)
   {
      proid =  dr["ProductID"].ToString(); 
   }
   else
   {
      proid += "," + dr["ProductID"].ToString();
   }
}


and when i cal the query with request.querystring on another page i.e Productcatalog.aspx

C#
string query = "Select * from Products where ProductID in ('" + Request.QueryString["ID"].ToString() +"') AND UnitCost < 5000";


but the query generated is as below

C#
query	"Select * from Products where ProductID= ',63,64,65' AND UnitCost < 5000"	string


i want to generate the query as

C#
Select * from Products where ProductID in (63,64,65) AND UnitCost < 5000


what changes i have to make in above while loop. to generate the similar query.
Posted
Updated 28-Dec-11 7:31am
v2

One way to do the loop is
C#
while (dr.Read())
{
   proid = dr["ProductID"].ToString() + ", ";
}
proid.TrimEnd(' ', ',');

In more elegant (and faster) version you could use StringBuilder.
 
Share this answer
 
Comments
Espen Harlinn 28-Dec-11 14:03pm    
Nice reply :)
Wendelius 28-Dec-11 14:09pm    
Thanks :)
Try:

string query = "Select * from Products where ProductID in ('" + Request.QueryString["ID"].ToString().Substring(1) +"') AND UnitCost < 5000";


The problem lies in this statement:

proid += "," + dr["ProductID"].ToString();


You are putting a comma before every value that is returned. The substring will take everything after the first comma.
 
Share this answer
 
v2
Comments
codegeekalpha 28-Dec-11 11:15am    
its not removing the first comma... but its removing its generating query like that Select * from Products where ProductID= ',3,4,5' AND UnitCost < 5000" which is wrong..
Tim Groven 28-Dec-11 12:11pm    
Is there any other code executed after the "string query =" line that gets executed before you check the value of query?

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