Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone
I have a problem with comma separated string. I have a database vendor detail which is as follows.
C#
ID       Vendor_Name    Asset_Type
37	Futuresoft	Motherboard,SMPS,
38	Future India	HDD,joystick,Laptop Screen,
39	Tech_M	        Baterry,Laptop Screen,Mouse,
46	dgd	        Battery,
47	Religare	HDD,joystick,Mouse,Processor,RAM,
48	ryy	        Battery,HDD,joystick,

I have a text box TextBox8 that contain Battery,Mouse. So i need to selcet the Vendor Name and bind it to a dropdownlist from Vendor detail that contains both Battery,Mouse present in TextBox. How to do it Please help.
My code is as follows
C#
public void bindddl()
 {
   //string serial = TextBox8.Text;
    // string[] s = serial.Split(',');
 
    // for (int i = 0; i < s.Length; i++)
     {
         //string s1 = s[i].ToString();
      
         string qry = "SELECT DISTINCT Vendor_Name FROM [VandorDetail] WHERE Asset_Type in ("+TextBox8.Text+"') ";
         SqlDataAdapter adpt = new SqlDataAdapter(qry, con);
         DataTable dt = new DataTable();
         adpt.Fill(dt);
         DropDownList4.DataSource = dt;
         DropDownList4.DataBind();
         DropDownList4.DataTextField = "Vendor_Name";
         DropDownList4.DataValueField = "Vendor_Name";
         con.Close();
     }

 }
Posted
Updated 3-Jul-14 23:22pm
v2
Comments
Prasad Avunoori 4-Jul-14 5:31am    
First of all your database design doesn't follow normalization rules.
Make it normalized.

1 solution

Please try following.

public void bindddl()
 {
   string serial = TextBox8.Text;
   string[] s = serial.Split(',');
 
     for (int i = 0; i < s.Length; i++)
     {
         string s1 = s[i].ToString();
      
         string qry = "SELECT DISTINCT Vendor_Name FROM [VandorDetail] WHERE Asset_Type like'%'+s1+'%';
         SqlDataAdapter adpt = new SqlDataAdapter(qry, con);
         DataTable dt = new DataTable();
         adpt.Fill(dt);
         DropDownList4.DataSource = dt;
         DropDownList4.DataBind();
         DropDownList4.DataTextField = "Vendor_Name";
         DropDownList4.DataValueField = "Vendor_Name";
         con.Close();
     }
 
 }
 
Share this answer
 
Comments
Member 10578683 4-Jul-14 5:41am    
It is showing error invalid column name S1
Prasad Avunoori 4-Jul-14 6:54am    
Try this.

string qry = "SELECT DISTINCT Vendor_Name FROM [VandorDetail] WHERE Asset_Type like '%"+s1+"%'";
Member 10578683 4-Jul-14 7:17am    
Actually i need that vendor name that contains the assets present in my textbox.
Prasad Avunoori 4-Jul-14 7:45am    
Then solution1 should work for you.

Why did you allow your table's columns to allow multiple values in one cell?
Member 10578683 4-Jul-14 8:54am    
I have to write a query that select the vendor names that contains Battery and mouse.like this. but i am unable to form this type query. Kindly help

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