Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how wl i pass the variable here with related names like Tv or TTV or Coalall in place of TV and Coal

string commaDelimited = textBox1.Text;
string[] year = commaDelimited.Split(new char[] { });
          
/////////////////////////////////////////
//string[] r = { "TV", "Radio", "Coal" };
string filter = string.Empty;
foreach (string name in year)
{
    if (filter.Length > 0)
    {
        filter += ",";
    }
    filter += string.Format("'{0}'", 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 IN (" + filter + ")", con);
adp.Fill(ds, "Prod_Tbl");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
Posted
Updated 29-Mar-11 20:01pm
v2

To use like you have to use something like:

SQL
Select * from Prod_Tbl where ProductName like '%TV%' OR ProductName LIKE '%RADIO%';
 
Share this answer
 
v2
Comments
manasBonton 30-Mar-11 2:38am    
Sir ,but i want to take collection of item in the variable filter dynamically.if i wl take this ,can not change the value bcz it is constant
If you are looking for exact match then the code which you have written will work:

adp = new SqlDataAdapter("Select * from Prod_Tbl where ProductName IN (" + filter + ")", con);


But if the values which you are search are not exact values then you have to use the like operator.

something like
foreach (string name in year)
{    
if (filter.Length > 0)    
{       
 filter += "AND ";    
}    
filter += string.Format("ProductName  LIKE '{0}'", name);}


Another option:
http://www.eggheadcafe.com/software/aspnet/29398160/combine-in--like.aspx[^]
 
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