Options

Populating a listbox in C# using Webservices

akerstiensakerstiens Member Posts: 15
edited 2009-10-27 in NAV Three Tier
I am almost embarrassed to ask this question, but seeing how I have been trying one thing after another in Visual Studio, I thought I would ask. I am developing a Windows Form using C# where I am trying to populate a list_box with items it receives from a readmultiple webservice call.

I cannot seem to find a good code example for this in a Windows Form setting.

Thank you all in advance for your help.

I did find the below example for VB however not for c# :-)

Dim ws As New Mywebservice.Customer_Binding
ws.UseDefaultCredentials = True
ws.Url = "http://localhost:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Page/Customer"

Dim Cust(100) As Mywebservice.Customer
Dim wsFields As New Mywebservice.Customer_Fields
Dim wsfilter(1) As Mywebservice.Customer_Filter
wsfilter(1).Field = wsFields.Name
wsfilter(1).Criteria = "J*"
Cust = ws.ReadMultiple(wsfilter, "", 0)

For Counter = LBound(Cust) To UBound(Cust)
Me.ListBox1.Items.Add(Counter & ": " + Cust(Counter).No & "-> " & Cust(Counter).Name)
Next Counter

Comments

  • Options
    boku25boku25 Member Posts: 39
    Thats a good question.

    Well are you using any ajax or special tools to populate your list box with web services? Or is it a just a list box?
  • Options
    akerstiensakerstiens Member Posts: 15
    Web Services :-)
  • Options
    freddy.dkfreddy.dk Member, Microsoft Employee Posts: 360
    The C# code is very simple...

    First of all I create a Web Reference (Add Service Reference -> Advanced -> Add Web Reference) to the customer page and call it CustomerPageRef, I also added a using statement and then the C# code would be:
    var service = new Customer_Service();
                service.UseDefaultCredentials = true;
    
                var filter = new Customer_Filter();
                filter.Field = Customer_Fields.Name;
                filter.Criteria = "J*";
                var filters = new Customer_Filter[] { filter };
                Customer[] customers = service.ReadMultiple(filters, null, 0);
    
                foreach (Customer customer in customers)
                    this.listBox1.Items.Add(customer.Name);
    

    good luck
    Freddy Kristiansen
    Group Program Manager, Client
    Microsoft Dynamics NAV
    http://blogs.msdn.com/freddyk

    The information in this post is provided "AS IS" with no warranties, and confers no rights. This post does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.
  • Options
    kinekine Member Posts: 12,562
    Or you can take the result of the webservice as DataBinding source for your listbox:
                var service = new Customer_Service();
                service.UseDefaultCredentials = true;
    
                var filter = new Customer_Filter();
                filter.Field = Customer_Fields.Name;
                filter.Criteria = "J*";
                var filters = new Customer_Filter[] { filter };
                Customer[] customers = service.ReadMultiple(filters, null, 0);
    
                this.listBox1.DataSource = customers;
                this.listBox1.DisplayMember = Customer_Service.Customer_Fields.Name.ToString();
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    tedcjohnstontedcjohnston Member Posts: 41
    Being a VS novice, I also had the problem.

    I added a data grid control to Form1 bound to the customer page web service.

    If I added the Name field to the datagrid, I would get errors. The error was in standard code:
    this.Name = "Form1";
    It appears that having a field Name conflicted with the property of the form. VS was confused.

    The errors in VS were:
    Error 1 Cannot implicitly convert type 'string' to 'System.Windows.Forms.DataGridViewTextBoxColumn'
    Warning 2 'Customer_Lookup.Form1.Name' hides inherited member 'System.Windows.Forms.Control.Name'. Use the new keyword if hiding was intended.

    When I remove the Name field from the Datagrid, no errors.

    So, to fix this, in the Edit Columns dialog, I selected the Name field, then changed the (Name) property to CustName. Now all is good and the form runs.

    Hopefully, this will help someone get around the issue of using NAV "Name" fields in VS.
    "There are only two truly infinite things: the universe and stupidity. And I am unsure about the universe." - Albert Einstein
    Corollary- Build and idiot proof system and nature will build a better idiot.
Sign In or Register to comment.