Options

GridView and ObjectDataSource with WebService data (paging)

GoyoGoyo Member Posts: 36
edited 2012-06-08 in NAV Three Tier
Hello.

I'm developing a web page with .net and visual studio. I'm trying to accesss Navision 2009 R2 data through web services. I've created a Web Form and inserted a GridView and an ObjectDataSource. I've configured the DataSource to access to Navision data trough my WebService and made this the Datasource for my GridView. Everything is ok so far. The GridView shows the columns I want it to show.

The error appears when I run the Web Page. The message is:
Error de la solicitud con el código de estado HTTP 401: Unauthorized. 
Descripción: Excepción no controlada al ejecutar la solicitud Web actual. Revise el seguimiento de la pila para obtener más información acerca del error y dónde se originó en el código. 

Detalles de la excepción: System.Net.WebException: Error de la solicitud con el código de estado HTTP 401: Unauthorized.

Error de código fuente: 


Línea 178:        [return: System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
Línea 179:        public Visibles[] ReadMultiple([System.Xml.Serialization.XmlElementAttribute("filter")] Visibles_Filter[] filter, string bookmarkKey, int setSize) {
Línea 180:            object[] results = this.Invoke("ReadMultiple", new object[] {
Línea 181:                        filter,
Línea 182:                        bookmarkKey,

But if I bind the GridView to my WebService without the ObjectDataSource, with the UseDefaultCredentials = True for my web service, and I run ReadMultiple(wsFiltros.ToArray(), null, NUMLINEAS), it runs Ok. So I don't what am I doing wrong.

I'm guessing if I should configure my WebService in design time with the UseDefaultCredentials = True, but I can't find the place to configure anything in my WebService.

Any ideas?

Thanks.

Answers

  • Options
    Troubles_In_ParadiseTroubles_In_Paradise Member Posts: 588
    I'm not an expert but in the training material I found that when you access to a WS exposed from NAV you've to use default credentials:
    static void Main(string[] args)
    {
    // Create a new instance of the service.
    DemoCU ws = new DemoCU();
    // Use default credentials for authenticating
    // against Microsoft Dynamics NAV.
    ws.UseDefaultCredentials = true;      <-------------
    ws.Url ="http://localhost:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Codeunit/DemoCU";
    ...
    
    ~Rik~
    It works as expected... More or Less...
  • Options
    GoyoGoyo Member Posts: 36
    Where is that training material you're talking about? I still can't make it work but I could use that material to look for errors.

    Thanks.
  • Options
    Troubles_In_ParadiseTroubles_In_Paradise Member Posts: 588
    Goyo wrote:
    Where is that training material you're talking about? I still can't make it work but I could use that material to look for errors.

    Thanks.

    You should find it on PartnerSource.
    ~Rik~
    It works as expected... More or Less...
  • Options
    GoyoGoyo Member Posts: 36
    Hi, I'm looking for it but I can't find anything. Please, could you say what to look for in the partner source? It's almost impossible to find anything there. If i look for "Training Materials" i can't see anything useful. The same for "Training Material Nav 2009". I'd really appreciate your help.
  • Options
    koubekkoubek Member Posts: 84
    Theoretically you can authenticate in two ways:
    - using the default credentials of the currently logged on user (client.UseDefaultCredentials = true;)
    - define credentials as NetworkCredentials (client.Credentials = new System.Net.NetworkCredential("username", "pwd", "domain");)

    The following code has been copy-pasted from "NAV 2009 Developer and IT Pro Help"
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication
    {
        // Import newly generated Web service proxy.
        using WebService; 
        
        class Program
        {
            static void Main(string[] args)
            {
                // Create instance of service and set credentials.
                Customer_Service service = new Customer_Service();
                service.UseDefaultCredentials = true;
                Customer cust = new Customer();
                cust.Name = "Customer Name";
                service.Create(ref cust);
                cust = service.Read(cust.No);
                service.Delete(cust.Key);
            }
        }
    }
    
  • Options
    GoyoGoyo Member Posts: 36
    I finally found the documentation, thank you both. But it doesn't solve my problem. What I was trying to do is to show a GridView with paging. If I configure the WebService that way, I put "AllowPaging = true", I run the WebPage, it shows the first 10 records (so far so good), but now I click the "2" link (to navigate to page 2) and I get the exception "System.Web.HttpException: GridView 'GridView1' desencadenó el evento PageIndexChanging, que no estaba controlado". Do you know the way to do that so I don't have to program anything for paging?
  • Options
    koubekkoubek Member Posts: 84
    OK, you should probably implement paging by yourself. "ReadMultiple" method reads has "setSize" parameter:
    - (setSize == 0) => returns the entire set of results (this is not the best way because of large data sets that you can retrieve)
    - (setSize != 0) => returns the size of the set (if negative the result will be in reverse order) - this is useful parameter to implement paging

    Look at the example from the manual:
    Customer_Service service = new Customer_Service();
    service.UseDefaultCredentials = true;
    
    const int fetchSize = 10;
    string bookmarkKey = null;
    List<Customer> customerList = new List<Customer>();
    
    // Read customer data in pages of 10.
    Customer[] results = service.ReadMultiple(new Customer_Filter[] { }, bookmarkKey, fetchSize);
    while (results.Length > 0)
    {
        bookmarkKey = results.Last().Key;
        customerList.AddRange(results);
        results = service.ReadMultiple(new Customer_Filter[] { }, bookmarkKey, fetchSize);
    }
    
    // Print the collected data.
    foreach (Customer customer in customerList)
    {
        Console.WriteLine(customer.Name);
    }
    
  • Options
    GoyoGoyo Member Posts: 36
    Hello. Thank you for your help. I've managed to solve this in a very easy way:
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    

    Just with that code, the paging works. \:D/
  • Options
    koubekkoubek Member Posts: 84
    Perfect. But in your case you read all dataset at once (maybe i am wrong) and then you display only some part of the whole dataset while paging. Hmmm, be aware of the huge datasets and changes of the data (some records can be changed and disappears from the dataset meanwhile you are listing the pages of the "static" dataset)...
  • Options
    GoyoGoyo Member Posts: 36
    Hello koubek. How should I do that? I'm starting with WebServices and I don't know exactly how to work with them. I'm looking for documentation for almost every step, and I can't find examples for the most simple things like select data, show it and navigate through it with paging.
Sign In or Register to comment.