drop down list add

rangerbud249

Active member
Joined
Aug 18, 2004
Messages
27
Programming Experience
Beginner
I have a drop down list that gets populated by a database. I would like for the first thing to show in the dropdown list to be ((SELECT ALL))

I have some code but it adds ((SELECT ALL )) at the bottom of my dropdownlist and I want it at the beginning.

<my code>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
lstAircraft.DataSource = GetAircraft() 'this function will load the Tail Numbers
lstAircraft.DataValueField = "ID"
lstAircraft.DataTextField = "TailNumber"
lstAircraft.DataBind()
lstAircraft.Items.Add("SELECT ALL")
End If
End Sub

Function GetAircraft() As System.Data.DataSet
'this function will load the Tail Numbers
Dim connectionString As String = "myconnection"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [Aircraft].[TailNumber], [Aircraft].[ID] FROM [Aircraft] ORDER BY [Aircraft].[TailNumber] "
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet


dataAdapter.Fill(dataSet)

Return dataSet
End Function


Thanks
Rock *
 
Try This:

VB.NET:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  If Not Page.IsPostBack Then
    lstAircraft.DataSource = GetAircraft() 'this function will load the Tail Numbers
    lstAircraft.DataValueField = "ID"
    lstAircraft.DataTextField = "TailNumber"
    lstAircraft.DataBind()
    'lstAircraft.Items.Add("SELECT ALL")
    [COLOR=Red]'This line Inserts a new item at position 0 which is the top.
    lstAircraft.Items.Insert(0,New ListItem("-- SELECT ALL --", 0))[/COLOR]
  End If
End Sub

Function GetAircraft() As System.Data.DataSet
  'this function will load the Tail Numbers
  Dim connectionString As String = "myconnection"
  Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

  Dim queryString As String = "SELECT [Aircraft].[TailNumber], [Aircraft].[ID] FROM [Aircraft] ORDER BY [Aircraft].[TailNumber] "
  Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
  dbCommand.CommandText = queryString
  dbCommand.Connection = dbConnection
  Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
  dataAdapter.SelectCommand = dbCommand
  Dim dataSet As System.Data.DataSet = New System.Data.DataSet

  dataAdapter.Fill(dataSet)

  Return dataSet
End Function
 
Last edited:
Back
Top