The issue you are having is because the session variable "cart" is nothing/null when the addCart method is fired. I have updated your code as below:
Protected Function addToCart(ByVal sender As Object, ByVal e As CommandEventArgs) As Integer
Dim pid As String = e.CommandArgument.ToString
If Not IsNothing(Session("cart")) Then
Dim slCart As SortedList = CType(Session("cart"), SortedList)
slCart.Add(pid, "1")
Response.Redirect("~/testing.aspx")
Else
Dim slCart As New SortedList
slCart.Add(pid, "1")
Session("cart") = slCart
Response.Redirect("~/testing.aspx")
End If
Return Nothing
End Function
To further enhance the mechanics of your page logic and to work with good coding practice, is to define your session cart variable as a class member:
Private Property cart() As SortedList
Get
If Session("cart") Is Nothing Then
Session("cart") = New SortedList
End If
Return Session("cart")
End Get
Set(ByVal value As SortedList)
Session("cart") = value
End Set
End Property