LINQ Binding to Dropdownlist ASP.NET
I came accross some issue when I had to bind a data coming from Table to a Dropdownlist using Linq to SQL data access, in this example I’m going to show you, we’ll use U.S. States as a prime subject for this tutorial. Let’s get started!
Let’s say you have a table with 2 fields
ID | State
— —–
1 AZ
2 CA
3 WA
Before you begin to call the following fields from your Linq statements , prepare a class object that you can use to hold or handle the data for instance in this example we can do the following:
Public Class StateObj
{
public int sID {get;set;}
public string stateName {set;set;}
}Now that we got that taken care of, it will be ready once we pass the value. So let’s build our query in Linq. Let’s create an object that extract the data.
So after you’ve done this, all you have to do now is bind that method to your dropdownlist , let’s call our dropdownlist “ddl_state”;
ddl_state.DataSource = GetState();
ddl_state.DataBind();
ddl_state.DataTextField = “sID”;
ddl_state.DataValueField =”stateName”;
And there you are, you’re all set when you run this it will show you a dropdownlist containing the state description if you view the source html would look like this:
<input type='Dropdownlist' id='ddl_state'>
<option value='1'>AZ</option>
<option value='2'>CA</option>
<option value='3'>WA</option>
</input>I hope this short tutorial will help you with this needs.
Happy Programming!~


