get multiple values from listbox

i need help with a listbox - allow user to select multiple values and put values in a string to search sql table using c#
protected void btnSearch_Click(object sender, EventArgs e)
{
  int itemNumber = lstbxSelected.Items.Count;
  string sql = "";

  if(itemNumber > 1)
  {
    //run loop
    /sql = select * from mytable where name in ('tom','jon,'sue')
  }
  else
  {
    foreach (ListItem lstItem in lstbxSelected.Items)
    {
      lstItem.Selected = true;
    }
    sql = "select * from  [Jsa].[dbo].[t_syclin] where syclin_name = '" + lstbxSelected.SelectedItem + "'";
  }
}

Open in new window

vicominAsked:
Who is Participating?
 
iHadiConnect With a Mentor Commented:
               List<string> lst = new List<string>();
                foreach (ListItem lstItem in lstbxSelected.Items)
                {
                    if (lstItem.Selected)
                        lst.Add("'" + lstItem.Text + "'");
                }
                sql = "SELECT * FROM  [Jsa].[dbo].[t_syclin] WHERE syclin_name IN (" + string.Join(",", lst.ToArray()) + ")";
0
 
radcaesarCommented:
where syclin_name in <your array values in comma seperated values>
0
 
vicominAuthor Commented:
when i step through it shows a count of 0
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
iHadiCommented:
You must set the SelectionMode property of your listbox to Multiple and select some items before you click the search button
0
 
vicominAuthor Commented:
I've got the listbox set to multiple and did the following but still nothing

if (itemNumber > 1)
            {
                //run loop
                List<string> lst = new List<string>();
                foreach (ListItem lstItem in lstbxSelected.Items)
                {
                    if (lstItem.Selected)
                    {
                        lst.Add("'" + lstItem.Text + "'");
                    }
                    lstItem.Selected = true;
                }
0
 
iHadiCommented:
it's working perfectly with me, "when i step through it shows a count of 0", where are you getting the 0 ?
0
 
vicominAuthor Commented:
i select 3 and it shows it adds but the lst is always nothing

the sql reads:

SELECT * FROM  [Jsa].[dbo].[t_syclin] WHERE syclin_name IN ()

so there is nothing like:

SELECT * FROM  [Jsa].[dbo].[t_syclin] WHERE syclin_name IN ('tom', 'sue', 'steve')
0
 
vicominAuthor Commented:
if (itemNumber > 1)
            {
                //run loop
                List<string> lst = new List<string>();
                foreach (ListItem lstItem in lstbxSelectedSites.Items)
                {
                    if (lstItem.Selected)
                    {
                        lst.Add("'" + lstItem.Text + "'");
                    }
                    lstItem.Selected = true;
                }
                lst.Count(); // is 0
0
 
vicominAuthor Commented:
got it

                    //if (lstItem.Selected)
                    //{
                        lst.Add("'" + lstItem.Text + "'");
                    //}

                    //lstItem.Selected = true;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.