VB.NET ASP.NET DropDownList Bind To Table Source Code Example
| .NET | .NET Programming (C# or VB) (Web or Desktop) | |||
| Key Supporting Languages | HTML | CSS | JavaScript | SQL Server |
| AJAX | ASP | Blogging | ColdFusion | mySQL |
| Perl | PHP | Ruby | Web Design | Web Dev |
VB.NET ASP.NET DropDownList Bind To Table Source Code
Purpose: – Illustrates using DropDownList and bind to data table for VB.NET ASP.NET.
Prerequistes:
- Install Visual Web Developer 2008
- Install SQL Server Express
- Download Northwind Database
- Attach Northwind Database to Databases in Sql Express
Instructions:
- Use Visual Web Developer 2008
- Select File/new web site; select template ASP.NET website; Select Visual Basic for Language;
name of website in Location field could be VBNET_ASPNET_Syntax.
- Right-click project name in solution explorer; add new folder; name of folder could be Standard_Controls
- Right-click folder; add new item; Select Web Form; check place code behind in separate file;
Web Form Name could be DropDownListBindToTable
- Copy first set of code into DropDownListBindToTable.aspx
- Copy second set of code into DropDownListBindToTable.aspx.vb
- Right-click on DropDownListBindToTable.aspx and select View in Browser
Step 1: Click on XML to Cut-n-paste code into DropDownListBindToTable.aspx
| XML | | copy code | | ? |
| 01 | |
| 02 | |
| 03 | <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DropDownListBindToTable.aspx.vb" |
| 04 | |
| 05 | Inherits="Standard_DropDownListBindToTable" %> |
| 06 | |
| 07 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 08 | |
| 09 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 10 | |
| 11 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 12 | <head runat="server"> |
| 13 | <title></title> |
| 14 | </head> |
| 15 | <body> |
| 16 | <form id="form1" runat="server"> |
| 17 | <div> |
| 18 | <asp:DropDownList ID="DropDownList1" runat="server"> |
| 19 | </asp:DropDownList> |
| 20 | </div> |
| 21 | </form> |
| 22 | </body> |
| 23 | </html> |
| 24 | |
| 25 |
Step 2: Click on VB to Cut-n-paste code into DropDownListBindToTable.aspx.vb
| Visual Basic | | copy code | | ? |
| 01 | |
| 02 | Imports System |
| 03 | Imports System.Data |
| 04 | Imports System.Data.SqlClient |
| 05 | |
| 06 | Partial Class Standard_DropDownListBindToTable |
| 07 | Inherits System.Web.UI.Page |
| 08 | |
| 09 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load |
| 14 | Dim con As SqlConnection = New SqlConnection("Server=(local)\SQLEXPRESS;Initial |
| 15 | |
| 16 | Catalog=Northwind;Integrated Security=SSPI") |
| 17 | |
| 18 | Dim cmd As New SqlCommand() |
| 19 | |
| 20 | cmd.CommandText = "SELECT EmployeeID, FirstName + ' ' + LastName as FullName FROM |
| 21 | |
| 22 | Employees" |
| 23 | cmd.Connection = con |
| 24 | |
| 25 | Dim Table1 As DataTable |
| 26 | Table1 = New DataTable("Employees") |
| 27 | 'creating a table named Employees |
| 28 | Dim Row1 As DataRow |
| 29 | 'declaring row for the table |
| 30 | |
| 31 | Dim EmployeeID As DataColumn = New DataColumn("EmployeeID") |
| 32 | 'declaring a column named EmployeeID |
| 33 | EmployeeID.DataType = System.Type.GetType("System.Int32") |
| 34 | 'setting the datatype for the column |
| 35 | Table1.Columns.Add(EmployeeID) |
| 36 | 'adding the column to table |
| 37 | Dim FullName As DataColumn = New DataColumn("FullName") |
| 38 | FullName.DataType = System.Type.GetType("System.String") |
| 39 | Table1.Columns.Add(FullName) |
| 40 | |
| 41 | |
| 42 | |
| 43 | Try |
| 44 | con.Open() |
| 45 | Dim reader As SqlDataReader = cmd.ExecuteReader '(CommandBehavior.SingleRow) |
| 46 | While reader.Read() |
| 47 | |
| 48 | Row1 = Table1.NewRow() |
| 49 | 'declaring a new row |
| 50 | Row1.Item("EmployeeID") = reader.GetInt32(0) |
| 51 | 'filling the row with values. Item property is used to set the field value. |
| 52 | Row1.Item("FullName") = reader.GetString(1) |
| 53 | 'filling the row with values. adding FullName |
| 54 | Table1.Rows.Add(Row1) |
| 55 | |
| 56 | |
| 57 | End While |
| 58 | reader.Close() |
| 59 | Finally |
| 60 | con.Close() |
| 61 | End Try |
| 62 | DropDownList1.DataSource = Table1 |
| 63 | Me.DropDownList1.DataTextField = "FullName" |
| 64 | Me.DropDownList1.DataValueField = "EmployeeID" |
| 65 | DropDownList1.DataBind() |
| 66 | End Sub |
| 67 | End Class |
| 68 | |
| 69 | |
| 70 |
Related posts:
- C# ASP.NET DropDownList Bind To Table Source Code Example C# ASP.NET DropDownList Bind To Table Source Code Example...
- VB.NET ASP.NET Table Load From SqlDataReader Source Code Example VB.NET ASP.NET Table Load From SqlDataReader Source Code Example...
- C# ASP.NET Table SqlDataReader Example – SqlDataReader Table Example C# ASP.NET C# ASP.NET Table SqlDataReader Example - SqlDataReader Table Example C#...
- VB.NET DataTable SqlDataReader Example – SqlDataReader DataTable Example VB.NET VB.NET DataTable SqlDataReader Example - SqlDataReader DataTable Example VB.NET...
- C# DataTable SqlDataReader Example – SqlDataReader Datatable Example C# C# DataTable SqlDataReader Example - SqlDataReader Datatable Example C#...
Related posts brought to you by Yet Another Related Posts Plugin.
