Solved

LISTBOX ISSUES

Posted on 2011-09-03
17
274 Views
Last Modified: 2012-05-12
The following markup has two listboxes.   One for station "sta" and the other employee "emp".   The employee box fills with data dependant upon the station selected.   Both controls are auto postback.  

I am having difficulty with the code behind for the Statiion "sta" dropdown control.   When I select a single station it populates the employee box correctly.   However, I need assistance with the code behind for a "Select All" statement
for the "sta" control that when selected will populate the listbox with all the stations listed personnel.
0
Comment
Question by:BOEING39
  • 8
  • 4
  • 2
  • +1
17 Comments
 
LVL 8

Expert Comment

by:Andrei Fomitchev
ID: 36480145
SELECT DISTINCT [Name] FROM [Results] WHERE ([Station] = ?) OR (1=?)

If you execute it with parameters (station Id,0) - it will return personal for the station
f you execute it with parameters (0,1) - it will return all personal


0
 

Author Comment

by:BOEING39
ID: 36480627
Are we taking about the select statement for "emp" correct?
0
 
LVL 2

Expert Comment

by:MoatazGad
ID: 36480864
If you know the index of "Select All" option within the list box, e.g. first in list box will have the index 0, then check if SelectedIndex == 0

OR

ini other way, check if SelectedText == "Select All" //string case insesitive

in both cases, if true, the the query should be SELECT [Name] FROM [Emp]

The query result could be then filled to a data table using a data adapter, then the data table could be set as data source for the list box.

Hope it works
0
 

Author Comment

by:BOEING39
ID: 36480922
The "Select All" is indexed to zero in "sta" which is a "ListItem".  The remainder of the items are tied to another DB  "personnel" & TBL "results" .   The "Select All" item is listed at "0" for the ListBox.   In the first exaple above are you saying in the Where caluse if the SelectedIndex == 0 to set it up as a Query Parameter?

If possible could show me how the code should look.   I have been orking this for a few days now and haven't gotten anywhere.   I am relatively new to this.    

<asp:ListBox runat="server" id="sta" DataValueField="Station" AppendDataBoundItems="true" DataTextField="Station" DataSourceID="AccessDataSource1" Width="82px" Height="127px" AutoPostBack="True" SelectionMode="Multiple">
<asp:ListItem>Select All "</asp:ListItem>
</asp:ListBox>
0
 
LVL 8

Expert Comment

by:Andrei Fomitchev
ID: 36480960
I was talking about

      <asp:AccessDataSource runat="server" ID="AccessDataSource3" DataFile="../Admin/personnel.mdb" SelectCommand="SELECT DISTINCT [Name] FROM [Results] WHERE ([Station] = ?)">
   
0
 

Author Comment

by:BOEING39
ID: 36482089
How would the Select parameter look?

/asp:DropDownList>
<asp:AccessDataSource runat="server" ID="AccessDataSource3" DataFile="../Admin/personnel.mdb" SelectCommand="SELECT DISTINCT [Name] FROM [Results] WHERE ([Station] = ?) OR (1=?)">

/<SelectParameters>
<asp:controlparameter ControlID="sta" PropertyName="SelectedValue" Name="Station" Type="String" />
/SelectParameters>
</asp:AccessDataSource>
0
 

Author Comment

by:BOEING39
ID: 36482108


I put this together no help.    

<asp:ListBox runat="server" id="sta" DataValueField="Station" AppendDataBoundItems="true" DataTextField="Station" DataSourceID="AccessDataSource1" Width="82px" Height="127px" AutoPostBack="True" SelectionMode="Multiple">
<asp:ListItem>Select All</asp:ListItem>
</asp:ListBox>


/asp:DropDownList>
<asp:AccessDataSource runat="server" ID="AccessDataSource3" DataFile="../Admin/personnel.mdb" SelectCommand="SELECT DISTINCT [Name] FROM [Results] WHERE ([Station] = ?) OR (1=?)">

<SelectParameters>
<asp:QueryStringParameter Name="Station" QueryStringField="(station Id,0)" Type="String" />
 <asp:QueryStringParameter Name="Station" QueryStringField="(0,1)" Type="String" />
                   
                        </SelectParameters>
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 36506893
Are you still having a problem?
0
 

Author Comment

by:BOEING39
ID: 36507521
Yes, I am not clear on how to setup the parameters on the Dropdown "emp" .   Previously it was explained I could use a where clause with the parameters.   I have a Where caluse setup when a station "sta" is selected the employees populate the ListBox for "emp".   That works fine but when all stations are selected "Select All" the ListBox does not populate with all the personnel.

