Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Populating a DropDownList from DB (ASP.Net)

Hi,

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?

Regards

James

James'Smiler' Farrer
Thursday, August 07, 2003

Here's a decent tutorial:
http://www.aspfriends.com/freebook/learn/dblist.aspx.  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.

rick

rick
Thursday, August 07, 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.

earlofroberts

    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
            .Connection.Open()
            With .Parameters.Add("@ct", SqlDbType.Int)
                .Direction = ParameterDirection.Output
            End With
            .Parameters("@ct").Value = 0
            dr = .ExecuteReader(CommandBehavior.CloseConnection)
        End With
        Try
           
            'Fills ddlTypes
            With ddlTypes
                .DataSource = dr
                .DataTextField = "TypeDesc"
                .DataValueField = "TypeCode"
                .DataBind()
            End With
        Catch
        End Try
        dr.Close()
        cmd.Connection.Close()
    End Sub

Ed Roberts
Tuesday, August 19, 2003

*  Recent Topics

*  Fog Creek Home