0

can someone help me

Im having issues binding my dropdown list from my sql linq query, theres seems to be issues with anon types not being statically typed and then the list is not being populated

please help thanks

public static void getlocation()
{
    DataClasses_AbintegroDataContext dc = new DataClasses_AbintegroDataContext("name = name");


    //List<Location> thelocations = new List<Location>(); 

    var locations = new[] { from a in dc.Locations select new { a.name } };


    DropDownList ddLocation = new DropDownList();

    ddLocation.DataSource = locations;
    ddLocation.DataTextField = "Location";
    ddLocation.DataValueField = "Location";


}
AlanMorton2.0
  • 1,043
  • 2
  • 12
  • 22

4 Answers4

0

Try this, You have missed ddLocation.DataBind(); and did't add this dynamic DDL to any Controls and needs to be some changes

public static void getlocation()
{
    DataClasses_AbintegroDataContext dc = new DataClasses_AbintegroDataContext("name = name");
    List<Location> locations =  (from a in dc.Locations).ToList();
    DropDownList ddLocation = new DropDownList();    
    ddLocation.DataSource = locations;
    ddLocation.DataTextField = "name";
    ddLocation.DataValueField = "Id";
    ddLocation.DataBind();
    divRunServer.Controls.Add(ddLocation);

}

add this div tag in client side

<div id="divRunServer" runat="server"></div>
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

Try it:

public static void getlocation()
{
    DataClasses_AbintegroDataContext dc = new DataClasses_AbintegroDataContext("name = name");
    var locations =  from a in dc.Locations;
    DropDownList ddLocation = new DropDownList();

    ddLocation.DataSource = locations.ToList();
    ddLocation.DataTextField = "name";
    ddLocation.DataValueField = "Id";
    ddLocation.DataBind();
}

I hope it will help you.

Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
  • Thanks, however im now getting " Cannot convert method group 'ToArray' to non-delegate type 'object'. did you intend to invoke a method?, god im having a nightmare day, thanks for your help – AlanMorton2.0 Feb 26 '14 at 12:44
0

Another way:

public static void getlocation()
{
    DataClasses_AbintegroDataContext dc = new DataClasses_AbintegroDataContext("name = name"); 
    var locations = from a in dc.Locations select new {a1 = a.Id, a2 = a.name };
    DropDownList ddLocation = new DropDownList();
    ddLocation.DataSource = locations;
    ddLocation.DataValueField = "a1";
    ddLocation.DataTextField = "a2";
}
GorkemHalulu
  • 2,925
  • 1
  • 27
  • 25
0

Anonymous types should work fine, you just provided property names for DataTextField and DataValueField that aren't in your anonymous type. Try:

                                                    V-- give the property a name
var locations = from a in dc.Locations select new { Location = a.name };

DropDownList ddLocation = new DropDownList();

ddLocation.DataSource = locations;
ddLocation.DataTextField = "Location";
ddLocation.DataValueField = "Location";

However I notice you're not doing anything with the DropDownList that you create - you may be unintentionally hiding a page control named ddLocation.

D Stanley
  • 149,601
  • 11
  • 178
  • 240