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

Discussion in 'ASP .Net' started by Zenobia, Jun 23, 2004.

  1. Zenobia

    Zenobia Guest

    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
    %>
     
    Zenobia, Jun 23, 2004
    #1
    1. Advertising

  2. Zenobia

    Zenobia Guest

    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?


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

    >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
    >%>
     
    Zenobia, Jun 23, 2004
    #2
    1. Advertising

  3. Zenobia

    Zenobia Guest

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

    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.

    >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
    >%>
     
    Zenobia, Jun 23, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pcouas
    Replies:
    13
    Views:
    8,083
    pcouas
    Feb 20, 2006
  2. Deepak
    Replies:
    2
    Views:
    1,728
    Rolf Magnus
    Nov 24, 2004
  3. CodeMonkey
    Replies:
    1
    Views:
    773
    joyal jhaveri
    Feb 4, 2011
  4. Lilith
    Replies:
    10
    Views:
    507
    peter koch
    Dec 20, 2007
  5. Peña, Botp
    Replies:
    1
    Views:
    266
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page