• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

DDL selectIndexChanged Problem


I have three DDL's and 6 TextBoxes whcih gets populated when I change each ddl. In the page_Init event I load data in the ddls as follows.

   Dim sqlText As String = "Select ID, Name + ':' + Section as ProfileInfo from Profiles"
        If Not Page.IsPostBack Then
            ddlHandlerID.DataSource = General.GetDr(sqlText)
            ddlHandlerID.DataTextField = "ID"
            ddlHandlerID.DataValueField = "ProfileInfo"

            ddlRecID.DataSource = General.GetDr(sqlText)
            ddlRecID.DataTextField = "ID"
            ddlRecID.DataValueField = "ProfileInfo"

            ddlAuthID.DataSource = General.GetDr(sqlText)
            ddlAuthID.DataTextField = "ID"
            ddlAuthID.DataValueField = "ProfileInfo"
End Sub

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        QCardNo = Request.QueryString("JobCardNo")

        lblmessage.Visible = False
        Dim Con As New OleDbConnection(ConfigurationSettings.AppSettings("ConnectionString"))

        Dim strSQL As String = "SELECT Jobs.*, Profiles.ID as ID1,Profiles.Name as Name1,Profiles.Section as Section1, PRofiles_1.ID as ID2,Profiles_1.Name as Name2,Profiles_1.Section as Section2,Profiles_2.ID as ID3, Profiles_2.Name as Name3,Profiles_2.Section as Section3 FROM Profiles AS Profiles_2 RIGHT JOIN (Profiles AS Profiles_1 RIGHT JOIN (Profiles RIGHT JOIN JobIn ON Profiles.ID = JobIn.Handler) ON Profiles_1.ID = JobIn.RecvdBy) ON Profiles_2.ID = JobIn.AuthBy Where JobCardNo = '" & QCardNo & "'"
      Dim cmd As OleDbCommand = New OleDbCommand(strSQL, Con)

        If Not Page.IsPostBack Then

                If Con.State <> ConnectionState.Open Then
                End If

                Dim rdr As OleDbDataReader = cmd.ExecuteReader()
                If rdr.Read Then

                        Dim itemHandler As ListItem = ddlHandlerID.Items.FindByText(CStr(rdr.Item("ID1")))
                    If Not itemHandler Is Nothing Then
                        itemHandler.Selected = True
                    End If

                    Dim itemRec As ListItem = ddlRecID.Items.FindByText(CStr(rdr.Item("ID2")))
                    If Not itemRec Is Nothing Then
                        itemRec.Selected = True
                    End If

                    Dim itemAuth As ListItem = ddlAuthID.Items.FindByText(CStr(rdr.Item("ID3")))
                    If Not itemAuth Is Nothing Then
                        itemAuth.Selected = True
                    End If

                    txtHandlerName.Text = rdr.Item("Name1")
                    txtHandlerSection.Text = rdr.Item("Section1")

                    txtRecName.Text = rdr.Item("Name2")
                    txtRecSection.Text = rdr.Item("Section2")

                    txtAuthName.Text = rdr.Item("Name3")
                    txtAuthSection.Text = rdr.Item("Section3")
End Sub

 Private Sub ddlHandlerID_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlHandlerID.SelectedIndexChanged
        Dim str() As String
        If ddlHandlerID.SelectedIndex <> 0 Then
            str = ddlHandlerID.SelectedItem.Value.Split(":"c)
            txtHandlerName.Text = str(0)
            txtHandlerSection.Text = str(1)
            txtHandlerName.Text = ""
            txtHandlerSection.Text = ""
        End If
    End Sub

 Private Sub ddlRecID_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlRecID.SelectedIndexChanged
        Dim str2() As String
        If ddlRecID.SelectedIndex <> 0 Then
            str2 = ddlRecID.SelectedItem.Value.Split(":"c)
            txtRecName.Text = str2(0)
            txtRecSection.Text = str2(1)
            txtRecName.Text = ""
            txtRecSection.Text = ""
        End If
    End Sub

    Private Sub ddlAuthID_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlAuthID.SelectedIndexChanged
        Dim str3() As String
        If ddlAuthID.SelectedIndex <> 0 Then
            str3 = ddlAuthID.SelectedItem.Value.Split(":"c)
            txtAuthName.Text = str3(0)
            txtAuthSection.Text = str3(1)
            txtAuthName.Text = ""
            txtAuthSection.Text = ""
        End If
    End Sub

Suppose all three DDL fills with 10,20,30. and frist ddl's select item is 10,2nd's 20 and 3rd ddl's 30. Here when I change any of the 3 ddls, I get the textboxes populated with related information except for the item 10.When I select 10 in any ddl I get blank textboxes. What is wrong with the code? What is the correct way of doing it?

Thanks in advanc.e

1 Solution
When you hear "index" expecially in .net, start counting at 0 not 1

Your problem is here, ddlRecID.SelectedIndex <> 0
as you are excluding the first item, you need to change 0 to -1

Also, since you are working with a dropdownlist, you really will always have a selection, so this check is meaningless, there also is no way to enter the selectedindexchanged event without a valid selection.  I'd just take this out

Hope this helps,
ayha1999Author Commented:
Thank u very much for the help.


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now