C# ASP.NET DropDownList Bind To Table Source Code Example
"I tried to learn .NET by taking boot camp classes that cost me $6,000. After taking the classes, I discovered tons of free (and very cheap) videos on the internet created by experts in the field. I found the videos to be much better than the classrooms. The videos were presented by Microsoft presenters that knew the material backwards and forwards, and they were much cheaper and better than the classroom teachers. I have spent the last three years organizing and hunting down the videos for my own use and decided that all could benefit by sharing what I found." - Wade Harvey
Click Here for Free 24 hour pass to lynda.com.
I AM VERY EXCITED ABOUT THIS AMAZING NEW DISCOVERY
Over 40,000 Top-Notch Video Tutorials (or 3,452 hours) on Lynda.com
I had stumbled across Lynda.com many times before, but never stopped to try it out. I recently took the plunge and started using Lynda.com to help me improve my javascript skills (See Javascript Essential Training 2007 by Dori Smith). It has revolutionized my understanding of the language! I love the fact that the exercise files allow you to follow along in real time with what the instructor is saying. Learning by doing is an extremely powerful technique. I have to give the site an A+ for both quality and content.
I am planning on using Lynda Videos to help me improve in:

Looking for premium .NET Training Videos? The best premium .NET Videos that I have found are at Learn Visual Studio. Those videos are only about 50 cents per hour, as compared to $25 per hour that other sites charge!
LearnVisualStudio is currently having a 25% off sale ($50 savings) and giving away a free membership to TrainingSpot ($60 value) when LVS Lifetime Membership is purchased.


Limited Time Offer: Free Lifetime Membership to Progress Monitor ($24.95 value) when you purchase any membership at Learn Visual Studio, Lynda Videos or any hosting account at DiscountASP - just forward order confirmation to harvey007@sbcglobal.net
I had stumbled across Lynda.com many times before, but never stopped to try it out. I recently took the plunge and started using Lynda.com to help me improve my javascript skills (See Javascript Essential Training 2007 by Dori Smith). It has revolutionized my understanding of the language! I love the fact that the exercise files allow you to follow along in real time with what the instructor is saying. Learning by doing is an extremely powerful technique. I have to give the site an A+ for both quality and content.
I am planning on using Lynda Videos to help me improve in:
- HTML
- CSS
- Classic ASP
- .NET (included in ASP section)
- AJAX
- Silverlight (in Microsoft Section)
- SharePoint (in Microsoft Section)
- Web Design
- Blogging
- SEO
- and much more

Looking for premium .NET Training Videos? The best premium .NET Videos that I have found are at Learn Visual Studio. Those videos are only about 50 cents per hour, as compared to $25 per hour that other sites charge!
LearnVisualStudio is currently having a 25% off sale ($50 savings) and giving away a free membership to TrainingSpot ($60 value) when LVS Lifetime Membership is purchased.


