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:
<asp:DropDownList ID="ddlDBs" runat="server" AutoPostBack="True" Width="192px"> </asp:DropDownList><br />
The code to populate
vb.net Code:
Protected Sub getDatabaseNames() Dim oDA As SqlClient.SqlDataAdapter Dim oDS As DataSet = New DataSet Dim oCmd As SqlClient.SqlCommand = New SqlClient.SqlCommand Try Dim SQL As String = String.Empty SQL = "Select Name From sys.databases Where Name NOT IN ('master','msdb','model','tempdb') Order by Name" If Me.sqlConnection.State = ConnectionState.Closed Then Me.openConn() End If oDA = New SqlClient.SqlDataAdapter(SQL, Me.sqlConnection) oDA.Fill(oDS) Me.ddlDBs.DataSource = oDS.Tables(0).DefaultView Me.ddlDBs.DataTextField = "Name" Me.ddlDBs.DataBind() Me.ddlDBs.ClearSelection() Catch ex As Exception Me.lblMessages.Text = ex.Message Finally If oDS IsNot Nothing Then oDS.Dispose() End If If oCmd IsNot Nothing Then oCmd.Dispose() End If End Try If Me.sqlConnection.State = ConnectionState.Open Then Me.closeConn() End If 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:
Protected Sub ddlDBs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlDBs.SelectedIndexChanged Response.Write("The database selected is: " & Me.ddlDBs.SelectedItem.Text & System.Environment.NewLine) 'Response.Write("The database selected is: " & Me.ddlDBs.SelectedIndex) End Sub


Reply With Quote
