PC Review


Reply
Thread Tools Rate Thread

Accessing a DataKey that has more than one value

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      1st Oct 2007
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.

 
Reply With Quote
 
 
 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      2nd Oct 2007
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

 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      2nd Oct 2007
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?
--
Nathan Sokalski
(E-Mail Removed)
http://www.nathansokalski.com/

"Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in message
newsE93F185-7C15-4799-AA66-(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Miha Markic
Guest
Posts: n/a
 
      2nd Oct 2007
I think you should pass an
dim x() as object
array instead of ints.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/


"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get DataKey value in GridView David C Microsoft ASP .NET 2 2nd Dec 2008 06:10 PM
Accessing a DataKey that has more than one value Nathan Sokalski Microsoft ADO .NET 3 2nd Oct 2007 05:54 PM
Accessing a DataKey that has more than one value Nathan Sokalski Microsoft VB .NET 3 2nd Oct 2007 05:54 PM
Get DataKey on RowDataBound MasterChief Microsoft ASP .NET 3 15th Feb 2006 11:03 PM
Datakey in Datagrid J. Ormaetxe Microsoft ASP .NET 3 29th Oct 2003 12:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:18 PM.