Limited Time Offer: Free Lifetime Membership to Progress Monitor ($24.95 value) when you purchase any membership at Learn Visual Studio, Lynda Videos or any hosting account at DiscountASP - just forward order confirmation to harvey007@sbcglobal.net
C# ASP.NET DropDownList Bind To Table Source Code Example
Purpose: – Illustrates using DropDownList and bind to data table for C# ASP.NET.
Prerequistes:
- Install C# (Express or Standard Edition)
- Install SQL Server Express
- Download Northwind Database
- Attach Northwind Database to Databases in Sql Express
Notes:
- You can build your own library of syntax examples by using same web site over and over and just adding new web forms.
Instructions:
- Use Visual Web Developer 2008
- Create new web site;
- Click File/New Web Site
- Select ASP.NET Website Template
- Select C-Sharp for Language
- name of website could be CSharp_ASPNET_Syntax.
- Add New folder named "Standard_Controls"
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: Standard_Controls
- Add Web Form Named DropDownListBindToTable to Standard_Controls folder
- Right-click Standard_Controls folder;
- add new item;
- Select Web Form
- Check place code behind in separate file
- Web Form name could be DropDownListBindToTable
- Click on copy code in code below to copy code into web form DropDownListBindToTable.aspx
- Click on copy code in second set of code below to copy code into code-behind DropDownListBindToTable.aspx.cs
- Click green arrow to start with debugging
Step 1: Click on Copy Code to Cut-n-paste code into DropDownListBindToTable.aspx
| XML | | copy code | | ? |
| 01 | |
| 02 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownListBindToTable.aspx.cs" Inherits="Standard_DropDownListBindToTable" %> |
| 03 | |
| 04 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 05 | |
| 06 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 07 | <head runat="server"> |
| 08 | <title></title> |
| 09 | </head> |
| 10 | <body> |
| 11 | <form id="form1" runat="server"> |
| 12 | <div> |
| 13 | <asp:DropDownList ID="DropDownList1" runat="server"> |
| 14 | </asp:DropDownList> |
| 15 | </div> |
| 16 | </form> |
| 17 | </body> |
| 18 | </html> |
| 19 |
Step 2: Click on Copy Code to Cut-n-paste code into DropDownListBindToTable.aspx.cs
| | | copy code | | ? |
| 01 | |
| 02 | |
| 03 | using System; |
| 04 | using System.Data; |
| 05 | using System.Data.SqlClient; |
| 06 | using System.Collections; |
| 07 | |
| 08 | partial class Standard_DropDownListBindToTable : System.Web.UI.Page |
| 09 | { |
| 10 | |
| 11 | protected void // ERROR: Handles clauses are not supported in C# |
| 12 | Page_Load(object sender, System.EventArgs e) |
| 13 | { |
| 14 | SqlConnection con = new SqlConnection("Server=(local)\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI"); |
| 15 | |
| 16 | SqlCommand cmd = new SqlCommand(); |
| 17 | |
| 18 | cmd.CommandText = "SELECT EmployeeID, FirstName + ' ' + LastName as FullName FROM Employees"; |
| 19 | cmd.Connection = con; |
| 20 | |
| 21 | DataTable Table1; |
| 22 | Table1 = new DataTable("Employees"); |
| 23 | //creating a table named Employees |
| 24 | DataRow Row1; |
| 25 | //declaring row for the table |
| 26 | |
| 27 | DataColumn EmployeeID = new DataColumn("EmployeeID"); |
| 28 | //declaring a column named EmployeeID |
| 29 | EmployeeID.DataType = System.Type.GetType("System.Int32"); |
| 30 | //setting the datatype for the column |
| 31 | Table1.Columns.Add(EmployeeID); |
| 32 | //adding the column to table |
| 33 | DataColumn FullName = new DataColumn("FullName"); |
| 34 | FullName.DataType = System.Type.GetType("System.String"); |
| 35 | Table1.Columns.Add(FullName); |
| 36 | |
| 37 | |
| 38 | |
| 39 | try { |
| 40 | con.Open(); |
| 41 | SqlDataReader reader = cmd.ExecuteReader(); |
| 42 | //(CommandBehavior.SingleRow) |
| 43 | while (reader.Read()) { |
| 44 | |
| 45 | Row1 = Table1.NewRow(); |
| 46 | //declaring a new row |
| 47 | Row1["EmployeeID"] = reader.GetInt32(0); |
| 48 | //filling the row with values. Item property is used to set the field value. |
| 49 | Row1["FullName"] = reader.GetString(1); |
| 50 | //filling the row with values. adding FullName |
| 51 | Table1.Rows.Add(Row1); |
| 52 | |
| 53 | |
| 54 | } |
| 55 | reader.Close(); |
| 56 | } |
| 57 | finally { |
| 58 | con.Close(); |
| 59 | } |
| 60 | DropDownList1.DataSource = Table1; |
| 61 | this.DropDownList1.DataTextField = "FullName"; |
| 62 | this.DropDownList1.DataValueField = "EmployeeID"; |
| 63 | DropDownList1.DataBind(); |
| 64 | } |
| 65 | } |
| 66 |
Related posts:
- VB.NET ASP.NET DropDownList Bind To Table Source Code Example VB.NET 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#...
- C# DataTable SqlDataReader Example – SqlDataReader Datatable Example C# C# DataTable SqlDataReader Example - SqlDataReader Datatable Example C#...
- VB.NET DataTable SqlDataReader Example – SqlDataReader DataTable Example VB.NET VB.NET DataTable SqlDataReader Example - SqlDataReader DataTable Example VB.NET...
Related posts brought to you by Yet Another Related Posts Plugin.
