The last two lines of this code do not work correctly — the results are coming back from the LINQ query. I’m just not sure how to successfully bind the indicated columns in the results to the textfield and valuefield of the dropdownlist:
protected void BindMarketCodes()
{
List<lkpMarketCode> mcodesList = new List<lkpMarketCode>();
LINQOmniDataContext db = new LINQOmniDataContext();
var mcodes = from p in db.lkpMarketCodes
orderby 0
select p;
mcodesList = mcodes.ToList<lkpMarketCode>();
//bind to Country COde droplist
dd2.DataSource = mcodesList;
dd2.DataTextField = mcodesList[0].marketName;
dd2.DataValueField = mcodesList[0].marketCodeID.ToString();
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
protected void BindMarketCodes()
{
using(var dc = new LINQOmniDataContext())
{
dd2.DataSource = from p in db.lkpMarketCodes
orderby 0
select new {p.marketName, p.marketCodeID };
dd2.DataTextField = "marketName";
dd2.DataValueField = "marketCodeID";
dd2.DataBind();
}
}
// no need to use ToList()
// no need to use a temp list;
// using an anonymous type will limit the columns in your resulting SQL select
// make sure to wrap in a using block;
Method 2
See revised code below
protected void BindMarketCodes()
{
using (var dataContext = new LINQOmniDataContext()) {
//bind to Country COde droplist
dd2.DataSource = from p in dataContext.lkpMarketCodes
orderby p.marketName
select new {p.marketCodeID, p.marketName};
dd2.DataTextField = "marketName";
dd2.DataValueField = "marketCodeID";
dd2.DataBind();
}
}
Method 3
DropDownList ddl_RouteLocation = (DropDownList)e.Row.FindControl("ddl_RouteLocation");
ddl_RouteLocation.DataSource = dtLocation;--(dtlocation i have return method of linq in dtlocation)
ddl_RouteLocation.DataTextField =dtLocation.Rows[0]"LocationName"].ToString();
ddl_RouteLocation.DataValueField =dtLocation.Rows[0]["LocationId"].ToString();
ddl_RouteLocation.DataBind();
ddl_RouteLocation.Items.Insert(0, new ListItem("--Select--", "0"));
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0