Hi,

I have two drop down lists boxes, upon page load first DDL gets populated and depending on user's selection on first DDL second DDL (child DDL) gets populated, works fine. My problem is when user goes back and makes another selection on first (parent) DDL (note that it's not a postback event, since user did not leave the page yet!! OR is that right? I am bit confused about the IsPostBack property),
So when selection in first DDL changes, it should automatically clear the Child DDL (which is still showing values for the first selection, which is deceiving)
When user actually clicks on a button to populate the Child DDL, it shows correct values for the new selection.
But when I use SelectedIndexChanged even on first DDL to clear Child DDL, it never gets triggered
This is my code
Any help is highly appreciated

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack() Then


            'Session("Current_Release_Id") = 1
  
            Dim MySQLString As String
            Dim objParam1 As SqlParameter
            Dim MyReader As SqlDataReader
            Dim MyConn As SqlConnection
            Dim Mycmd As SqlCommand
            Dim MyDataAdapter As SqlDataAdapter

            Dim cnt As Integer
            Dim str As String
            Dim MyDataSet As New DataSet


            Try

                MyConn = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))

                MySQLString = "Select DISTINCT PackageName , PackageId "
                MySQLString = MySQLString + " FROM Package "
                MySQLString = MySQLString + "WHERE (Package.ReleaseId = @PackageRelId) ORDER BY PackageName"

                MyDataAdapter = New SqlDataAdapter(MySQLString, MyConn)

                objParam1 = MyDataAdapter.SelectCommand.Parameters.Add("@PackageRelId", SqlDbType.Int)
                objParam1.Value = Rel_Matrix_Id.Text.Trim
                str = Rel_Matrix_Id.Text.Trim

                PackageNameDDL.Items.Clear()
                If MyConn.State = ConnectionState.Closed Then
                    MyConn.Open()
                End If


                cnt = MyDataAdapter.Fill(MyDataSet)

                If cnt = 0 Then
                    ComponentDDL.Visible = False
                    CompBtn.Visible = False
                    Say("No Packages are Added to this Release!!")
                    Exit Try
                End If

                ComponentDDL.Visible = True
                CompBtn.Visible = True


                'MyDataAdapter.Dispose()

                PackageNameDDL.DataSource = MyDataSet
                PackageNameDDL.DataTextField = "PackageName"
                PackageNameDDL.DataValueField = "PackageId"
                PackageNameDDL.DataBind()

                MyConn.Dispose()
                MyConn.Close()
            Catch ex As Exception
                Say("[PageLoad] Exception " & ex.Message)
                System.Diagnostics.Trace.WriteLine("[ShowBtnClick] Exception " & ex.Message)
            Finally
                MyConn.Close()
            End Try

        End If
    End Sub

First DDL is populated and now after click of a button second DDL gets populated

Private Sub CompBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompBtn.Click
        Dim MySQLString As String

        Dim objParam1 As SqlParameter
        Dim MyReader As SqlDataReader
        Dim MyConn As SqlConnection
        Dim Mycmd As SqlCommand
        Dim MyDataAdapter As SqlDataAdapter

        Dim cnt As Integer
        Dim str As String
        Dim MyDataSet As New DataSet


        Try

            MyConn = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))

            MySQLString = "Select DISTINCT ComponentID , Description  "
            MySQLString += " FROM Component"
            MySQLString += " WHERE (PackageID = @PackageId) ORDER BY Description"

            MyDataAdapter = New SqlDataAdapter(MySQLString, MyConn)

            objParam1 = MyDataAdapter.SelectCommand.Parameters.Add("@PackageId", SqlDbType.Int)
            objParam1.Value = PackageNameDDL.SelectedItem.Value.Trim

            ComponentDDL.Items.Clear()
            If MyConn.State = ConnectionState.Closed Then
                MyConn.Open()
            End If


            cnt = MyDataAdapter.Fill(MyDataSet)

            If cnt = 0 Then
                Say("No Components are Added to this Package!!")
                Exit Try
            End If

            'MyDataAdapter.Dispose()

            ComponentDDL.DataSource = MyDataSet
            ComponentDDL.DataTextField = "Description"
            ComponentDDL.DataValueField = "ComponentID"
            ComponentDDL.DataBind()



            MyConn.Dispose()
            MyConn.Close()
        Catch ex As Exception
            Say("[ShowComponent BtnClick] Exception " & ex.Message)
            System.Diagnostics.Trace.WriteLine("[ShowComponent BtnClick] Exception " & ex.Message)
        Finally
            MyConn.Close()
        End Try
    End Sub

works great... but what if user wants to change the package (first DDL) selection, my child DDL still shows old values until user clicks on the button again.. I tried to do something like this

Private Sub PackageNameDDL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PackageNameDDL.SelectedIndexChanged
        ComponentDDL.Items.Clear()
    End Sub

but the event never triggers, what am I doing wrong ?
:sad: :cry: :confused:

Recommended Answers

All 2 Replies

I have an very problem very similar to yours. Any helps from anyone? Thanks.

Hi,

