Click here to Skip to main content
15,936,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HiI have a list box that is bound to a table in a database. it produces a list of companies. The user will then come along and select multiple companies that they want to view information about, and then they hit a selet button which ill display the company information (e.g company name, company site, address e.t.c) in a gridview underneath. however the issue that i am having is that it only displays ONE of the multiple companies selected and its always the top one.

Can someone please shed some light on how i get all the companies to be displayed in the gridview?

i am programming in

please see source code below

<asp:ListBox ID="ListBox1" runat="server"
            DataSourceID="SqlDataSource11" DataTextField="compName"
            DataValueField="compDataID" SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox>
        <asp:SqlDataSource ID="SqlDataSource11" runat="server"
            ConnectionString="<%$ ConnectionStrings:IWSRiskAssessmentConnectionString %>"
            SelectCommand="SELECT [compDataID], [compName] FROM [tblCompany] WHERE ([compDataID] &lt;&gt; @compDataID) ORDER BY [compName]">
                <asp:ControlParameter ControlID="DropDownList1" Name="compDataID"
                    PropertyName="SelectedValue" Type="Int32" />

            ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                <asp:BoundField DataField="compName" HeaderText="compName"
                    SortExpression="compName" />
                <asp:BoundField DataField="Site Name" HeaderText="Site Name"
                    SortExpression="Site Name" />
                <asp:BoundField DataField="Reference Number" HeaderText="Reference Number"
                    SortExpression="Reference Number" />
                <asp:BoundField DataField="Asset" HeaderText="Asset" ReadOnly="True"
                    SortExpression="Asset" />
                <asp:BoundField DataField="Location" HeaderText="Location"
                    SortExpression="Location" />
                <asp:BoundField DataField="Block" HeaderText="Block" SortExpression="Block" />
                <asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
            ConnectionString="<%$ ConnectionStrings:IWSRiskAssessmentConnectionString %>"
            SelectCommand="SELECT tblCompany.compName, tblSite.siteName AS [Site Name], tblSite.siteUPRN AS [Reference Number], tblIncMain.incAsset + ' ' + CAST(tblIncMain.incNumber AS varchar) AS Asset, tblIncMain.incLocation AS Location, tblIncMain.incBlock AS Block, tblIncMain.incRoom AS Room FROM tblIncMain INNER JOIN tblSite ON tblIncMain.incSite = tblSite.siteID INNER JOIN tblCompany ON tblSite.siteCompany = tblCompany.compDataID WHERE (tblIncMain.incActive = 1) AND (tblSite.siteActive = 1) AND (tblIncMain.incRemoved = 0) AND (tblCompany.compDataID = @compDataID) ORDER BY [Site Name], tblIncMain.incAsset, tblIncMain.incNumber">
                <asp:ControlParameter ControlID="ListBox1" Name="compDataID"
                    PropertyName="SelectedValueList" />

any help is much appreciated

Take a look at:
GridView Multiple Filter AJAX Control[^]
MultiDropDown v2: A multiple selection Dropdown control for ASP.NET[^]

Those two articles covers most of what you need to know...

Best regards
Espen Harlinn
Share this answer
Simon Bang Terkildsen 6-Oct-11 7:57am    
My 5
Espen Harlinn 6-Oct-11 10:24am    
Thank you, Simon!

Here I'm providing some code for your requirement.

Just check once this it can helps you to get solution

<asp:listbox id="ListBox1" runat="server" selectionmode="Multiple" xmlns:asp="#unknown">
    Height="176px" Width="130px" ></asp:listbox><br />
<asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" xmlns:asp="#unknown" /><br />
<asp:datalist id="DataList1" runat="server" xmlns:asp="#unknown">
 <HeaderTemplate >
    <table width="100%" align="center">
          <td>Company ID</td>
          <td>Company Name</td>
          <td>Company Addr</td>
          <td>Company Url</td>

          <td><![CDATA[<%#Eval("id") %>]]></td>
          <td><![CDATA[<%#Eval("compname") %>]]></td>
          <td><![CDATA[<%#Eval("compaddr") %>]]></td>
          <td><![CDATA[<%#Eval("compurl") %>]]></td>


And the code behind file contains
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
public void filllist()
    TestingDBLinqDataContext db = new TestingDBLinqDataContext();
    var d = from w in db.CompanyTabs
            select new {,cname=w.compname  };
    foreach (var h in d)
        ListBox1.Items.Add(new ListItem(h.cname, ()));

protected void Button1_Click(object sender, EventArgs e)
    int[] st=ListBox1.GetSelectedIndices ();
    string sd="";
    for(int i=0;i<st.length mode="hold" />        {
         int h=st[i];

        sd=ListBox1.Items [h].Value ;
           sd=sd+","+ListBox1.Items [h].Value ;

    SqlConnection con = new SqlConnection(@"dbconnectionstring");
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter("select * from CompanyTab where id in ("+sd+")", con);
    DataList1.DataSource = dt;


All the Best
Share this answer
reid0588 7-Oct-11 6:08am    
hi many thanks for this but i cannot use this as the companies that are entered into the list box may change. the list box is connected to a sql table on a database so when a company is added onto the table it will automatically be added to the listbox, if i was to enter the list manually i would have to enter the company into the list each time a new one was added and this cannot be the case

however...many thanks for your suggestion
Muralikrishna8811 7-Oct-11 6:13am    
you are welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900