-
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; }
-
Re: Casting Issue
I can't read that, please format it and use [code] tags.
-
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;
}
-
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?
-
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.