none
Error: Index was outside the bounds of the array

    Question

  • Hi all, I am getting an Index was outside the bounds of the array error but I have set the array to reinitialize after each loop:

    Imports System
    Imports System.IO
    Imports System.Xml
    Imports System.Data


    Module ProductFeedImport

        Sub Main()
            Try



            Dim ds As New DataSet
            Dim xmlFile As XmlReader

            Dim ArtistName As String
            Dim AlbumName As String
            Dim Price As String
            Dim URL As String
            Dim Image As String

            Dim a(1) As String

            Console.WriteLine("Starting import of remote xml file")
            Console.WriteLine



            xmlFile = XmlReader.Create("http://remotesite.com/file.xml", New XmlReaderSettings())
            ds.ReadXml(xmlFile)
            Dim i As Integer
            For i = 0 To ds.Tables(0).Rows.Count - 1

                ArtistName = convert.ToString(ds.Tables(0).Rows(i).Item(0))
                AlbumName = convert.ToString(ds.Tables(0).Rows(i).Item(0))
                a = Split(ArtistName," - ",-1)
                ArtistName = (Replace(convert.ToString(a(0)),"'",""))
                AlbumName = (Replace(convert.ToString(a(1)),"'",""))
                Price = ds.Tables(0).Rows(i).Item(3)
                URL = ds.Tables(0).Rows(i).Item(1)
                Image = ds.Tables(0).Rows(i).Item(2)

                    Console.WriteLine(ArtistName)
                    Console.WriteLine
                    Console.WriteLine(AlbumName)
                    Console.WriteLine
                    Console.WriteLine(Price)
                    Console.WriteLine
                    Console.WriteLine(Image)
                    Console.WriteLine
                    Console.WriteLine(URL)
                    Console.WriteLine
                    redim a(1)
            Next

            console.ReadLine

            Catch ex As Exception
                Console.Title = ("Product Feed Import Errors")
                Console.WriteLine("An error occurred: " & ex.Message)
                Console.WriteLine("Time of error: " & DateTime.Now)
                Console.WriteLine("press any key to continue")
                Console.ReadKey()
            End Try
        End Sub

    End Module

    Thursday, January 21, 2010 10:20 AM

Answers

  • Well, solved:

    Try

            Dim ds As New DataSet
            Dim xmlFile As XmlReader
            Dim ArtistName As String
            Dim AlbumName As String
            Dim Price As String
            Dim URL As String
            Dim Image As String
            Dim a() As String

            Console.WriteLine("Starting import of remote xml file")
            Console.WriteLine


            xmlFile = XmlReader.Create("http://pf.tradedoubler.com/export/export?myFeed=12640688111733419&myFormat=12640688111733419", New XmlReaderSettings())
            ds.ReadXml(xmlFile)
            Dim i As Integer
            For i = 0 To ds.Tables(0).Rows.Count - 1
                ArtistName = convert.ToString(ds.Tables(0).Rows(i).Item(0))
                AlbumName = convert.ToString(ds.Tables(0).Rows(i).Item(0))
                a = Split(ArtistName," - ",-1)
                ArtistName = (Replace(convert.ToString(a(0)),"'",""))
                    If a.Length > 1 Then
                        AlbumName = (Replace(Convert.ToString(a(1)), "'", ""))
                        Console.WriteLine("array value: " & a(1))
                    End If
                Price = ds.Tables(0).Rows(i).Item(3)
                URL = ds.Tables(0).Rows(i).Item(1)
                Image = ds.Tables(0).Rows(i).Item(2)
               

                    Console.WriteLine
                    Console.WriteLine(ArtistName)
                    Console.WriteLine
                    Console.WriteLine(AlbumName)
                    Console.WriteLine
                    Console.WriteLine(Price)
                    Console.WriteLine
                    Console.WriteLine(Image)
                    Console.WriteLine
                    Console.WriteLine(URL)
                    Console.WriteLine
                    Console.WriteLine()

            Next

            console.ReadLine

            Catch ex As Exception
                Console.Title = ("Product Feed Import Errors")
                Console.WriteLine("An error occurred: " & ex.Message)
                Console.WriteLine("Time of error: " & DateTime.Now)
                Console.WriteLine("press any key to continue")
                Console.ReadKey()
            End Try

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    • Proposed as answer by Heslacher Thursday, January 21, 2010 3:07 PM
    • Marked as answer by fiftygent Friday, January 22, 2010 9:26 AM
    Thursday, January 21, 2010 3:04 PM
  • Well, i recomment you to not create the sql like that try this code:

      sql = "insert into AProducts (ArtistName, AlbumName, Price, Image, URL) values(@artistname,@albumname,@price,@image,@url)"


                command = New SqlCommand(sql, connection)
    command.Parameters.AddWithValue("@artistname", ArtistName)
    command.Parameters.AddWithValue("@albumname", AlbumName)
    command.Parameters.AddWithValue("@price", Price)
    command.Parameters.AddWithValue("@image", image)
    command.Parameters.AddWithValue("@url", url)
                adpter.InsertCommand = command
                adpter.InsertCommand.ExecuteNonQuery()
                    Console.WriteLine
                    Console.WriteLine(ArtistName)



    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    • Marked as answer by fiftygent Friday, January 22, 2010 9:38 AM
    Thursday, January 21, 2010 3:43 PM

