Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. index out of range exception

index out of range exception

Scheduled Pinned Locked Moved Visual Basic
helpdatabasesecuritytutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zaimah
    wrote on last edited by
    #1

    im running this function to convert a list of code into a certain standard.. the problem is, when i choose the files (where i get the value of coop) and directory (where i compare coop with the list in the directory) this msg come out "index out of range exception". But if i choose local file and directory example file from my desktop and my d:/ there's np error.. but if i choose the file from other map drive the error happen at the Catch part.

    Private Sub SEMAK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SEMAK.Click

        Dim fileName As String = TextBox1.Text.Trim
        Dim lineNumber As Integer = 0
        Dim sr As StreamReader = New StreamReader(fileName)
        Dim conn As New SqlClient.SqlConnection("Data Source=10.0.0.70;User ID= sysadm;Password=sysadm;Initial Catalog=SPGA\_Latihan;Persist Security Info=True;")
    
        Dim tarikh As String = ""
        Dim runNo As String = ""
        Dim coop As String = ""
        Dim line As String = ""
    
    
        Do
            line = sr.ReadLine()
    
            If line <> Nothing Then
                Try
                    tarikh = line.Substring(0, 8).Trim()
                    runNo = line.Substring(9, 5).Trim()
                    coop = line.Substring(15).Trim()
    
                Catch ex As Exception
                    Console.Write(ex.ToString())
                End Try
    
                Dim strSQL As String = "INSERT INTO portal (tarikh,runNo, coop) VALUES (@tarikh, @runNo, @coop)"
                Dim cmd As New System.Data.SqlClient.SqlCommand(strSQL, conn)
    
                conn.Open()
    
                cmd.Parameters.Add(New SqlClient.SqlParameter("@tarikh", SqlDbType.Char, 8))
                cmd.Parameters("@tarikh").Value = tarikh
    
                cmd.Parameters.Add(New SqlClient.SqlParameter("@runNo", SqlDbType.Char, 20))
                cmd.Parameters("@runNo").Value = runNo
    
                cmd.Parameters.Add(New SqlClient.SqlParameter("@coop", SqlDbType.VarChar, 100))
                cmd.Parameters("@coop").Value = coop
    
                cmd.ExecuteNonQuery()
                conn.Close()
                
                Dim dirB1 As DirectoryInfo = New DirectoryInfo(TextBox2.Text)
    
    
    
    
    
                Try
    
                    convertCoop(coop)
    
    
    
                    Dim dA1 As DirectoryInfo
                    Dim dirB2 As FileSystemInfo() = dirB1.GetDirectories
    
                    Dim strSQL2 As String = "SELECT folder, portal FROM  kodPortal"
    
    C S 2 Replies Last reply
    0
    • Z zaimah

      im running this function to convert a list of code into a certain standard.. the problem is, when i choose the files (where i get the value of coop) and directory (where i compare coop with the list in the directory) this msg come out "index out of range exception". But if i choose local file and directory example file from my desktop and my d:/ there's np error.. but if i choose the file from other map drive the error happen at the Catch part.

      Private Sub SEMAK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SEMAK.Click

          Dim fileName As String = TextBox1.Text.Trim
          Dim lineNumber As Integer = 0
          Dim sr As StreamReader = New StreamReader(fileName)
          Dim conn As New SqlClient.SqlConnection("Data Source=10.0.0.70;User ID= sysadm;Password=sysadm;Initial Catalog=SPGA\_Latihan;Persist Security Info=True;")
      
          Dim tarikh As String = ""
          Dim runNo As String = ""
          Dim coop As String = ""
          Dim line As String = ""
      
      
          Do
              line = sr.ReadLine()
      
              If line <> Nothing Then
                  Try
                      tarikh = line.Substring(0, 8).Trim()
                      runNo = line.Substring(9, 5).Trim()
                      coop = line.Substring(15).Trim()
      
                  Catch ex As Exception
                      Console.Write(ex.ToString())
                  End Try
      
                  Dim strSQL As String = "INSERT INTO portal (tarikh,runNo, coop) VALUES (@tarikh, @runNo, @coop)"
                  Dim cmd As New System.Data.SqlClient.SqlCommand(strSQL, conn)
      
                  conn.Open()
      
                  cmd.Parameters.Add(New SqlClient.SqlParameter("@tarikh", SqlDbType.Char, 8))
                  cmd.Parameters("@tarikh").Value = tarikh
      
                  cmd.Parameters.Add(New SqlClient.SqlParameter("@runNo", SqlDbType.Char, 20))
                  cmd.Parameters("@runNo").Value = runNo
      
                  cmd.Parameters.Add(New SqlClient.SqlParameter("@coop", SqlDbType.VarChar, 100))
                  cmd.Parameters("@coop").Value = coop
      
                  cmd.ExecuteNonQuery()
                  conn.Close()
                  
                  Dim dirB1 As DirectoryInfo = New DirectoryInfo(TextBox2.Text)
      
      
      
      
      
                  Try
      
                      convertCoop(coop)
      
      
      
                      Dim dA1 As DirectoryInfo
                      Dim dirB2 As FileSystemInfo() = dirB1.GetDirectories
      
                      Dim strSQL2 As String = "SELECT folder, portal FROM  kodPortal"
      
      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Have you bothered to walk through the code in te debugger ? I assume this means you're not getting any data back from your database.

      Christian Graus Driven to the arms of OSX by Vista.

      Z 1 Reply Last reply
      0
      • Z zaimah

        im running this function to convert a list of code into a certain standard.. the problem is, when i choose the files (where i get the value of coop) and directory (where i compare coop with the list in the directory) this msg come out "index out of range exception". But if i choose local file and directory example file from my desktop and my d:/ there's np error.. but if i choose the file from other map drive the error happen at the Catch part.

        Private Sub SEMAK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SEMAK.Click

            Dim fileName As String = TextBox1.Text.Trim
            Dim lineNumber As Integer = 0
            Dim sr As StreamReader = New StreamReader(fileName)
            Dim conn As New SqlClient.SqlConnection("Data Source=10.0.0.70;User ID= sysadm;Password=sysadm;Initial Catalog=SPGA\_Latihan;Persist Security Info=True;")
        
            Dim tarikh As String = ""
            Dim runNo As String = ""
            Dim coop As String = ""
            Dim line As String = ""
        
        
            Do
                line = sr.ReadLine()
        
                If line <> Nothing Then
                    Try
                        tarikh = line.Substring(0, 8).Trim()
                        runNo = line.Substring(9, 5).Trim()
                        coop = line.Substring(15).Trim()
        
                    Catch ex As Exception
                        Console.Write(ex.ToString())
                    End Try
        
                    Dim strSQL As String = "INSERT INTO portal (tarikh,runNo, coop) VALUES (@tarikh, @runNo, @coop)"
                    Dim cmd As New System.Data.SqlClient.SqlCommand(strSQL, conn)
        
                    conn.Open()
        
                    cmd.Parameters.Add(New SqlClient.SqlParameter("@tarikh", SqlDbType.Char, 8))
                    cmd.Parameters("@tarikh").Value = tarikh
        
                    cmd.Parameters.Add(New SqlClient.SqlParameter("@runNo", SqlDbType.Char, 20))
                    cmd.Parameters("@runNo").Value = runNo
        
                    cmd.Parameters.Add(New SqlClient.SqlParameter("@coop", SqlDbType.VarChar, 100))
                    cmd.Parameters("@coop").Value = coop
        
                    cmd.ExecuteNonQuery()
                    conn.Close()
                    
                    Dim dirB1 As DirectoryInfo = New DirectoryInfo(TextBox2.Text)
        
        
        
        
        
                    Try
        
                        convertCoop(coop)
        
        
        
                        Dim dA1 As DirectoryInfo
                        Dim dirB2 As FileSystemInfo() = dirB1.GetDirectories
        
                        Dim strSQL2 As String = "SELECT folder, portal FROM  kodPortal"
        
        S Offline
        S Offline
        Steven J Jowett
        wrote on last edited by
        #3

        Have you remembered that the index for substring starts at 0, therefore SubString(15) would return the string from the 16th character of the string.

        Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

        Z 1 Reply Last reply
        0
        • C Christian Graus

          Have you bothered to walk through the code in te debugger ? I assume this means you're not getting any data back from your database.

          Christian Graus Driven to the arms of OSX by Vista.

          Z Offline
          Z Offline
          zaimah
          wrote on last edited by
          #4

          i am using the debugger.. the thing is, i can read data from the files but when it comes to this part in convertCoop, i cannot get value from "portal" but the value of "coop" is ok. after this part, at the Catch part, the msg "index out of range exception" come out. is it bcoz the directory that i choose got a lot (thounsands) of folder n files? bcoz when i choose directory that only have a few folders n files, it runs smoothly.. no errors, and i can see all the missing files in that directory.

          portal = ds.Tables(0).Rows(0).Item("portal")

          1 Reply Last reply
          0
          • S Steven J Jowett

            Have you remembered that the index for substring starts at 0, therefore SubString(15) would return the string from the 16th character of the string.

            Steve Jowett ------------------------- Real programmers don't comment their code. If it was hard to write, it should be hard to read.

            Z Offline
            Z Offline
            zaimah
            wrote on last edited by
            #5

            i dont have any problems with my substring.. i can read data from file using that code.. as u can see my reply at Christian answers, the problem start if i choose directory that have thousands of folder n files. but if a choose a directory that have just a few folder n files.. it can run and can get output from the prgm..

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups