Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 15th, 2005, 05:17 AM
Authorized User
 
Join Date: Oct 2004
Location: Bristol, Nth. Somerset, United Kingdom.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default ADO.NET

I'm having trouble a bit of trouble with the code below. It was taken from a book on VB.NET.

I've highlighted the error below which is a simple IndexOutOfRange. I'm happy with the right side of the equation but I can’t work out the syntax for the left hand side which I shown in red here:

myRow.Item(counter) = strCollection(counter)

Any help would be appreciated.:)





Code:
Imports System
Imports System.Object
Imports System.Data
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Windows.Forms

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strCollection As New StringCollection

        strCollection.Add("Dog")
        strCollection.Add("Cat")
        strCollection.Add("Sheep")
        strCollection.Add("Cow")

        Dim myDataSet As New DataSet

        Dim myTable As New DataTable
        'Add the table to the tables collection
        myDataSet.Tables.Add(myTable)

        ' Create a column
        Dim AccountsColumn As New DataColumn("Accounts")
        ' And add it to the columns collection of the table
        myDataSet.Tables(0).Columns.Add(AccountsColumn)

        'Create a row
        Dim myRow As DataRow
        ' Add a new row to the table


        Dim counter As Integer
        For counter = 0 To strCollection.Count - 1
            myRow = myDataSet.Tables(0).NewRow()
            myRow.Item(counter) = strCollection(counter) <----ERROR
            myTable.Rows.Add(myRow)
        Next
    End Sub
Reply With Quote
  #2 (permalink)  
Old June 15th, 2005, 06:09 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I would say the problem here is that you are referencing an incremental index some the columnds of the table which you don't need to do. Basically it plays out like this:

If your string collection looks like this:

"one"
"two"
"three"

And your new datatable structure looks like this

Accounts
--------

then what that loop is trying to do is this:
Code:
What you want:   What you are getting:

Accounts         Accounts
--------         --------
"one"            "one"
"two"                      "two"
"three"                           "three"
Every column assignment after the first one is trying to assign to a non-existing column. You don't need to index the Items of the row, just use the first one (which is the first and only column you added to the table):

myRow.Item(0) = strCollection(counter)

-Peter
Reply With Quote
  #3 (permalink)  
Old June 16th, 2005, 08:28 AM
Authorized User
 
Join Date: Oct 2004
Location: Bristol, Nth. Somerset, United Kingdom.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between ADO and ADO.NET rakeshclose2u ADO.NET 2 April 23rd, 2007 03:57 AM
ADO AND ADO.NET royalsurej ADO.NET 1 November 8th, 2004 08:28 AM
How can connect VB.Net with ADO.Net? arvind_pathak VS.NET 2002/2003 1 November 6th, 2003 09:22 AM
ADO.NET with ASP.NET stu9820 ADO.NET 5 September 2nd, 2003 11:47 AM
MSDE and SQL CE (using VB.NET and ADO.NET) LEGS ADO.NET 0 July 12th, 2003 11:27 AM



All times are GMT -4. The time now is 01:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.