DataView Find method - Error: Expecting 2 value(s) for the key being indexed, but received 3 value(s

Z

Zenobia

Hi,

Below is a bit of code from ASP.NET Unleashed which gives an
error and I can't figure out why. It uses the Authors table from
the standard Pubs database.

The error message is
"System.ArgumentException: Expecting 2 value(s) for the
key being indexed, but received 3 value(s)."

For the line:
intRowIndex = dvwAuthors.Find( arrValues )

Can someone, please, explain what is wrong here.

Commenting out one of
arrValues( 0 ) = "Dull"
or
arrValues( 1 ) = "Ann"
seems to makes no difference to the error message.

++++++++++ ++++++++++ ++++++++++

<%@ Page Language="VB" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<%
Dim dstAuthors As DataSet
Dim conPubs As SqlConnection
Dim dadAuthors As SqlDataAdapter
Dim dvwAuthors As DataView
Dim arrValues(2) As Object
Dim intRowIndex As Integer

' Grab Authors Table
dstAuthors = New DataSet()
conPubs = New SqlConnection(
"Server=localhost;UID=sa;PWD=secret;Database=Pubs" )
dadAuthors = New SqlDataAdapter( "Select * From Authors",
conPubs )
dadAuthors.Fill( dstAuthors, "Authors" )

' Create DataView
dvwAuthors = dstAuthors.Tables( "Authors" ).DefaultView()
dvwAuthors.Sort = "au_lname, au_fname"

' Find Ann Dull
arrValues( 0 ) = "Dull"
arrValues( 1 ) = "Ann"
intRowIndex = dvwAuthors.Find( arrValues )

' Display Phone Number
If intRowIndex <> -1 Then
Response.Write( dvwAuthors( intRowIndex ).Row( "Phone" ) )
Else
Response.Write( "Can't find Ann Dull" )
End If
%>
 
Z

Zenobia

I replaced these 3 lines:

arrValues( 0 ) = "Dull"
arrValues( 1 ) = "Ann"
intRowIndex = dvwAuthors.Find( arrValues )

with this one and it works now.

dvwAuthors.RowFilter = "au_lname='Dull' and au_fname='Ann'"

was that the 'corrent' thing to do?

But it still doesn't explain the Find error. How do I code the
Find correctly?
 
Z

Zenobia

On Wed, 23 Jun 2004 17:45:18 +0100, Zenobia

Apologies for wasting people's time the book has an error in it.

It should be:
Dim arrValues(1) As Object
not:
Dim arrValues(2) As Object
as, of course, the lower array bound starts at 0.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top