*********************************************************************************************

SELECT DISTINCT [Name] FROM [Results] WHERE ([Station] = ?) OR (1=?)

If you execute it with parameters (station Id,0) - it will return personal for the station
f you execute it with parameters (0,1) - it will return all personal

**********************************************************************************************

I currently have:

<asp:ListBox runat="server" id="sta" DataValueField="Station" AppendDataBoundItems="true" DataTextField="Station" DataSourceID="AccessDataSource1" Width="82px" Height="127px" AutoPostBack="True" SelectionMode="Multiple">
<asp:ListItem>Select All</asp:ListItem>
</asp:ListBox>
                  
<asp:AccessDataSource runat="server" ID="AccessDataSource1" DataFile="../Admin/personnel.mdb" SelectCommand="SELECT DISTINCT [Station] FROM [Results]">
</asp:AccessDataSource>
                  &nbsp;</td>
            </tr>
            <tr>
                  <td class="style1" style="width: 150px">&nbsp;</td>
                  <td>&nbsp;</td>
            </tr>
            <tr>
                  <td class="style1" style="width: 150px">Employees:</td>
                  <td>
                  <asp:ListBox runat="server" id="emp" Width="156px" Height="179px" DataValueField="Name" AppendDataBoundItems="true" DataTextField="Name" DataSourceID="AccessDataSource3" AutoPostBack="True" SelectionMode="Multiple">
               
<asp:ListItem>Select All</asp:ListItem>
                        
</asp:ListBox>
                  <asp:AccessDataSource runat="server" ID="AccessDataSource3" DataFile="../Admin/personnel.mdb" SelectCommand="SELECT DISTINCT [Name] FROM [Results] WHERE ([Station] = ?)">
                        <SelectParameters>
                              <asp:controlparameter ControlID="sta" PropertyName="SelectedValue" Name="Station" Type="String" />
                        </SelectParameters>
                  </asp:AccessDataSource>






0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36507609
That sounds like you need to dynamically adjust the AccessDataSource parameters in the code-behind, but the exact place depends on the page life cycle.  My first guess would be to set the parameter QueryStringField in the Page.Load event handler, depending on the criteria that you just explained.
0
 

Author Comment

by:BOEING39
ID: 36507902
Would you be able to help me with this?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36513002
I would think something like this might get you started:

QueryStringParameter stationParameter = (QueryStringParameter)   AccessDataSource3.SelectParameters["Station"];        
stationParameter.QueryStringField = "(0,1)";

Open in new window

0
 

Author Comment

by:BOEING39
ID: 36514946
I placed the code in the following way but no help.





// Process the queries
            if ((emp.SelectedIndex != -1) && (emp.SelectedItem.Text == "Select All"))
            {
                foreach (ListItem employee in emp.Items)
                {
                    if (employee.Text != "Select All") // Ignore the "Select All" option
                    {
                        // Set the value of the @Emp parameter and execute the query for each employee
                        myCommand.Parameters["@Emp"].Value = employee.Text;
                        myCommand.ExecuteNonQuery();
                    }
                }
            }
            else if (emp.SelectedIndex != -1)
            {
                ListItem employee = emp.SelectedItem;

                // Insert into database for employee
                myCommand.Parameters["@Emp"].Value = employee.Text;
                myCommand.ExecuteNonQuery();
            }
            QueryStringParameter stationParameter = (QueryStringParameter)AccessDataSource3.SelectParameters["Station"];
            stationParameter.QueryStringField = "(0,1)";

            if ((sta.SelectedIndex != -1) && (sta.SelectedItem.Text == "Select All"))
               
                foreach (ListItem Station in sta.Items)
                {
                    if ((Station.Selected) && (Station.Text != "Select All"))
                    {
                        myCommand.Parameters["@sta"].Value = Station.Text;
                        myCommand.ExecuteNonQuery();
                    }
                }
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36517152
It is crucial that you get the right timing, and maybe that is not the correct approach.  Another approach is to handle the OnSelecting event, and work with the passed in OleDbCommand.

Programmatically accessing data from DataSource controls
http://www.mikesdotnetting.com/Article/45/Programmatically-accessing-data-from-DataSource-controls

protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["EmployeeID"].Value = 1;
}

Open in new window

0
 

Author Closing Comment

by:BOEING39
ID: 36895459
Thx, this worked out for me.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now