Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2005, 05:17 AM
Authorized User
 
Join Date: Oct 2004
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
 
Old June 15th, 2005, 06:09 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old June 16th, 2005, 08:28 AM
Authorized User
 
Join Date: Oct 2004
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.