?
Solved

get multiple values from listbox

Posted on 2011-05-09
9
Medium Priority
?
264 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:vicomin
  • 5
  • 3
9 Comments
 
LVL 9

Expert Comment

by:radcaesar
ID: 35721263
where syclin_name in <your array values in comma seperated values>
0
 
LVL 13

Accepted Solution

by:
iHadi earned 2000 total points
ID: 35721330
               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
 

Author Comment

by:vicomin
ID: 35721424
when i step through it shows a count of 0
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Expert Comment

by:iHadi
ID: 35721478
You must set the SelectionMode property of your listbox to Multiple and select some items before you click the search button
0
 

Author Comment

by:vicomin
ID: 35721621
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
 
LVL 13

Expert Comment

by:iHadi
ID: 35721904
it's working perfectly with me, "when i step through it shows a count of 0", where are you getting the 0 ?
0
 

Author Comment

by:vicomin
ID: 35722287
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
 

Author Comment

by:vicomin
ID: 35722308
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
 

Author Comment

by:vicomin
ID: 35722499
got it

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

                    //lstItem.Selected = true;
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question