Results 1 to 4 of 4

Thread: Cascading dropdownlist

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    4

    Cascading dropdownlist

    Hi All, I'm trying to create a web based form. couple of fields I need it as cascading. First list is is GP Surgery and second list is GP Name. GP Surgery is a table from SQL server and GPName is a stored procedure. What I want is, when I select a GP Surgery, I want the second list to show only the GPs that work in the selected GP Surgery.
    Here is the code iI used dto get the GP Surgeries - THIS WORKS FINE
    Public Function PopulateGPSurgeryDropdown()
    Dim costrnBedBureau = ConfigurationManager.ConnectionStrings("Servername").ConnectionString

    Try
    DDLGPSurgery.Items.Clear()

    Dim conGPSurgery As New SqlConnection(costrnBedBureau)

    'Populate GPSurgery drop down list
    Dim sqlString As String = "SELECT [Organisation Code],[Organisation Name]FROM [Server_Test].[dbo].[General Medical Practice] order by [Organisation Name] asc"
    Dim cmdGetGPSurgery As SqlCommand = New SqlCommand(sqlString, conGPSurgery)
    cmdGetGPSurgery.CommandType = CommandType.Text


    conGPSurgery.Open()
    Dim drGPSurgery As SqlDataReader
    drGPSurgery = cmdGetGPSurgery.ExecuteReader
    DDLGPSurgery.DataSource = drGPSurgery
    DDLGPSurgery.DataTextField = "Organisation Name"
    DDLGPSurgery.DataValueField = "Organisation Code"
    DDLGPSurgery.DataBind()
    conGPSurgery.Close()

    DDLGPSurgery.Items.Insert(0, "")

    cmdGetGPSurgery.Dispose()

    Catch ex As Exception

    ' HandleSystemException(System.Reflection.MethodBase.GetCurrentMethod.DeclaringType, System.Reflection.MethodBase.GetCurrentMethod(), ex)

    End Try

    End Function

    Here is the code i use for GPName: THIS DOESNT populate the list.
    Public Function PopulateGPNameDropdown(ByRef DDLGPName As Web.UI.WebControls.DropDownList, ByVal lngOrganisationcode As Char)

    DDLGPName.Items.Clear()
    DDLGPName.Items.Add(New ListItem("--Select GP--", ""))

    Dim constrnBedBureau = ConfigurationManager.ConnectionStrings("Server").ConnectionString

    'Update the GPName list.

    Try
    DDLGPName.Items.Clear()
    Dim conGPName As New SqlConnection(constrnBedBureau)
    Dim cmdGetGPName As SqlCommand = New SqlCommand("[dbo].[usp_GPName]", conGPName) With {
    .CommandType = CommandType.StoredProcedure
    }

    Dim paramParent As SqlClient.SqlParameter
    paramParent = New SqlParameter
    paramParent.ParameterName = "@OrganisationCode"
    paramParent.SqlDbType = SqlDbType.Text
    paramParent.Value = lngOrganisationcode
    cmdGetGPName.Parameters.Add(paramParent)

    conGPName.Open()
    Dim drGPName As SqlDataReader
    drGPName = cmdGetGPName.ExecuteReader
    DDLGPName.DataSource = drGPName
    DDLGPName.DataTextField = "FullName"
    DDLGPName.DataValueField = "GP.GP_Nat_Code"
    DDLGPName.DataBind()
    conGPName.Close()


    Catch ex As Exception
    'HandleSystemException(System.Reflection.MethodBase.GetCurrentMethod.DeclaringType, System.Reflection.MethodBase.GetCurrentMethod(), ex)

    End Try


    End Function

    Protected Sub DDLGPSurgery_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDLGPSurgery.SelectedIndexChanged
    PopulateGPNameDropdown()

    End Sub


    Im' new to programming, so im at a clueless on what is wrong.

    Please advice.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,388

    Re: Cascading dropdownlist

    Your sub that calls the stored procedure: PopulateGPNameDropdown is expecting two paramters:
    Code:
    Public Function PopulateGPNameDropdown(ByRef DDLGPName As Web.UI.WebControls.DropDownList, ByVal lngOrganisationcode As Char)
    But when you call it, you don't give it any:
    Code:
    PopulateGPNameDropdown()
    That's why it isn't working. honestly I'm a bit surprised you're not getting an error. You need to pass it the DropDownList you plan to populate, and the Id of the selected item from the parent drop down.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    4

    Re: Cascading dropdownlist

    Quote Originally Posted by techgnome View Post
    Your sub that calls the stored procedure: PopulateGPNameDropdown is expecting two paramters:
    Code:
    Public Function PopulateGPNameDropdown(ByRef DDLGPName As Web.UI.WebControls.DropDownList, ByVal lngOrganisationcode As Char)
    But when you call it, you don't give it any:
    Code:
    PopulateGPNameDropdown()
    That's why it isn't working. honestly I'm a bit surprised you're not getting an error. You need to pass it the DropDownList you plan to populate, and the Id of the selected item from the parent drop down.

    -tg
    Hi, Thank you for your response. so how does the code look like? I'm not too sure what to put to be honest. And no, I didnt get any errors. Thanks

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    4

    Re: Cascading dropdownlist

    I've changed it to this.

    Protected Sub DDLGPSurgery_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDLGPSurgery.SelectedIndexChanged
    PopulateGPNameDropDown(Me.DDLGPName, Me.DDLGPSurgery.SelectedItem.Value)
    End Sub

    But it still doesnt populate the relevant drop down.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width