Tuesday, December 21, 2010

DropDownListANDListBoxFilling Using LINQandSQL

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ShowProduct();
}
}


private void ShowProduct()
{
DataClassesDataContext db = new DataClassesDataContext();
FillListBox(db);
}


private void FillListBox(DataClassesDataContext db)
{

var myBindData = from c in db.Linqs select new { CustomerId=c.CustomerID, Country=c.Country};

//Bind the ListBox
ListBox1.DataSource = myBindData;
ListBox1.DataTextField = "Country";
ListBox1.DataValueField = "CustomerID";
ListBox1.DataBind();

//Bind the DropDownList
DropDownList1.DataSource = myBindData;
DropDownList1.DataTextField = "Country";
DropDownList1.DataValueField = "CustomerID";
DropDownList1.DataBind();
}

No comments: