Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Populating a DropDownList from DB (ASP.Net)


Me again :)

Wanting to hook in a drop down list to DB data, have the stored procedure ready to go. What's the best way to populate it?

I've seen various methods but what I haven't been able to figure out is how to hook it up with just the DropDownBox.Datasource property?

Anyone care to explain how?



James'Smiler' Farrer
Thursday, August 7, 2003

Here's a decent tutorial:  If you're using Sql Server, use the SqlClient namespace as opposed to the OleDb one.  Also, remember to dispose of you Command objects in addition to closing your database connection.

You can probably find many similar ones with a quick search from Google.


Thursday, August 7, 2003

Here's a sub I use for a ddl.  Output parameter could be an Input parameter.  parameters need to be created in the same order they appear in the Stored Procedure.


    Private Sub TypeLoad()
        'Fills the ddlTypes drop down list box
        Dim dr As SqlDataReader
        Dim sSql As String
        Dim scn As String
        Dim cmd As New SqlCommand()
        Dim i As Integer

        'ocosp_4000_GetAllContactTypes returns all the Types
        'from TypeDriver table
        sSql = "ocosp_4000_GetAllContactTypes"
        scn = Session("ConnectString").ToString

        With cmd
            .Connection = _
                New SqlConnection(scn)
            .CommandText = sSql
            .CommandType = CommandType.StoredProcedure
            With .Parameters.Add("@ct", SqlDbType.Int)
                .Direction = ParameterDirection.Output
            End With
            .Parameters("@ct").Value = 0
            dr = .ExecuteReader(CommandBehavior.CloseConnection)
        End With
            'Fills ddlTypes
            With ddlTypes
                .DataSource = dr
                .DataTextField = "TypeDesc"
                .DataValueField = "TypeCode"
            End With
        End Try
    End Sub

Ed Roberts
Tuesday, August 19, 2003

*  Recent Topics

*  Fog Creek Home