Friday, November 4, 2011

Friday 11.4.11

Imports System.Web.Configuration
Imports System.Data.SqlClient
Imports System.Data

Partial Class listDataBinding
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
'create the Command and the Connection
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("Northwind").ConnectionString
Dim sql As String = "SELECT EmployeeID, TitleOfCourtesy + ' ' + FirstName + ' ' + LastName As FullName FROM Employees"
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(sql, con)

Try
'Open the connection and get the data reader
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
'Bind the DataReader to the list
lstNames.DataSource = reader
lstNames.DataBind()
reader.Close()

Catch ex As Exception
Response.Write(ex.ToString())
Finally
'close the connection
con.Close()
End Try
End If
End Sub

Protected Sub cmdGetSelection_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdGetSelection.Click
Result.Text &= "Selected Employees:"
For Each li As ListItem In lstNames.Items
If li.Selected Then
Result.Text &= String.Format("
  • ({0}) {1}
  • ", li.Value, li.Text)
    End If
    Next
    End Sub
    End Class

    No comments:

    Post a Comment