Results 1 to 3 of 3

Thread: [RESOLVED] DataRow("ColumnName") vs DataRow.Item("ColumnName") ???

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2006
    Location
    NJ
    Posts
    59

    Resolved [RESOLVED] DataRow("ColumnName") vs DataRow.Item("ColumnName") ???

    Example 2 appears to be the correct way of referencing a column in a datarow; but what's strange is that in some cases, example #1 works (not necessarily in the precise example), and it other cases, it doesn’t. I'm stumped.
    Any thoughts on why??

    Example 1
    Dim dr As DataRow = Me.MyDataSet.PERSON.Rows(0)
    Dim ID As Integer = CInt(dr("PERSON_ID"))

    Example 2
    Dim dr As DataRow = Me.MyDataSet.PERSON.Rows(0)
    Dim ID As Integer = CInt(dr.Item("PERSON_ID"))

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,391

    Re: DataRow("ColumnName") vs DataRow.Item("ColumnName") ???

    There's absolutely no difference between the two. Item is the default property of the DataRow class so one is simply a shorthand for the other. Basically all classes that are a collection of items have a default property and its usually named Item. Coincidentally, you're doing exactly the same thing else where in your code. This:
    vb.net Code:
    1. Me.MyDataSet.PERSON.Rows(0)
    is just shorthand for this:
    vb.net Code:
    1. Me.MyDataSet.PERSON.Rows.Item(0)
    because Rows is a DataRowCollection and the default property for that type is Item too.

    That said, it looks like you're using a typed DataSet so you shouldn't be using untyped DataRows anyway. In that case it should look something like this:
    vb.net Code:
    1. Dim dr As PersonRow = Me.MyDataSet.PERSON(0)
    2. Dim ID As Integer = dr.PERSON_ID
    That's the whole point of using typed DataSets in the first place.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2006
    Location
    NJ
    Posts
    59

    Re: DataRow("ColumnName") vs DataRow.Item("ColumnName") ???

    As always, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width