• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 724
  • Last Modified:

Using multiple select items from a list box

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
leap29
Asked:
leap29
  • 8
  • 5
  • 3
1 Solution
 
Praveen VenuTechnical Project ManagerCommented:
for(int i=0;i<listBox1.SelectedItems.Count;i++)
            {
                CallProc(listBox1.SelectedItems[i].ToString())
            }
0
 
leap29Author Commented:
can you explain that code please?
0
 
Praveen VenuTechnical Project ManagerCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
leap29Author Commented:
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
 
Praveen VenuTechnical Project ManagerCommented:
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
 
leap29Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
leap29Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
The code looks fine. What issues are you having?
0
 
leap29Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
leap29Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
How are you intending to pass multiple locations to the SP (comma-seperated list, etc)?
0
 
leap29Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
leap29Author Commented:
Brilliant! sorted many thanks!

Points increased
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 8
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now