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. Web Development
  3. ASP.NET
  4. Converting a DataTable Column to an Array?

Converting a DataTable Column to an Array?

Scheduled Pinned Locked Moved ASP.NET
helpcsharpdatabasedata-structures
8 Posts 6 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.
  • J Offline
    J Offline
    James Shao
    wrote on last edited by
    #1

    Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function: Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function

    D C K A J 7 Replies Last reply
    0
    • J James Shao

      Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function: Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Check the value in intColumn variable.

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      1 Reply Last reply
      0
      • J James Shao

        Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function: Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function

        C Offline
        C Offline
        Covean
        wrote on last edited by
        #3

        I'm not really confirm with the VB-Syntax but arrValues(i2) = dt.Rows(i2).Item(intColomn) looks like you try to access an double array that is not initialized (or doesn't have enough space). Look at you definition: Dim arrValues() As Double = New Double(){} <- maybe here you should say that this array should have dt.Rows.Count elements

        Greetings Covean

        1 Reply Last reply
        0
        • J James Shao

          Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function: Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function

          K Offline
          K Offline
          Kannan Ar
          wrote on last edited by
          #4

          Declare arrValues with a specific size. Like this

          Dim arrValues() As Double = New Double(dt.Rows.Count - 1) {}

          1 Reply Last reply
          0
          • J James Shao

            Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function: Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function

            A Offline
            A Offline
            Abhishek Sur
            wrote on last edited by
            #5

            First of all why dont you use List.... as your collection. It is always better to use ArrayList or Generic List as collection rather than going for Array. Next, I always prefer to use ForEach rather than For when I am looping through an Enumerable. If I was in your situation I would write this as :

            Private Function DataTableToArray(ByVal x As DataTable, ByVal intCol As Integer) As List(Of Double)
                Dim lst As New List(Of Double)
            
                For Each dr As DataRow In x.Rows
                    lst.Add(dr.Item(intCol))
                Next
            
                Return lst
            End Function
            

            Or if you still want to use Array you might use List(Of Double).ToArray as well. Cheers. :rose:

            Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


            My Latest Articles-->** Simplify Code Using NDepend
            Basics of Bing Search API using .NET
            Microsoft Bing MAP using Javascript

            1 Reply Last reply
            0
            • J James Shao

              Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function: Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function

              J Offline
              J Offline
              James Shao
              wrote on last edited by
              #6

              Thanks a lot guys. As soon as I'm home I'll give all of these options a try. :)

              1 Reply Last reply
              0
              • J James Shao

                Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function: Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function

                C Offline
                C Offline
                carlecomm
                wrote on last edited by
                #7

                Hi, try the following code: Dim arrValues(0 To intRows - 1) As Double instead of Dim arrValues() As Double = New Double(){}

                1 Reply Last reply
                0
                • J James Shao

                  Hi, I am having some trouble with creating a simple function that converts a column of a datatable to an array (containing all numbers). Upon running it, it says "Index was outside the bounds of the array"; however, I couldn't understand why my function would create this problem. :( Also since I do not usually program using VB.Net, I am not sure if my function has other errors (like syntax errors). Could you please help me check it out? I'd greatly appreciate it! :) Thanks in advance. Here's my VB function: Public Function DatatableToArray(ByVal dt As DataTable, ByVal intColomn As Integer) As Double() Dim intRows As Integer = dt.Rows.Count Dim arrValues() As Double = New Double(){} Dim i2 As Integer For i2 = 0 To intRows - 1 arrValues(i2) = dt.Rows(i2).Item(intColomn) Next Return arrValues End Function

                  J Offline
                  J Offline
                  James Shao
                  wrote on last edited by
                  #8

                  Thank you guys, my problem is solved! :) Greatly appreciate it.

                  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