locked
Cascading Dropdownlist in vb.net webform RRS feed

  • Question

  • User-1416261966 posted

    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.

    Thursday, May 13, 2021 10:24 AM

All replies