/ Published in: ASP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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