[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using multiple select items from a list box

Posted on 2009-02-10
16
Medium Priority
?
721 Views
Last Modified: 2013-12-17
I have a stored procedure that takes multiple values of a variable this is working fine.

How do I select multiple values from a listbox and pass them to the stored procedure and display the results in a gridview?


0
Comment
Question by:leap29
  • 8
  • 5
  • 3
16 Comments
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598864
for(int i=0;i<listBox1.SelectedItems.Count;i++)
            {
                CallProc(listBox1.SelectedItems[i].ToString())
            }
0
 

Author Comment

by:leap29
ID: 23598900
can you explain that code please?
0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598912
it loops throgh the selected items (from the multiple items selected) and inside that you can calll the stored procedure repetaedly using the combobox value as

listBox1.SelectedItems[i].ToString()
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:leap29
ID: 23598935
Thanks for explaining that, I've tried to use the code but I get the following error
 
-CS0117: 'System.Web.UI.WebControls.ListBox' does not contain a definition for 'SelectedItems'
 
Please find my button click code below.

 protected void Button1_Click(object sender, EventArgs e)
    {
        string strConn = "";
        string jobtitle = TextBox1.Text;
        string location = ListBox1.SelectedItem.ToString();
              
        
 
 
        strConn = ConfigurationManager.ConnectionStrings["SQL03"].ConnectionString;
        SqlConnection cnn = new SqlConnection(strConn);
        cnn.Open();
        
 
        System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand("sp_leap29_jobdetails", cnn);
 
        SqlDataAdapter da = new SqlDataAdapter(objCmd);
        objCmd.CommandType = System.Data.CommandType.StoredProcedure;
        objCmd.Parameters.Add(new SqlParameter("@location", location));
        objCmd.Parameters.Add(new SqlParameter("@jobtitle", jobtitle));
       
 
 
        for(int i=0;i<ListBox1.SelectedItems.Count;i++)
           {
               CallProc(ListBox1.SelectedItems[i].ToString());
           }
 
        
        DataTable dt = new System.Data.DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
 
    }

Open in new window

0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598973
this will be your answer

 protected void Button1_Click(object sender, EventArgs e)
    {
        string strConn = "";
        string jobtitle = TextBox1.Text;
        
              
        
 DataTable dt = new System.Data.DataTable();
 
        strConn = ConfigurationManager.ConnectionStrings["SQL03"].ConnectionString;
        SqlConnection cnn = new SqlConnection(strConn);
        cnn.Open();
        
 for(int i=0;i<ListBox1.SelectedItems.Count;i++){
        System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand("sp_leap29_jobdetails", cnn);
 
        SqlDataAdapter da = new SqlDataAdapter(objCmd);
        objCmd.CommandType = System.Data.CommandType.StoredProcedure;
        string location = ListBox1.SelectedItems[i].ToString();
        objCmd.Parameters.Add(new SqlParameter("@location", location));
        objCmd.Parameters.Add(new SqlParameter("@jobtitle", jobtitle));
       
 
 
        
          
        
        
        da.Fill(dt);
}
        GridView1.DataSource = dt;
        GridView1.DataBind();
 
    }

Open in new window

0
 

Author Comment

by:leap29
ID: 23599023
02.10.2009 at 10:46AM GMT, ID: 23599007
I'm still getting the sdame error.  Any ideas where i've gone wrong?

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0117: 'System.Web.UI.WebControls.ListBox' does not contain a definition for 'SelectedItems'

Source Error:

Line 96: cnn.Open(); Line 97: Line 98: for (int i = 0; i < ListBox1.SelectedItems.Count; i++) Line 99: { Line 100: System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand("sp_leap29_jobdetails", cnn);
Source File: \\rdb1\testing\jobsearchtest.aspx.cs    Line: 98    
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 23599149
Because it is a web listbox you will need something like:
    foreach (ListItem item in ListBox1.Items)
    {
        if (item.Selected)
        {
            // do something with the item
        }
    }

Open in new window

0
 

Author Comment

by:leap29
ID: 23599305
Thanks for the reply I'm still having no joy.  Could you have a quick look at my code and tell me where I've gone wrong??
 
 

 protected void Button1_Click(object sender, EventArgs e)
    {
        string strConn = "";
        string jobtitle = TextBox1.Text;
        
 
 
 
        DataTable dt = new System.Data.DataTable();
 
        strConn = ConfigurationManager.ConnectionStrings["SQL03"].ConnectionString;
        SqlConnection cnn = new SqlConnection(strConn);
        cnn.Open();
 
 
 
 
        string countSelected = "";
 
            foreach (ListItem item in ListBox1.Items)
            {
 
                if (item.Selected)
                {
 
                    countSelected += 1;
 
                }
 
            }
 
           
            Response.Write(ListBox1.Items);               
            
            
            System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand("sp_leap29_jobdetails", cnn);
 
            SqlDataAdapter da = new SqlDataAdapter(objCmd);
            objCmd.CommandType = System.Data.CommandType.StoredProcedure;
            string location = ListBox1.Items.ToString();//countSelected;
            objCmd.Parameters.Add(new SqlParameter("@location", location));
            objCmd.Parameters.Add(new SqlParameter("@jobtitle", jobtitle));
 
 
 
 
 
 
 
        da.Fill(dt);
        
        GridView1.DataSource = dt;
        GridView1.DataBind();
 
    }
 

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 23599315
The code looks fine. What issues are you having?
0
 

Author Comment

by:leap29
ID: 23599325
It's not populating the gridview and I'm getting System.Web.UI.WebControls.ListItemCollection written to the screen.
This is driving me mad!  
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 23599356
Ok, you're getting "System.Web.UI.WebControls.ListItemCollection" written to screen because of the line:

    Response.Write(ListBox1.Items);

The "Items" property of the listbox has no override of the ToString() method so it outputs the objects type instead. I'm not quite sure what you were trying to achieve with this line.

Also, the line:

   string location = ListBox1.Items.ToString(); //countSelected;

Will be putting "System.Web.UI.WebControls.ListItemCollection" into the parameter for your SP.

What is it that you actually want to pass to the @location parameter?
0
 

Author Comment

by:leap29
ID: 23599377
we have a list box which is populated with several locations. the aim is that from the listbox the user can select multiple locations and these get passed to my SP and a list of results get returned.
 
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 23599422
How are you intending to pass multiple locations to the SP (comma-seperated list, etc)?
0
 

Author Comment

by:leap29
ID: 23599450
well this is what i am hoping you can help me with.
the stored procedure is working fine with multiple values through one parameter.
so i need a way of collecting the values from my listbox and pushing them into one parameter to the sp.
the sp is working with a function to remove commas from the parameter i push to it. so yes it would work best if there was a way to force the values from the listbox in a comma seperated string if possible.
 
hope that makes sense?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 23599631
Ok, in that case try something like:
    StringBuilder sb = new StringBuilder();
 
    foreach (ListItem item in ListBox1.Items)
    {
        if (item.Selected)
        {
            sb.AppendFormat("{0},", item.Value);
        }
    }
 
    string locations = sb.ToString().TrimEnd(new char[] { ',' });
 
    // now you can pass the "locations" variable as your parameter

Open in new window

0
 

Author Closing Comment

by:leap29
ID: 31544929
Brilliant! sorted many thanks!

Points increased
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

830 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