ahmed salah

ahmed salah

  • NA
  • 530
  • 146.9k

How to select data from database based on multi select list

May 2 2017 9:03 AM

I work in c# windows form vs2015

Problem

How to select multiple item from database based on list box multiple select ?

Details

I set listbox 1 control property selection mode to Multi Simple

code below select only one item selected from list box and work without any problem but select only one item

but if i need to multi select for list box How to changes my code below
  1. public DataTable ShowaSpecificSerial(string SerialNo)  
  2.         {  
  3.   
  4.             SqlConnection con = new SqlConnection(connection);  
  5.   
  6.   
  7.             con.Open();  
  8.             string str = "select * from View_showdata where SerialNo=@SerialNo";  
  9.             SqlCommand com = new SqlCommand();  
  10.             com = new SqlCommand(str, con);  
  11.             com.Parameters.AddWithValue("@SerialNo", SerialNo);  
  12.             SqlDataAdapter oledbda = new SqlDataAdapter();  
  13.             oledbda = new SqlDataAdapter(com);  
  14.             DataSet ds = new DataSet();  
  15.             ds = new DataSet();  
  16.             oledbda.Fill(ds, "View_showdata");  
  17.             con.Close();  
  18.             DataTable dt = new DataTable();  
  19.             dt = ds.Tables["View_showdata"];  
  20.             return dt;  
  21.   
  22.   
  23.         }public DataTable ShowaSpecificSerial(string SerialNo)  
  24.         {  
  25.   
  26.             SqlConnection con = new SqlConnection(connection);  
  27.   
  28.   
  29.             con.Open();  
  30.             string str = "select * from View_showdata where SerialNo=@SerialNo";  
  31.             SqlCommand com = new SqlCommand();  
  32.             com = new SqlCommand(str, con);  
  33.             com.Parameters.AddWithValue("@SerialNo", SerialNo);  
  34.             SqlDataAdapter oledbda = new SqlDataAdapter();  
  35.             oledbda = new SqlDataAdapter(com);  
  36.             DataSet ds = new DataSet();  
  37.             ds = new DataSet();  
  38.             oledbda.Fill(ds, "View_showdata");  
  39.             con.Close();  
  40.             DataTable dt = new DataTable();  
  41.             dt = ds.Tables["View_showdata"];  
  42.             return dt;  
  43.   
  44.   
  45.         }  
under button i write following
  1. private void button5_Click(object sender, EventArgs e)  
  2.        {  
  3.            string root = @"D:\Temp";  
  4.   
  5.   
  6.            if (!Directory.Exists(root))  
  7.            {  
  8.   
  9.                Directory.CreateDirectory(root);  
  10.   
  11.            }  
  12.            Class1 CLS = new Class1();  
  13.            DataTable dt = CLS.ShowaSpecificSerial(listBox3.SelectedValue.ToString());  
  14.             
  15.            for (int i = 0; i <= Convert.ToInt32(dt.Rows.Count) - 1; i++)  
  16.            {  
  17.                txt = "UserID" + dt.Rows[i][0] + "ProductNo" + dt.Rows[i][1] + "Firm Name" + dt.Rows[i][2] + "BtachNo" + dt.Rows[i][3] + "SerialNo" + dt.Rows[i][4];  
  18.                 
  19.                dm.DM(txt, Color.FromName(comboBox1.SelectedItem.ToString()), Color.White).Save(root + "\\" + dt.Rows[i][4] + ".emf", System.Drawing.Imaging.ImageFormat.Emf);  
  20.            }  
  21.        }  
 

Sample data to view View_showdata :

UserID Product Firm BatchNo SerialNo

Ali ParasitemolFarco 2099 000055
AliColdFlueFarco 2998 01112
AliColdFlueFarco 2998 08888866

    

Result

suppose i make multi select to list box to serial no to 000055 and 08888866

the result must done is create two files image datamatrix with name 000055 and 08888866 in E : /Temp

and when read it by scanner or mobile read data following to every serial selected

file name 08888866 when read it the data must have

 
AliColdFlueFarco 2998 08888866

so that How to change my code above to accept multi select from database also

according to list box it accept multi select but not from database


Answers (8)