Web Service function not returning arraylist
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Web Service function not returning arraylist

  1. #1
    Join Date
    Nov 2009
    Posts
    14

    Web Service function not returning arraylist

    Hi,

    I have a web service that returns an ArrayList that is working fine when tested when invoking the service. However, when I call the function from aremote app I get NULL objects.

    Wb Service function.

    Code:
        public ArrayList GetTicketsForOffline()
        {
            // Connect to DB
            SqlConnection connStr = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["idaura"]);
    
            // Call sproc
            string procString = "pr_ticket_all_get";
            SqlCommand cmd = connStr.CreateCommand();
            cmd.CommandText = procString;
            cmd.CommandType = CommandType.StoredProcedure;
            connStr.Open();
    
            // run sproc
            cmd.ExecuteNonQuery();
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            dataAdapter.SelectCommand = cmd;
    
            // Setup Dataset
            DataSet dsTickets = new DataSet();
    
            // Loop thru and store data into Dataset
            dataAdapter.Fill(dsTickets, "Tickets");
    
            // display the rows in the Tickets DataTable
            ArrayList arrTickets = new ArrayList();
    
            DataTable tickets = dsTickets.Tables["Tickets"];
            foreach (DataRow ticket in tickets.Rows)
            {
                TicketVO ticketsObj = new TicketVO();
                ticketsObj.TicketID = (int)ticket["ticket_id"];
                ticketsObj.EventID = (int)ticket["event_id"];
                ticketsObj.OrderID = (int)ticket["order_id"];
                ticketsObj.StatusID = (int)ticket["ticket_status_id"];
                ticketsObj.TypeID = (int)ticket["ticket_type_id"];
                ticketsObj.IPAddress = (string)ticket["ip_address"];
                ticketsObj.SaleDate = (DateTime)ticket["issue_date"];
                ticketsObj.ScanDate = DateTime.Now;
                arrTickets.Add(ticketsObj);
                
            }
    
            // Close Connection
            connStr.Close();
    
            return arrTickets;
        
        }

    Call to function which return NULL objects

    Code:
    ArrayList arrTickets = new ArrayList(service.GetTicketsForOffline());

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,956

    Re: Web Service function not returning arraylist

    Just change the service method signature to return an array of objects.

    Code:
    public TicketVO[ ] GetTicketsForOffline()
    {
      // Connect to DB
      using( SqlConnection connStr = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["idaura"]) )
      { 
    
        // Call sproc
        using( SqlCommand cmd = connStr.CreateCommand() )
        {
          cmd.CommandText = "pr_ticket_all_get";
          cmd.CommandType = CommandType.StoredProcedure;
          connStr.Open();
    
          // run sproc
          SqlDataReader reader = cmd.ExecuteReader();
    
          // Loop through records and add them to the list
          List<TicketVO > _ticketList = new List< TicketVO >( );
    
          while ( reader.Read( ) )
          {
            TicketVO ticket = new TicketVO();
            ticket.TicketID = (int)reader["ticket_id"];
            ticket.EventID = (int)reader["event_id"];
            ticket.OrderID = (int)reader["order_id"];
            ticket.StatusID = (int)reader["ticket_status_id"];
            ticket.TypeID = (int)reader["ticket_type_id"];
            ticket.IPAddress = (string)reader["ip_address"];
            ticket.SaleDate = (DateTime)reader["issue_date"];
            ticket.ScanDate = DateTime.Now;
    
            ticketList.Add(ticketsObj);
          }
        }
        
        return ticketList.ToArray( );
      }
    }
    I've replaced the ArrayList to use the generic List collection. Also, I'm using the using block to close the sql connection and sql command objects.
    Finally I'm using the SqlDataReader class to directly extract the records.
    Last edited by Arjay; November 20th, 2009 at 01:34 PM.

  3. #3
    Join Date
    Nov 2009
    Posts
    14

    Re: Web Service function not returning arraylist

    Thanks Arjay,

    Thanks for response.

    The calling fucntion is the app will cause an invalid cast exception.

    Code:
    WebService.TicketVO[] listTickets = (WebService.TicketVO[])service.GetTicketsForOffline().ToArray();

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,956

    Re: Web Service function not returning arraylist

    Quote Originally Posted by lexavision View Post
    Thanks Arjay,

    Thanks for response.

    The calling fucntion is the app will cause an invalid cast exception.

    Code:
     
    WebService.TicketVO[] listTickets = (WebService.TicketVO[])service.GetTicketsForOffline().ToArray();
    You need to rebuild the web proxy. Afterwards you should be able to call it this way:
    Code:
     
    WebService.TicketVO[] listTickets = service.GetTicketsForOffline();

  5. #5
    Join Date
    Nov 2009
    Posts
    14

    Re: Web Service function not returning arraylist

    Thanks Arjay...

    However with that approach I get a compile error:

    Cannot implicitly convert type 'object[]' to 'URATicketScanMobile.com.smartonline.dev.urassociation.neweventtickets.TicketVO[]'. An explicit conversion exists (are you missing a cast?)

  6. #6
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,956

    Re: Web Service function not returning arraylist

    If you've defined the web service as:

    public TicketVO[ ] GetTicketsForOffline()
    {
    ...
    }
    but are getting an error when calling it, check out the generated proxy code and make sure it's been generated properly.

    Otherwise, have you tried to cast it?

  7. #7
    Join Date
    Nov 2009
    Posts
    14

    Resolved Re: Web Service function not returning arraylist

    Works now!

    I wasn't rebuilding the web proxy properly.

    Thanks for your help Arjay!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center