Thursday, July 23, 2009

Dotnet 2005 Data List and Custom binding controls with Drop down list


















Category Name:
Company Name:
Last Name:
Product Name:




Imports System
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

NameSpace DotNetJohn

Public Class ManyResults : Inherits System.Web.UI.Page

Protected ddlCategoryName As DropDownList
Protected ddlCompanyName As DropDownList
Protected ddlLastName As DropDownList
Protected ddlProductName As DropDownList

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

Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim dataReader As SqlDataReader
Dim strSql As String

objConn = New SqlConnection(ConfigurationSettings.AppSettings.Get("ConnectionString"))
strSql = _
"SELECT CategoryName FROM Categories ORDER BY CategoryName;" _
& "SELECT Top 10 CompanyName FROM Customers ORDER BY CompanyName;" _
& "SELECT LastName FROM Employees ORDER BY LastName;" _
& "SELECT Top 10 ProductName FROM Products ORDER BY ProductName;"

objCmd = New SqlCommand(strSql, objConn)
Try
objConn.Open()
dataReader = objCmd.ExecuteReader()
'CategoryName
With ddlCategoryName
.DataSource = dataReader
.DataTextField = "CategoryName"
.DataValueField = "CategoryName"
.DataBind()
End With
'CompanyName
dataReader.NextResult()
With ddlCompanyName
.DataSource = dataReader
.DataTextField = "CompanyName"
.DataValueField = "CompanyName"
.DataBind()
End With
'LastName
dataReader.NextResult()
With ddlLastName
.DataSource = dataReader
.DataTextField = "LastName"
.DataValueField = "LastName"
.DataBind()
End With
'ProductName
dataReader.NextResult()
With ddlProductName
.DataSource = dataReader
.DataTextField = "ProductName"
.DataValueField = "ProductName"
.DataBind()
End With
Catch exc As Exception
Response.Write(exc)
Finally
If Not dataReader Is Nothing Then
dataReader.Close()
End If
objCmd = Nothing
If objConn.State = ConnectionState.Open Then
objConn.Close()
End If
objConn.Dispose()
End Try

End Sub

End Class

End NameSpace

No comments: