Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Respected Sir,
i want to filtrate collection of items that stored in string array variable and pass this variable in where clause but that filtrate only last value like here 'Coal'.not filtrate all variable members
my query is like this///////////////

 string[] r = { "TV", "Radio", "Coal" };
foreach (string name in r)
{
  // MessageBox.Show(name);
 SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=ERPDB;Integrated Security=True");
 SqlDataAdapter adp;
 DataSet ds = new DataSet();
 adp = new SqlDataAdapter("Select * from Prod_Tbl where ProductName='+name+'"), con);
 adp.Fill(ds, "Prod_Tbl");
 dataGridView1.DataSource = ds.Tables[0].DefaultView;
 }
Posted
Updated 28-Mar-11 19:32pm
v2
Comments
manasBonton 29-Mar-11 1:46am    
I have tried aleardy "Select * from Prod_Tbl where ProductName='" + name + "'"
Venkatesh Mookkan 29-Mar-11 2:28am    
Answer has been updated. check it out

Correction:

SQL
"Select * from Prod_Tbl where ProductName='" + name + "'"


Update:
I haven't read your answer properly.

You should not load the DataGridView on the loop. Remove it from the loop and place it outside. Change the foreach to below one.

C#
string filter = string.Empty;
foreach (string name in r)
{
  if (filter.Length > 0)
    filter +=",";
  filter += string.Format("'{0}'",name);
}


Now try below:

SQL
"Select * from Prod_Tbl where ProductName IN (" + filter + ")"
 
Share this answer
 
v2
Comments
m@dhu 29-Mar-11 2:18am    
That still gives the last element of the array to filter.
Venkatesh Mookkan 29-Mar-11 2:27am    
I miss understood the question. Answer has been updated.
manasBonton 29-Mar-11 2:44am    
Thank you so much...........
it is working now.
This dirty code will work but this is not the best solution. You got to handle this in db itself.
C#
string[] r = { "TV", "Radio", "Coal" };
for (int i = 0; i < r.Length; i++)
            {
                ir +="ProductName = "+ r[i] + " or ";

            }
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=ERPDB;Integrated Security=True");
            SqlDataAdapter adp;
            DataSet ds = new DataSet();
            adp = new SqlDataAdapter("Select * from Prod_Tbl where "+ir+""), con);
            adp.Fill(ds, "Prod_Tbl");
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
 
Share this answer
 
v2
Comments
Venkatesh Mookkan 29-Mar-11 2:27am    
IN clause would be better than OR ;)
manasBonton 29-Mar-11 2:34am    
Incorrect syntax near '='. displaying
m@dhu 29-Mar-11 2:45am    
As the name is varchar you got to place in quote replace this statement. Its better to use inclause as said by venkatesh.
ir +="ProductName = '"+ r[i] + "' or ";

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