Accessing a DataKey that has more than one value

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I am attempting to access a DataKey that has more than one value. The code I
use to create the table and where the error occurs are:

Creating the DataTable:

cart.Columns.Add(New DataColumn("productid", GetType(Integer)))
cart.Columns.Add(New DataColumn("producttable", GetType(Integer)))
cart.Columns.Add(New DataColumn("quantity", GetType(Integer)))
cart.Columns.Add(New DataColumn("price", GetType(Single)))
Dim x() As DataColumn = {cart.Columns(0), cart.Columns(1)}
cart.PrimaryKey = x

Where the error occurs:

Private Sub AddToCart(ByVal productid As Integer)
Dim x() As Integer = {productid, Me.categoryid}
If Me.cart.Rows.Contains(x) Then

When I run my code, I recieve the following error message:

Server Error in '/' Application.
--------------------------------------------------------------------------------
Expecting 2 value(s) for the key being indexed, but received 1 value(s).
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentException: Expecting 2 value(s) for the
key being indexed, but received 1 value(s).

Source Error:

Line 46: Private Sub AddToCart(ByVal productid As Integer)
Line 47: Dim x() As Integer = {productid, Me.categoryid}
Line 48: If Me.cart.Rows.Contains(x) Then
Line 49:
Me.cart.Rows(Me.cart.Rows.IndexOf(Me.cart.Rows.Find(x)))(2) =
CInt(Me.cart.Rows(Me.cart.Rows.IndexOf(Me.cart.Rows.Find(x)))(2)) + 1
Line 50: Else


The error occurs on line 48 (in the Contains() method). The error says that
it is expecting 2 values, but you will notice that on line 47 I declare x()
and initialize it to an array of 2 values, and then use it as the parameter
for Contains() in line 48. What am I doing wrong? Thanks.
 
C

Cor Ligthert[MVP]

You use 2 values.

Even if you set them in an array, it are 2 values.

Although you have one reference to them, it still stays two values.

Cor
 
N

Nathan Sokalski

Unless I am mistaken, isn't that what I am doing in the following code
(which I posted in my original posting):

Private Sub AddToCart(ByVal productid As Integer)
Dim x() As Integer = {productid, Me.categoryid}
If Me.cart.Rows.Contains(x) Then

If there is some kind of error or something here, please let me know, but it
seems to me that the above code would do the following:

1. Create an array of Integers and assign it the values in productid and
Me.categoryid
2. Use that array as the parameter for the Contains() method

Because 2 values are assigned to x() in the first step, doesn't that mean I
am passing 2 values in the second step?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top