I am getting this Error :
Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.Array'.
On the following Line :
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:
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:
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:
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()
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