Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am getting this Error :
C#
Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.Array'.

On the following Line :
C#
Dim SlctdPhones As Array = CType(Session.Item("SlctdPhones"), Array)

Any help will be appreciated!

What I have tried:

This is the code on my previous Page to get the array:
VB
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles CheckOut.Click

    Dim PNames As New List(Of String)()
     For Each index As Integer In ListBox1.GetSelectedIndices
         PNames.Add(ListBox1.Items(index).Text)
     Next
     Session("SlctdPhones") = PNames

     Response.Redirect("CheckOut.aspx?Name=" + Label2.Text + "&TPrice=" + Label1.Text)


 End Sub


this is the code that will call the funtion:
VB
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim SlctdPhones As Array = CType(Session.Item("SlctdPhones"), Array)
        Dim p As New Class1
        Dim y As Integer
        If (y = p.AddOrder(Label1.Text, SlctdPhones, CInt(Label2.Text))) = 1 Then

        End If
End Sub

This is the code of the Function:

VB
Public Function AddOrder(ByVal Name As String, ByVal NArray() As String, ByVal TPrice As Integer) As Integer

       Dim connetionString As String = Nothing
       Dim connection As SqlConnection
       Dim command1, command2 As SqlCommand
       Dim adapter As New SqlDataAdapter()
       Dim ds As New DataSet()
       Dim sql1, sql2 As String
       connetionString = "Data Source=.;Initial Catalog=Shop;integrated security=true"
       connection = New SqlConnection(connetionString)
       connection.Open()

       'Dim table1 As DataTable = New DataTable("logins")
       'Dim table2 As DataTable = New DataTable("Orders")

       sql1 = "select * from Customer_Detail"
       command1 = New SqlCommand(sql1, connection)
       adapter.SelectCommand = command1
       adapter.Fill(ds, "logins")
       sql2 = "select * from CustomerOrders"
       command2 = New SqlCommand(sql2, connection)
       adapter.SelectCommand = command2
       adapter.Fill(ds, "Orders")
       adapter.Dispose()
       connection.Close()

       Dim rows() As DataRow = ds.Tables("logins").Select("Name = '" & Name & "'")
       Dim searchedValue As Integer
       If rows.Count > 0 Then
           searchedValue = CInt(rows(0).Item("LoginID"))
       End If
       Dim PhoneNames As String = ""
       For i As Integer = 0 To NArray.Length - 1
           PhoneNames = NArray(i).ToString + PhoneNames
       Next

       Dim dr As DataRow
       dr = ds.Tables("Orders").NewRow()
       dr("logIn") = searchedValue
       dr("PhoneNames") = PhoneNames
       dr("TotalPrice") = TPrice
       ds.Tables("Orders").Rows.Add(dr)

       adapter.SelectCommand = command2
       Dim ESCBuilder As SqlCommandBuilder = New SqlCommandBuilder(adapter)
       adapter.InsertCommand = ESCBuilder.GetInsertCommand()
       adapter.Update(ds, "Orders")

       Return 1
   End Function
Posted
Updated 25-May-16 6:32am
v5

1 solution

SlctdPhones is a List(of String) not a String Array.

So get it from the Session as a List(of String) first, then if you really need an array use the .ToArray() method (but I can't see why you would want an array in this case)

[EDIT] - the next problem
You haven't populated table1 - you've just created a datatable.

Move the declarations for table1 and table2 to after you have populated the DataSet ds and populate them from the dataset
VB.NET
Dim table1 As DataTable = ds.Tables("logins")
Dim table2 As DataTable = ds.Tables("Orders")
 
Share this answer
 
v2
Comments
xTMx9 25-May-16 11:40am    
i got it now like this: Dim SlctdPhones As List(Of String) = CType(Session.Item("SlctdPhones"), List(Of String))
How can i send it to the function now? there is an error below "SlctdPhones"(the parameter of the function) ....and how is a list is taken as an argument in the function?
xTMx9 25-May-16 11:45am    
I used the .ToArray() now and it is working but getting an error when reaching this line:Dim rows() As DataRow = table1.Select("Name = '" & Name & "'") error:Cannot find column [Name].
How can i place Name to be the string taken from the function?
CHill60 25-May-16 12:15pm    
I've added a fix to the solution
xTMx9 25-May-16 14:10pm    
Any chance you know why it is happening?? (the error posted in my code below?)
The Problem iss somewhere here:
Dim rows() As DataRow = ds.Tables("logins").Select("Name = '" & Name & "'")
Dim searchedValue As Integer
If rows.Count > 0 Then
searchedValue = CInt(rows(0).Item("LoginID"))
End If
If i remove the if statement,i will get an indexout of range error....
xTMx9 25-May-16 12:38pm    
I removed the "New dataTabele" altogether ,i updated the code so now it looks like what i have(it is working) but when it reached the last line(adapter.update.....) an error occurs:
<pre>The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CustomerOrders_Customer_Detail". The conflict occurred in database "Shop", table "dbo.Customer_Detail", column 'LoginID'. </pre>

here is the 2 tables in my database:
Customer_Detail(LogInID pk,EmailAddress,Password,Name,DOB)
CustomerOrders(OrderID pk,logIn (foreign key),PhoneNames,TotalPrice)
i am suspecting that something is wrong with my code where i get the ID that matches the name sent by the function... or is there something else? It's so frustrating for the program to break just at the last line :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900