Results 1 to 4 of 4

Thread: DropDownList Issue

Threaded View

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    DropDownList Issue

    Hi All

    I'm trying to learn ASP.Net (VB)

    I'm having a question on a dropdown list. I have populated the list using a dataset ( a list of databases).

    The dropdown list code
    asp.net Code:
    1. <asp:DropDownList ID="ddlDBs" runat="server" AutoPostBack="True" Width="192px">
    2.         </asp:DropDownList><br />

    The code to populate
    vb.net Code:
    1. Protected Sub getDatabaseNames()
    2.         Dim oDA As SqlClient.SqlDataAdapter
    3.         Dim oDS As DataSet = New DataSet
    4.         Dim oCmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
    5.         Try
    6.             Dim SQL As String = String.Empty
    7.             SQL = "Select Name From sys.databases Where Name NOT IN ('master','msdb','model','tempdb') Order by Name"
    8.  
    9.             If Me.sqlConnection.State = ConnectionState.Closed Then
    10.                 Me.openConn()
    11.             End If
    12.             oDA = New SqlClient.SqlDataAdapter(SQL, Me.sqlConnection)
    13.            
    14.             oDA.Fill(oDS)
    15.             Me.ddlDBs.DataSource = oDS.Tables(0).DefaultView
    16.             Me.ddlDBs.DataTextField = "Name"
    17.             Me.ddlDBs.DataBind()
    18.             Me.ddlDBs.ClearSelection()
    19.  
    20.         Catch ex As Exception
    21.             Me.lblMessages.Text = ex.Message
    22.         Finally
    23.             If oDS IsNot Nothing Then
    24.                 oDS.Dispose()
    25.             End If
    26.             If oCmd IsNot Nothing Then
    27.                 oCmd.Dispose()
    28.             End If
    29.         End Try
    30.         If Me.sqlConnection.State = ConnectionState.Open Then
    31.             Me.closeConn()
    32.         End If
    33.  
    34.     End Sub

    This part works. I get a list of databases in the list. My problem is when I make a selection from the list. I always get the first item in the list as selected not what I actually select.

    Here is the code for the selected index change
    vb.net Code:
    1. Protected Sub ddlDBs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlDBs.SelectedIndexChanged
    2.         Response.Write("The database selected is: " & Me.ddlDBs.SelectedItem.Text & System.Environment.NewLine)
    3.         'Response.Write("The database selected is: " & Me.ddlDBs.SelectedIndex)
    4.     End Sub
    Last edited by GaryMazzone; Nov 14th, 2009 at 02:43 PM. Reason: Highligting
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width