List box Control and SqlDataSource

Last post 12-27-2006 11:49 AM by fpinder. 6 replies.

Sort Posts:

  • List box Control and SqlDataSource

    12-20-2006, 1:22 PM
    • Member
      point Member
    • fpinder
    • Member since 11-22-2006, 11:50 AM
    • Sacramento, CA
    • Posts 4

    Hello I have a list box control (SelectMode is multiple) and I'm populating the data Gridview.

    My problems is that the selected value that should be pass to the store procedure as to be a string in the format a 'string','string','string' . I have done the following but I get a error message in .net 2.0 say that the value is not part of the list items.

     

    My Code

    Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim objListItem As ListItem

    Dim strCounties As String = " "

    If ListBox1.SelectedIndex > -1 Then

    For Each objListItem In ListBox1.Items

    If objListItem.Selected Then

    strCounties = strCounties &

    "'" & objListItem.Text & "', "

    End If

    Next

    strCounties = Left(Trim(strCounties), Len(Trim(strCounties)) - 1)

    ListBox1.SelectedValue = strCounties

    End If

    End Sub

    ERROR on .net 2.0 

    System.ArgumentOutOfRangeException was unhandled by user code
      Message="'ListBox1' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value"
      ParamName="value"
      Source="System.Web"
      StackTrace:
           at System.Web.UI.WebControls.ListControl.set_SelectedValue(String value)
           at _Default.ListBox1_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\Project\listbox\Default.aspx.vb:line 17
           at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
           at System.Web.UI.WebControls.ListBox.RaisePostDataChangedEvent()
           at System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
           at System.Web.UI.Page.RaiseChangedEvents()
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
     

     

     

    See below store procedure to pupulate the Grid

    ALTER

    PROCEDURE [dbo].[uspLUTHospital_fp]

    @strCountyID varchar(500)

     

    AS

    exec ('SELECT HOSPITAL as HospitalName, Hospital HospitalID FROM HOCRawData WHERE county IN (' + @strCountyID + ') order by Hospital')

     

     

  • Re: List box Control and SqlDataSource

    12-20-2006, 2:23 PM
    • Contributor
      2,507 point Contributor
    • omerkamal
    • Member since 02-06-2006, 2:47 PM
    • Germany
    • Posts 513

    you are menipulating the value u selcted and then want to selct it again.

    check if the value stays same after this line

    strCounties = Left(Trim(strCounties), Len(Trim(strCounties)) - 1)

    I would prefer like this instead;

    int index=ListBox1.SelectedIndex

    If ListBox1.SelectedIndex > -1 Then

    For Each objListItem In ListBox1.Items

    If objListItem.Selected Then

    strCounties = strCounties &

    "'" & objListItem.Text & "', "

    End If

    Next

    strCounties = Left(Trim(strCounties), Len(Trim(strCounties)) - 1)

    ListBox1.SelectedIndex = index

    End If

     

    Regards,

    Omer Kamal

    www.dotnet-friends.com

  • Re: List box Control and SqlDataSource

    12-20-2006, 6:04 PM
    • Member
      point Member
    • fpinder
    • Member since 11-22-2006, 11:50 AM
    • Sacramento, CA
    • Posts 4

    Thank you for your response Omer!  I have been trying to get this to work over two weeks.

    Yes, I'm manipulating the value because of the store procedure. The Value that need to be pass as to have a 'string','string' 

    Example I Have the following in the list box (Alameda, Amador, Sacramento, Cuba) at the end the selectedValue as to be 'Alameda', 'Amador', 'Sacramento', 'Cuba'

    I see what you indicated however, what I'm trying to do is pass the select Value to be store procedure.

    Hope the preceding make sense if not I can explain again.

    Frank P.

     

  • Re: List box Control and SqlDataSource

    12-21-2006, 6:48 AM
    • Contributor
      2,507 point Contributor
    • omerkamal
    • Member since 02-06-2006, 2:47 PM
    • Germany
    • Posts 513

    If you can show how u passing the string value to stored proceedure then it would be more clear.

    Let me suggest u how can we change the code a bit;

    1. if u passing value to the stored proceedure, u need not to select it again in the Listbox.

    2. I dont see the reason for your "foreach loop"

    3. You dont need Event "SelectedIndexChanged" You can use PostBack Property of the listBox

     

    Please, see below the code;

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If (Page.IsPostBack) Then

    Dim strCont As String = ListBox1.SelectedValue

    MyProceedureFunction(strCont)

    ' this is my Function for passing selected Country

    End If

    End Sub

    Public Sub MyProceedureFunction(ByVal stCountry as String)

    'Data Menipulation here

    End Sub
  • Re: List box Control and SqlDataSource

    12-21-2006, 5:01 PM
    • Member
      point Member
    • fpinder
    • Member since 11-22-2006, 11:50 AM
    • Sacramento, CA
    • Posts 4

    Your example make sense and that is the way it was done in VS 2003 in VS2005 you don't have to create the connetion and pass the parameter because it's all done for you. I don't know if you are using the VS2005

    The value is pass some way in Sub SqlDataSourceDataGridView_Selecting which was created when I added the datasource.

    One more thing the problems is this when I added the string to the selected "ListBox1.SelectedValue = strCounties" I get the message say that it not part of the Listbox1 because I'm change that string to look like this ex. "'ALAMEDA'" but the select Item is just "ALAMEDA". However is I leave it like  this "ALAMEDA" I will get and error message because the store procedure as an in statement which require the strings to be in the following way  "'ALAMEDA'" with the single quotes.

    This is a lot of imformation but wanted to make sure I were conveying all the details Indifferent

    I added the control "SqlDataSourceDataGridView_Selecting" with the .net 2.0 Wizard and I select the  sqldatasource as  store procedure it ask me for the crontrol to use for the value that is pass to the store procedure therefore, I choose "listbox.selectedvlaue".

    One way to duplicate this is as follow:

    1)Added a listbox1 then use the Wizard to add the sqlDatasource in northwind database select * from table 
    2) Then add a Gridview  after that add the sqldatasource and select the store procedure (FIx it for your tables but leave the rest the same) as I indicated below.

    You will notice that the only code you will see is the following below (of course and the HTML aspx file).

    Partial

    Class _Default

    Inherits System.Web.UI.Page

     

    Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    ************************************ I ADDED THE FOLLOWING***********************************

    Dim objListItem As ListItem

    Dim strCounties As String = " "

    If ListBox1.SelectedIndex > -1 Then

    For Each objListItem In ListBox1.Items

    If objListItem.Selected Then

    strCounties = strCounties &

    "'" & objListItem.Text & "', "

    End If

    Next

    strCounties = Left(Trim(strCounties), Len(Trim(strCounties)) - 1)

    ListBox1.SelectedValue = strCounties

    'ListBox1.SelectedValue = ListBox1.SelectedItem.ToString

    End If

    ****************************END OF WHAT I ADDED******************************************

    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub

    Protected Sub SqlDataSourceListbox_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSourceListbox.Selecting

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub SqlDataSourceDataGridView_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSourceDataGridView.Selecting

    End Sub

    End

    Class
  • Re: List box Control and SqlDataSource

    12-21-2006, 6:14 PM
    • Contributor
      2,507 point Contributor
    • omerkamal
    • Member since 02-06-2006, 2:47 PM
    • Germany
    • Posts 513

    The code u copied is not doing any thing other then u trying to selecet the value again.

     To select the proper value ( as u said) remove '  '  sarrounding the menipulated string. Once again, The error u posted will be only resolve when u not try to select which is not in the Listbox.

     If you want to pass value to the stored procedure then you have to do yourself. Otherwise, if u have some code which passes the value to stored procedure please post.

    please, post your ASPX page also so it gets clear what your  SqlDataSource and GridView Code look like.

    one more thing,

     in your stored procedure you dont neeed to use EXEC It will execute it self when u use the SELECT satment.

    SELECT HOSPITAL as HospitalName, Hospital HospitalID FROM HOCRawData WHERE county= @strCountyID

     

  • Re: List box Control and SqlDataSource

    12-27-2006, 11:49 AM
    • Member
      point Member
    • fpinder
    • Member since 11-22-2006, 11:50 AM
    • Sacramento, CA
    • Posts 4

    I have added the ASPX code also I can not change the select statement because I need to get a list of all item. as an example the county colunm will have the following: see below if I want the address for the county where the county is Alameda, Manteca I will have to use the "county in ('Amador','Alameda'). Hence, the string @strCountyID pass as to be "'Amador','Alameda'"

    County       Name        Address
    Alameda
    Alameda
    Alameda
    Alameda
    Alameda
    Amador
    Manteca
    Manteca
    Manteca
    Alameda
    Amador
    Amador
    Amador

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <%@ Register Assembly="DundasWebChart" Namespace="Dundas.Charting.WebControl" TagPrefix="DCWC" %>

    <!

    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <

    html xmlns="http://www.w3.org/1999/xhtml" >

    <

    head runat="server">

    <title>Untitled Page</title>

    </

    head>

    <

    body>

    <form id="form1" runat="server">

    <div>

    <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourceListbox"

    DataTextField="CountyName" DataValueField="CountyName" AppendDataBoundItems="True" Rows="6" SelectionMode="Multiple"></asp:ListBox><asp:SqlDataSource

    ID="SqlDataSourceListbox" runat="server" ConnectionString="<%$ ConnectionStrings:HOCConnection %>"

    SelectCommand="SELECT CountyID, CountyName FROM dbo.LUTCounty WHERE (Voided = 0)"></asp:SqlDataSource>

    &nbsp;&nbsp;&nbsp;&nbsp;<br />

    <br />

    &nbsp;&nbsp; &nbsp; &nbsp;

     

    </div>

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" Height="276px" Width="216px" AllowSorting="True" DataSourceID="SqlDataSourceDataGridView">

    <Columns>

    <asp:BoundField DataField="HospitalName" HeaderText="HospitalName" SortExpression="HospitalName" />

    <asp:BoundField DataField="HospitalID" HeaderText="HospitalID" SortExpression="HospitalID" />

    </Columns>

    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSourceDataGridView" runat="server" ConnectionString="<%$ ConnectionStrings:HOCConnection %>"

    SelectCommand="uspLUTHospital_fp" SelectCommandType="StoredProcedure">

    <SelectParameters>

    <asp:ControlParameter ControlID="ListBox1" Name="strCountyID" PropertyName="SelectedValue"

    Type="String" />

    </SelectParameters>

    </asp:SqlDataSource>

    &nbsp;&nbsp;

    </form>

    </

    body>

    </

    html>
Page 1 of 1 (7 items)