DDL selectIndexChanged Problem

Posted on 2005-04-30
Last Modified: 2010-04-07

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

Question by:ayha1999
    LVL 33

    Accepted 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,
    LVL 7

    Author Comment

    Thank u very much for the help.


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now