locked
How Populate a Combobox with ADO.NET dataservices and LINQ RRS feed

  • Question

  • Can you show me  a CODE LINE that I can do this?

    I try that... but dont work :(

    Dim ctx As New AutorizanetEntities(New Uri("http://localhost/ServiceServer/DataServices/webdataservice.svc"))

     'Grab the list of citys and populate the combobox
     Dim dados = (From jj In ctx.tbl_municipio _
                        Order By jj.cmp_codigo_nacional_municipio).ToList()

     Me.ComboBox4.DataSource = dados




    Thanks.

    J.Luz
    Wednesday, May 13, 2009 3:16 PM

Answers

  • Could you try the following (replace "object" with the actual type) :

    Dim dados = From jj In ctx.tbl_municipio  Order By jj.cmp_codigo_nacional_municipio Select jj
    foreach (object d in dados)
                {
                    Me.ComboBox4.Items.Add(d.City);
                }

    Hope this helps,
    Monica

    • Marked as answer by Biometrics Wednesday, May 13, 2009 7:33 PM
    Wednesday, May 13, 2009 7:26 PM
    Moderator

All replies

  • What, specifically, is not working? Are you getting an error or just an empty list?

    If you are getting an exception, please provide the full details and use a tool like Fiddler to capture the raw request and response.

    If you are simply getting an empty list, first determine whether the 'dados' variable has anything in it, to determine if the problem is with the combo-box itself or the query. If the list itself is empty, the please provide the request/response as above.

    Matt Meehan, ADO.NET Data Services (Astoria)
    Wednesday, May 13, 2009 4:56 PM
    Moderator
  • Could you try the following (replace "object" with the actual type) :

    Dim dados = From jj In ctx.tbl_municipio  Order By jj.cmp_codigo_nacional_municipio Select jj
    foreach (object d in dados)
                {
                    Me.ComboBox4.Items.Add(d.City);
                }

    Hope this helps,
    Monica

    • Marked as answer by Biometrics Wednesday, May 13, 2009 7:33 PM
    Wednesday, May 13, 2009 7:26 PM
    Moderator
  • Thanks Monica



    J.Luz
    Wednesday, May 13, 2009 7:33 PM
  • My code work good like that:

            
            Dim dados1 = (From jj In ctx.tbl_municipio _
                         Select jj Order By jj.cmp_descricao_municipio).ToList()

            Me.ComboBox9.DataSource = dados1
            Me.ComboBox9.DisplayMember = "cmp_descricao_municipio"
            Me.ComboBox9.ValueMember = "cmp_codigo_nacional_municipio"

            Me.ComboBox4.DataSource = dados1
            Me.ComboBox4.DisplayMember = "cmp_descricao_municipio"
            Me.ComboBox4.ValueMember = "cmp_codigo_nacional_municipio"


    But when i chance combobox9, my combobox4 change too????
     
    How can i fix that?

    juliano


    J.Luz
    Wednesday, May 13, 2009 7:37 PM
  • Both have the same reference to the 'dados1' list.

    Try making a copy of the list before assigning it to ComboBox4 (ie, pass it to the List<T> constructor).

    I could be wrong, but that's my guess at why they're acting that way.

    Matt Meehan, ADO.NET Data Services (Astoria)
    Wednesday, May 13, 2009 7:44 PM
    Moderator
  • Im sorry Matt,

    But how can i do that?

    :P

    J.Luz
    Wednesday, May 13, 2009 7:51 PM

  • How about this?

     Dim dados1 = (From jj In ctx.tbl_municipio _
                         Select jj Order By jj.cmp_descricao_municipio).ToList()

    Dim dados2 = (From jj In ctx.tbl_municipio _
                         Select jj Order By jj.cmp_descricao_municipio).ToList()


            Me.ComboBox9.DataSource = dados1
            Me.ComboBox9.DisplayMember = "cmp_descricao_municipio"
            Me.ComboBox9.ValueMember = "cmp_codigo_nacional_municipio"

            Me.ComboBox4.DataSource = dados2
            Me.ComboBox4.DisplayMember = "cmp_descricao_municipio"
            Me.ComboBox4.ValueMember = "cmp_codigo_nacional_municipio"
    Wednesday, May 13, 2009 7:52 PM
    Moderator
  • Ahhhh Ok with 2 linq's commands....

    I do that !!!


    I thought I could use the same command linq 

    Can you explain me why i cant use the same linq command?

    Ok..

    Thanks ALL!!!!

    J.Luz
    Wednesday, May 13, 2009 8:05 PM
  • This is actually the behavior for this scenario apparently. Each combo neesds its own separate data source.

    Here is what I found searching:

    ***********

    This has to do with how data bindings are set up in the dotnet framework, especially the BindingContext. On a high level it means that if you haven't specified otherwise each form and all the controls of the form share the same BindingContext. When you are setting the DataSource property the ComboBox will use the BindingContext to get a ConcurrenyMangager that wraps the list. The ConcurrenyManager keeps track of such things as the current selected position in the list.

    When you set the DataSource of the second ComboBox it will use the same BindingContext (the forms) which will yield a reference to the same ConcurrencyManager as above used to set up the data bindings.

    To get a more detailed explanation see BindingContext.

    ***********
    Best regards,
    Monica

    Wednesday, May 13, 2009 8:17 PM
    Moderator