I have two drop down lists boxes, upon page load first DDL gets populated and depending on user's selection on first DDL second DDL (child DDL) gets populated, works fine. My problem is when user goes back and makes another selection on first (parent) DDL (note that it's not a postback event, since user did not leave the page yet!! OR is that right? I am bit confused about the IsPostBack property),
So when selection in first DDL changes, it should automatically clear the Child DDL (which is still showing values for the first selection, which is deceiving)
When user actually clicks on a button to populate the Child DDL, it shows correct values for the new selection.
But when I use SelectedIndexChanged even on first DDL to clear Child DDL, it never gets triggered
This is my code
Any help is highly appreciated

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack() Then


            'Session("Current_Release_Id") = 1
  
            Dim MySQLString As String
            Dim objParam1 As SqlParameter
            Dim MyReader As SqlDataReader
            Dim MyConn As SqlConnection
            Dim Mycmd As SqlCommand
            Dim MyDataAdapter As SqlDataAdapter

            Dim cnt As Integer
            Dim str As String
            Dim MyDataSet As New DataSet


            Try

                MyConn = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))

                MySQLString = "Select DISTINCT PackageName , PackageId "
                MySQLString = MySQLString + " FROM Package "
                MySQLString = MySQLString + "WHERE (Package.ReleaseId = @PackageRelId) ORDER BY PackageName"

                MyDataAdapter = New SqlDataAdapter(MySQLString, MyConn)

                objParam1 = MyDataAdapter.SelectCommand.Parameters.Add("@PackageRelId", SqlDbType.Int)
                objParam1.Value = Rel_Matrix_Id.Text.Trim
                str = Rel_Matrix_Id.Text.Trim

                PackageNameDDL.Items.Clear()
                If MyConn.State = ConnectionState.Closed Then
                    MyConn.Open()
                End If


                cnt = MyDataAdapter.Fill(MyDataSet)

                If cnt = 0 Then
                    ComponentDDL.Visible = False
                    CompBtn.Visible = False
                    Say("No Packages are Added to this Release!!")
                    Exit Try
                End If

                ComponentDDL.Visible = True
                CompBtn.Visible = True


                'MyDataAdapter.Dispose()

                PackageNameDDL.DataSource = MyDataSet
                PackageNameDDL.DataTextField = "PackageName"
                PackageNameDDL.DataValueField = "PackageId"
                PackageNameDDL.DataBind()

                MyConn.Dispose()
                MyConn.Close()
            Catch ex As Exception
                Say("[PageLoad] Exception " & ex.Message)
                System.Diagnostics.Trace.WriteLine("[ShowBtnClick] Exception " & ex.Message)
            Finally
                MyConn.Close()
            End Try

        End If
    End Sub

First DDL is populated and now after click of a button second DDL gets populated

Private Sub CompBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompBtn.Click
        Dim MySQLString As String

        Dim objParam1 As SqlParameter
        Dim MyReader As SqlDataReader
        Dim MyConn As SqlConnection
        Dim Mycmd As SqlCommand
        Dim MyDataAdapter As SqlDataAdapter

        Dim cnt As Integer
        Dim str As String
        Dim MyDataSet As New DataSet


        Try

            MyConn = New SqlConnection(ConfigurationSettings.AppSettings("Conn"))

            MySQLString = "Select DISTINCT ComponentID , Description  "
            MySQLString += " FROM Component"
            MySQLString += " WHERE (PackageID = @PackageId) ORDER BY Description"

            MyDataAdapter = New SqlDataAdapter(MySQLString, MyConn)

            objParam1 = MyDataAdapter.SelectCommand.Parameters.Add("@PackageId", SqlDbType.Int)
            objParam1.Value = PackageNameDDL.SelectedItem.Value.Trim

            ComponentDDL.Items.Clear()
            If MyConn.State = ConnectionState.Closed Then
                MyConn.Open()
            End If


            cnt = MyDataAdapter.Fill(MyDataSet)

            If cnt = 0 Then
                Say("No Components are Added to this Package!!")
                Exit Try
            End If

            'MyDataAdapter.Dispose()

            ComponentDDL.DataSource = MyDataSet
            ComponentDDL.DataTextField = "Description"
            ComponentDDL.DataValueField = "ComponentID"
            ComponentDDL.DataBind()



            MyConn.Dispose()
            MyConn.Close()
        Catch ex As Exception
            Say("[ShowComponent BtnClick] Exception " & ex.Message)
            System.Diagnostics.Trace.WriteLine("[ShowComponent BtnClick] Exception " & ex.Message)
        Finally
            MyConn.Close()
        End Try
    End Sub

works great... but what if user wants to change the package (first DDL) selection, my child DDL still shows old values until user clicks on the button again.. I tried to do something like this

Private Sub PackageNameDDL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PackageNameDDL.SelectedIndexChanged
        ComponentDDL.Items.Clear()
    End Sub

but the event never triggers, what am I doing wrong ?
:sad: :cry: :confused:

Just for giggles, try and set the SelectedIndex = -1 which should be no item selected when you make a new selection from the parent and then re-run your connection string anytime the parent SelectedIndex changes ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.