none
Filling dropdown list from linq query results?

    Question

  • Greetins. I'm trying to fill dropdown list with resutls from linq query results.

    Query gets result from SQL Server 2008 view and I can fill data grid without any problems. I'd like to put resutls of query in dynamic array, but I don't know how to extract resutls.

    This is my code

    Dim

     

     

    baza As New dbAktivnostiDataContext

     

     

    Dim firme = From tvrtke In baza.w_Tvrtke_lista_pregleds - this code querryes Company's ID, Name and address

     

     

    Select tvrtke

    I'd like to get companye's ID and to put them in dropdown list DataValuefiled, and to put companye's name in DataTextField.

    Please help!


    Seni Buljat, MCP
    • Moved by Bob_BaoMVP Monday, January 17, 2011 5:36 PM (From:Visual Basic General)
    Sunday, January 16, 2011 4:09 PM

Answers

  • Hi Seni,

    Welcome to LINQ to SQL, and thanks @BOb Bao.

    You can use Anonymous type to achieve your goal, here is my code that works Ok on my computer:

          var courses = (from c in context.CourseNames select new {Field=c.CName,Value=c.CId}).ToList();
          this.dropDownList.DataSource = courses;
          dropDownList.DataTextField = "Field";
          dropDownList.DataValueField = "Value";
          dropDownList.DataBind();
    
    <asp:DropDownList ID="dropDownList" runat="server"></asp:DropDownList>
    

    Please have a try, if you have any problems, please feel free to follow up.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 18, 2011 7:02 AM
    Moderator