Casting Issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Casting Issue

  1. #1
    Join Date
    Nov 2009
    Posts
    14

    Casting Issue

    Hi,

    I am trying to call a web service and return the results in an ArrayList. I'm petty much at a standstill with a casting issue. Here is the function that call the webservice and the error.

    Using Windows CE
    Thanks!



    public ArrayList GetTicketsForOffline() { WebService.TicketVO wsTicket = new WebService.TicketVO(); WebService.TicketService service = new WebService.TicketService(); ArrayList arrTicketResult = new ArrayList(); ArrayList arrTickets = new ArrayList(service.GetTicketsForOffline().ToList()); for (int i = 0; i <= (arrTickets.Count - 1); i++) { TicketVO ticket = new TicketVO(); wsTicket = (WebService.TicketVO)arrTickets[i];<--System.InvalidCastException: InvalidCastException ticket.EventID = wsTicket.EventID; ticket.IPAddress = wsTicket.IPAddress; ticket.OrderID = wsTicket.OrderID; ticket.SaleDate = wsTicket.SaleDate; ticket.ScanDate = wsTicket.ScanDate; ticket.StatusID = wsTicket.StatusID; ticket.TicketID = wsTicket.TicketID; ticket.TypeID = wsTicket.TypeID; arrTicketResult.Add(ticket); } return arrTicketResult; }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Casting Issue

    I can't read that, please format it and use [code] tags.

  3. #3
    Join Date
    Nov 2009
    Posts
    14

    Re: Casting Issue

    Sorry abut the formatting....

    I've also tried:

    WebService.TicketVO[] wsTicket = (WebService.TicketVO[])arrTickets.ToArray(typeof(WebService.TicketVO));

    instead of wsTicket = (WebService.TicketVO)arrTickets[i];

    but I get ther same error.

    Code:
     
    public ArrayList GetTicketsForOffline() 
    { 
         WebService.TicketVO wsTicket = new WebService.TicketVO(); 
         WebService.TicketService service = new WebService.TicketService(); 
         ArrayList arrTicketResult = new ArrayList(); 
         ArrayList arrTickets = new ArrayList(service.GetTicketsForOffline().ToList()); 
        for (int i = 0; i <= (arrTickets.Count - 1); i++) 
       { 
             TicketVO ticket = new TicketVO(); 
             wsTicket = (WebService.TicketVO)arrTickets[i];<--System.InvalidCastException:    InvalidCastException 
    
             ticket.EventID = wsTicket.EventID; 
             ticket.IPAddress = wsTicket.IPAddress; 
             ticket.OrderID = wsTicket.OrderID; 
             ticket.SaleDate = wsTicket.SaleDate; 
             ticket.ScanDate = wsTicket.ScanDate; 
             ticket.StatusID = wsTicket.StatusID; 
             ticket.TicketID = wsTicket.TicketID; 
             ticket.TypeID = wsTicket.TypeID; 
             arrTicketResult.Add(ticket); 
      } 
      return arrTicketResult; 
    }

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Casting Issue

    1. You don't need this:
    Code:
    WebService.TicketVO wsTicket = new WebService.TicketVO();
    You only use wsTicket in the look, so you can declare it there.

    2. Usually, this for
    Code:
    for (int i = 0; i <= (arrTickets.Count - 1); i++)
    is coded like this:
    Code:
    for (int i = 0; i < arrTickets.Count ; i++)
    Now, what is WebService.TicketVO? Is it a value type or a reference type? Have you tried this:
    Code:
    wsTicket = arrTickets[i] as WebService.TicketVO;
    And what does service.GetTicketsForOffline() return?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Nov 2009
    Posts
    14

    Re: Casting Issue

    Hi Cilu,

    WebService.TicketVO is a value type class object.

    Code:
    [Serializable]
    public class TicketVO
    {
    
        private int ticketId;
        private int eventId;
        private int orderId;
        private int statusId;
        private int typeId;
        private string ipaddress;
        private DateTime saleDate;
        private DateTime scanDate;
    
        public int TicketID { get { return ticketId; } set { ticketId = value; } }
        public int EventID { get { return eventId; } set { eventId = value; } }
        public int OrderID { get { return orderId; } set { orderId = value; } }
        public int StatusID { get { return statusId; } set { statusId = value; } }
        public int TypeID { get { return typeId; } set { typeId = value; } }
        public string IPAddress { get { return ipaddress; } set { ipaddress = value; } }
        public DateTime SaleDate { get { return saleDate; } set { saleDate = value; } }
        public DateTime ScanDate { get { return scanDate; } set { scanDate = value; } }
    
    }
    service.GetTicketsForOffline() returns and arraylist.

    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");
    
            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;
        
        }
    Actually, I just found out the ArrayList arrTickets = new ArrayList(service.GetTicketsForOffline().ToList()); is returning NULL Objects.

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