Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

LISTBOX ISSUES

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
BOEING39
Asked:
BOEING39
  • 8
  • 4
  • 2
  • +1
1 Solution
 
Andrei FomitchevCommented:
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
 
BOEING39Author Commented:
Are we taking about the select statement for "emp" correct?
0
 
MoatazGadCommented:
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
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.

 
BOEING39Author Commented:
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
 
Andrei FomitchevCommented:
I was talking about

      <asp:AccessDataSource runat="server" ID="AccessDataSource3" DataFile="../Admin/personnel.mdb" SelectCommand="SELECT DISTINCT [Name] FROM [Results] WHERE ([Station] = ?)">
   
0
 
BOEING39Author Commented:
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
 
BOEING39Author Commented:


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
 
Bob LearnedCommented:
Are you still having a problem?
0
 
BOEING39Author Commented:
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
 
Bob LearnedCommented:
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
 
BOEING39Author Commented:
Would you be able to help me with this?
0
 
Bob LearnedCommented:
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
 
BOEING39Author Commented:
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
 
Bob LearnedCommented:
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
 
BOEING39Author Commented:
Thx, this worked out for me.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 8
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now