All replies

  • The code above will throw an exception if there is no instance of ' - ' in the ArtistName field which would match the exception you have described. If you code in a check and handle incorrect data does it help? You can do this by inspecting the count of the a variable after the split - if you have a count of one then your data is causing the error

    Try something like:

     

    If a.Count > 1 Then

    albumName = (Replace(Convert.ToString(a(1)),

    "'", ""))

     

    End If

    Thursday, January 21, 2010 10:51 AM
  • You haven't indicted where the error is occuring, but if it is in either of the two Replace lines, then it's because the string you are using (ArtistName) does not include a '-'. 

    You do not need the Redim a(1).  You should remove  Dim a(1) As String and you should replace 
        a = Split(ArtistName," - ",-1)
    with
        Dim a() As String = Split(ArtistName," - ",-1)

    Thursday, January 21, 2010 11:10 AM
  • I think that you have no need to define the a(1) if you put a() when you use the split automatically the a takes the lenght of the array returned by the split method.

    I agree with the others telling that the string doesnt contain the "-" to avoid this try out this code:

    Dim ds As New DataSet
            Dim xmlFile As XmlReader

            Dim ArtistName As String
            Dim AlbumName As String
            Dim Price As String
            Dim URL As String
            Dim Image As String

            Dim a() As String

            Console.WriteLine("Starting import of remote xml file")
            Console.WriteLine



            xmlFile = XmlReader.Create("http://remotesite.com/file.xml", New XmlReaderSettings())
            ds.ReadXml(xmlFile)
            Dim i As Integer
            For i = 0 To ds.Tables(0).Rows.Count - 1

                ArtistName = convert.ToString(ds.Tables(0).Rows(i).Item(0))
                AlbumName = convert.ToString(ds.Tables(0).Rows(i).Item(0))
                a = Split(ArtistName," - ",-1)
                ArtistName = (Replace(convert.ToString(a(0)),"'",""))
                if a.Length>=1 then            AlbumName = (Replace(convert.ToString(a(1)),"'",""))
                Price = ds.Tables(0).Rows(i).Item(3)
                URL = ds.Tables(0).Rows(i).Item(1)
                Image = ds.Tables(0).Rows(i).Item(2)

                    Console.WriteLine(ArtistName)
                    Console.WriteLine
                    Console.WriteLine(AlbumName)
                    Console.WriteLine
                    Console.WriteLine(Price)
                    Console.WriteLine
                    Console.WriteLine(Image)
                    Console.WriteLine
                    Console.WriteLine(URL)
                    Console.WriteLine

            Next

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 11:16 AM

  • Change this  Dim a(1) As String to Dim a as String()

    And remove the redim, it has in my idea not any sense at all, you are moving everything a new array of strings to it while using that split.




    Success
    Cor
    Thursday, January 21, 2010 11:50 AM
  • Thanks for the replies really appreciate it,

    I tried the code jtorrecilla, it is still generating the same out of bounds error and Simon will a.getlength work the same as a.count?

    If I use a different data in my xml file it still runs into the same problem and if I remove the array I will run into an incorrect Syntax near 'S error
    Thursday, January 21, 2010 11:55 AM
  • Another thing is if your array is typeof String you dont need to use Conver.Tostring(a(1))....

    could you evalueate the valur of a(1) when the error occurs....

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 12:01 PM
  • Another thing is if your array is typeof String you dont need to use Conver.Tostring(a(1))....

    could you evalueate the valur of a(1) when the error occurs....

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    like this?


                    Console.WriteLine(URL)
                    Console.WriteLine
                    Console.WriteLine("array value: " & a(1))
                    Console.WriteLine
    Thursday, January 21, 2010 12:13 PM
  • Yes... or simply with a quickwath while debugging.

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 12:21 PM
  • It's putting out the correct value just before the error:


    array value: Album name

    An error occurred: Index was outside the bounds of the array.
    Time of error: 21/01/2010 12:25:01
    press any key to continue
    Thursday, January 21, 2010 12:29 PM
  • then i think the error is in other side....

    Are you sure the datatable yo are using has 4 columns?

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 12:34 PM
  • by datatable you mean the xml file?
    Thursday, January 21, 2010 12:56 PM
  • Yes you have this code:
    Price = ds.Tables(0).Rows(i).Item(3) ' i think it could be the error...
                URL = ds.Tables(0).Rows(i).Item(1)
                Image = ds.Tables(0).Rows(i).Item(2)
    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 1:05 PM
  • I just ran it with the Price variable commented out and it was still throwing up the error and all the items in the xml file should have a price.
    Thursday, January 21, 2010 2:06 PM
  • Could you upload your proyect to Skydrive to take a look?
    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 2:11 PM
  • Hi,

    just try the code below. Its only for the Artist and Albumname.

            Dim i As Integer
            Dim ArtistAndAlbumValue As String
            Dim ArtistAndAlbumArray() As String
            Dim ArtistName As String
            Dim AlbumName As String
            Dim ds As New DataSet
            
    Dim xmlFile As XmlReader
            Console.WriteLine(
    "Starting import of remote xml file")
            Console.WriteLine()
            xmlFile = XmlReader.Create(
    "http://remotesite.com/file.xml"New XmlReaderSettings())
            ds.ReadXml(xmlFile)
            
    For i = 0 To ds.Tables(0).Rows.Count - 1
                
    If Not IsDBNull(ds.Tables(0).Rows(i).Item(0)) Then
                    ArtistAndAlbumValue = Convert.ToString(ds.Tables(0).Rows(i).Item(0))
                    ArtistAndAlbumArray = Split(ArtistAndAlbumValue, 
    " - ", -1)
                    Console.WriteLine(ArtistAndAlbumArray(0))
                    
    If ArtistAndAlbumArray.Length >= 1 Then

                        Console.WriteLine(ArtistAndAlbumArray(1))

                    
    End If

                End If

            Next


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Thursday, January 21, 2010 2:30 PM
  • http://cid-9ce15adafd589a1c.skydrive.live.com/self.aspx/Code/ProductFeedImport.zip
    • Edited by fiftygent Thursday, January 21, 2010 2:54 PM
    Thursday, January 21, 2010 2:34 PM
  • Please upload the complet solution with the xml example zipped all.

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 2:36 PM
  • You should put your project ( including the subfolders ) inside an archive. Only the .sln file isn`t enough.
    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Thursday, January 21, 2010 2:37 PM
  • I've uploaded the zip file in the location above
    Thursday, January 21, 2010 2:56 PM
  • Well, solved:

    Try

            Dim ds As New DataSet
            Dim xmlFile As XmlReader
            Dim ArtistName As String
            Dim AlbumName As String
            Dim Price As String
            Dim URL As String
            Dim Image As String
            Dim a() As String

            Console.WriteLine("Starting import of remote xml file")
            Console.WriteLine


            xmlFile = XmlReader.Create("http://pf.tradedoubler.com/export/export?myFeed=12640688111733419&myFormat=12640688111733419", New XmlReaderSettings())
            ds.ReadXml(xmlFile)
            Dim i As Integer
            For i = 0 To ds.Tables(0).Rows.Count - 1
                ArtistName = convert.ToString(ds.Tables(0).Rows(i).Item(0))
                AlbumName = convert.ToString(ds.Tables(0).Rows(i).Item(0))
                a = Split(ArtistName," - ",-1)
                ArtistName = (Replace(convert.ToString(a(0)),"'",""))
                    If a.Length > 1 Then
                        AlbumName = (Replace(Convert.ToString(a(1)), "'", ""))
                        Console.WriteLine("array value: " & a(1))
                    End If
                Price = ds.Tables(0).Rows(i).Item(3)
                URL = ds.Tables(0).Rows(i).Item(1)
                Image = ds.Tables(0).Rows(i).Item(2)
               

                    Console.WriteLine
                    Console.WriteLine(ArtistName)
                    Console.WriteLine
                    Console.WriteLine(AlbumName)
                    Console.WriteLine
                    Console.WriteLine(Price)
                    Console.WriteLine
                    Console.WriteLine(Image)
                    Console.WriteLine
                    Console.WriteLine(URL)
                    Console.WriteLine
                    Console.WriteLine()

            Next

            console.ReadLine

            Catch ex As Exception
                Console.Title = ("Product Feed Import Errors")
                Console.WriteLine("An error occurred: " & ex.Message)
                Console.WriteLine("Time of error: " & DateTime.Now)
                Console.WriteLine("press any key to continue")
                Console.ReadKey()
            End Try

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    • Proposed as answer by Heslacher Thursday, January 21, 2010 3:07 PM
    • Marked as answer by fiftygent Friday, January 22, 2010 9:26 AM
    Thursday, January 21, 2010 3:04 PM
  • Thank's seems to have got rid of the array problem but when I try to insert it into a table I get the following:

    array value: Bluegrass: It'S About Time It'S About Me
    An error occurred: Line 1: Incorrect syntax near 'S'.
    Time of error: 21/01/2010 15:18:01
    press any key to continue
    Thursday, January 21, 2010 3:21 PM
  • You havent tod this problem....


    This another one.

    Could you show the way you make the insert into the database??

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 3:35 PM
  •             URL = ds.Tables(0).Rows(i).Item(1)
                Image = ds.Tables(0).Rows(i).Item(2)

                sql = "insert into AProducts (ArtistName, AlbumName, Price, Image, URL) values('" & ArtistName & "','" & AlbumName & "','" & Price & "','" & Image & "','" & URL & "')"
                command = New SqlCommand(sql, connection)
                adpter.InsertCommand = command
                adpter.InsertCommand.ExecuteNonQuery()

                    Console.WriteLine
                    Console.WriteLine(ArtistName)
    Thursday, January 21, 2010 3:37 PM
  • Well, i recomment you to not create the sql like that try this code:

      sql = "insert into AProducts (ArtistName, AlbumName, Price, Image, URL) values(@artistname,@albumname,@price,@image,@url)"


                command = New SqlCommand(sql, connection)
    command.Parameters.AddWithValue("@artistname", ArtistName)
    command.Parameters.AddWithValue("@albumname", AlbumName)
    command.Parameters.AddWithValue("@price", Price)
    command.Parameters.AddWithValue("@image", image)
    command.Parameters.AddWithValue("@url", url)
                adpter.InsertCommand = command
                adpter.InsertCommand.ExecuteNonQuery()
                    Console.WriteLine
                    Console.WriteLine(ArtistName)



    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    • Marked as answer by fiftygent Friday, January 22, 2010 9:38 AM
    Thursday, January 21, 2010 3:43 PM
  • Thanks, I was going to change that anyway. I am going to do some more testing but all good so far have tested it on a much larger (110mb) file and it's pulling everything through!
    Thursday, January 21, 2010 4:56 PM
  • Out of curiosity would this be the way you would go about this? I had read that xmlreader would be the fastest method?
    Thursday, January 21, 2010 4:58 PM
  • I think is a good way.

    Have the problem solved?

    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 5:04 PM
  • Yes no problems so far many thanks
    Thursday, January 21, 2010 5:14 PM
  • Then please mark all the responses that satisfaces you as "Answer" for helping other users to solve problems similar from you...
    Si la respuesta te ha sido util Marcala como Respuesta o Votala.
    Mi Blog: Jtorrecilla
    Thursday, January 21, 2010 6:31 PM