Return to Snippet

Revision: 11680
at February 13, 2009 13:04 by wrburgess


Initial Code
Sub BindDropDownListDB(ByVal ddlDrop As Object, ByVal strTable As String, ByVal strValue As String, ByVal strText As String, ByVal strOrder As String, ByVal strWhere As String)

        Dim objConn As New SqlConnection()
        Dim objCmd As New SqlCommand()
        Dim strSQL As String

        objConn = New SqlConnection(ConfigurationManager.ConnectionStrings("[database]").ConnectionString)
        objCmd.Connection = objConn
        objCmd.Connection.Open()

        strSQL = "SELECT " + strValue + " As Value1, " + strText + " As Text1 "
        strSQL += "FROM " + strTable + " "
        strSQL += "WHERE " + strWhere + " "
        strSQL += "ORDER BY " + strOrder + ";"

        objCmd.CommandText = strSQL
        ddlDrop.DataSource = objCmd.ExecuteReader()
        ddlDrop.DataTextField = "Text1"
        ddlDrop.DataValueField = "Value1"
        ddlDrop.DataBind()
        ddlDrop.Items.Insert(0, New ListItem("Default Item", ""))

        objConn.Dispose()
        objConn.Close()

    End Sub

Initial URL


Initial Description


Initial Title
Load DropDownList from Database in ASP.NET 3.5

Initial Tags
aspnet

Initial Language
ASP