Hi I am using the following code to populate text boxes based on a drop down list index change event

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As WinControls.UI.Data.PositionChangedEventArgs) Handles DropDownList1.SelectedIndexChanged

        Dim connString As String = My.Settings.strConn
        Dim conn As New SqlConnection(connString)
        Try
            conn.Open()
            Dim qry As String = "select * from property where propRef='" + RadDropDownList1.SelectedText + "'"

            Dim cmd As New SqlCommand(qry, conn)

            Dim dr As SqlDataReader = cmd.ExecuteReader()
            If dr.Read() Then
                TextBoxHouseNo.Text = dr(3)
                TextBoxStreet.Text = dr(4)
                TextBoxCity.Text = dr(5)
                TextBoxPostCode.Text = dr(6)
                TextBoxRent.Text = dr(12)
            End If
            dr.Close()
        Catch ex As Exception

        Finally

            conn.Close()
        End Try

    End Sub

The code works until it gets to the last item if there is only one item in the dropdown box it does not populate the text boxes, if I loop through the code it is going to the droplist index change but it goes from dr.Read() and just jumps to dr.close() without populating the text boxes?

any suggestions

tahnks

M

Dani AI

Generated

Two quick issues explain the behavior seen in the thread: a control-name/selection-property mismatch and reliance on SelectedText (which is only the actively highlighted text, so it can be empty until the user clicks). was on the right track suggesting .Text, and 's suggestion to log what is actually selected helps confirm the problem.

Checklist to diagnose and fix (run these in order):

  • Confirm the same control instance is referenced everywhere (the handler shows DropDownList1 but the query used RadDropDownList1).
  • After binding, check DropDownList1.SelectedItem IsNot Nothing, DropDownList1.Text and DropDownList1.SelectedValue (log or breakpoint). If ValueMember was set (p_id), prefer SelectedValue over text matching.
  • Call the populate routine explicitly immediately after the DataSource is filled (do not rely only on the selection event firing during binding). The GotFocus workaround indicates the event fires only after user interaction because SelectedText was empty initially.

Robust populate routine (call this from SelectedIndexChanged and immediately after binding):

Private Sub PopulateFromSelection()
    Dim key As String = If(DropDownList1.SelectedItem IsNot Nothing, DropDownList1.Text, String.Empty)
    If String.IsNullOrEmpty(key) Then Exit Sub

    Using conn As New SqlConnection(My.Settings.strConn)
        conn.Open()
        Using cmd As New SqlCommand("SELECT houseNo, street, city, postCode, rent FROM property WHERE propRef = @ref", conn)
            cmd.Parameters.AddWithValue("@ref", key)
            Using dr As SqlDataReader = cmd.ExecuteReader()
                If dr.Read() Then
                    TextBoxHouseNo.Text = If(IsDBNull(dr("houseNo")), "", dr("houseNo").ToString())
                    TextBoxStreet.Text = If(IsDBNull(dr("street")), "", dr("street").ToString())
                    TextBoxCity.Text = If(IsDBNull(dr("city")), "", dr("city").ToString())
                    TextBoxPostCode.Text = If(IsDBNull(dr("postCode")), "", dr("postCode").ToString())
                    TextBoxRent.Text = If(IsDBNull(dr("rent")), "", dr("rent").ToString())
                End If
            End Using
        End Using
    End Using
End Sub

Notes and cautions: prefer parameterized queries (shown) and query by the numeric p_id (SelectedValue) when possible to avoid string matching issues. Avoid empty Catch blocks — log exceptions so binding/SQL errors don't get swallowed.

Recommended Answers

All 7 Replies

What will be selected text ur getting when only one item in textbox?
Just take that value and try to run on database check to see if there are records for it.

Dim qry As String = "select * from property where propRef='" + RadDropDownList1.SelectedText + "'"

Chang it to

Dim qry As String = "select * from property where propRef='" + RadDropDownList1.Text + "'"

If any other problem . let me know....

Thanks for the response but that didn't resolve my problem

I am using this to populate the dropdownlist

Dim connString As String = My.Settings.strConn
        Dim conn As New SqlConnection(connString)
        Try
            conn.Open()
            Dim strSQL As String = "SELECT * FROM property WHERE (available = 1)"
            Dim da As New SqlDataAdapter(strSQL, conn)
            Dim ds As New DataSet
            da.Fill(ds, "property")

            With DropDownList1
                .DataSource = ds.Tables("property")
                .DisplayMember = "propRef"
                .ValueMember = "p_id"
                .SelectedIndex = 0

            End With
        Catch ex As Exception

        Finally
            'conn.Dispose()
            conn.Close()
        End Try

I have tried putting this code on the page load event and on button click event either works the same way.

Ok I am able to get it to work using the GotFocus event it is not populating by itself I still have to click the dropDownList to get it to populate the textBoxes it seems a little strange that it does not do this on an event?

First test is your dataset filed with data or not....

I mean is dataadapter fill your dataset???

Yes on page load

Me.TenantTableAdapter.Fill(Me.PropertyDataSet.tenant)
        'TODO: This line of code loads data into the 'PropertyDataSet._property' table. You can move, or remove it, as needed.
        Me.PropertyTableAdapter.Fill(Me.PropertyDataSet._property)
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.