System.IndexOutOfRangeException Error?

Shihtzu

Member
Joined
Nov 24, 2006
Messages
16
Programming Experience
Beginner
hi there,

sorry this is the third thread I created :(

based on the last 2 thread help I managed to store the labels into array and figured out about the date

how do I actually extracted all the data from 1 colum out? my select query is to abstract few rows of interger depending on what the user key in.

how do I actually check how many rows are inside there?

say I have a
dataset called result
dataAdapter called dataAdapter1

dataAdapter.fill(result,"facility")

I might have 1 to 14 rows inside depending on the sql select command
 
To further elaborate my situation, from the data extracted from the dataset I want to check to see whether either of them is 900,1000,1100,1200

sorry for being long winded :(
 
hmm no replies :(

the ultimate objective is to check if the dataset contain no more rows in there

Dim i as Integer = 0
Dim timeofuse() As integer

While result1.Tables("Facility").Rows(i).Items("TimeOfUse")
timeofuse(i) = result1.Tables("Facility").Rows(i).Items("TimeOfUse")


after managing to add into timeofuse(i)
how do I check if this array store how many different set of values?
 
Here is a suggestion code
VB.NET:
[SIZE=2][COLOR=#0000ff]    [COLOR=Black][COLOR=Blue]Private Sub[/COLOR] Button8_Click([COLOR=Blue]ByVal [/COLOR]sender [COLOR=Blue]As [/COLOR]System.Object, [COLOR=Blue]ByVal [/COLOR]e [COLOR=Blue]As [/COLOR]System.EventArgs) [COLOR=Blue]Handles [/COLOR]Button8.Click
        [COLOR=Blue]Dim [/COLOR]dtable [COLOR=Blue]As [/COLOR]DataTable = [COLOR=Blue]New [/COLOR]DataTable
        dtable.Columns.Add([COLOR=Blue]New [/COLOR]DataColumn([COLOR=DarkRed]"Test1"[/COLOR], [COLOR=Blue]GetType[/COLOR]([COLOR=Blue]String[/COLOR])))
        dtable.Columns.Add([COLOR=Blue]New [/COLOR]DataColumn([COLOR=DarkRed]"Test2"[/COLOR], [COLOR=Blue]GetType[/COLOR]([COLOR=Blue]String[/COLOR])))
        [COLOR=Blue]Dim [/COLOR]row [COLOR=Blue]As [/COLOR]DataRow = dtable.NewRow
        row.Item([COLOR=DarkRed]"Test1"[/COLOR]) = [COLOR=DarkRed]"1"[/COLOR]
        row.Item([COLOR=DarkRed]"Test2"[/COLOR]) = [COLOR=DarkRed]"2"[/COLOR]
        dtable.Rows.Add(row)
        row = dtable.NewRow
        row.Item([COLOR=DarkRed]"Test1"[/COLOR]) = [COLOR=DarkRed]"1"[/COLOR]
        row.Item([COLOR=DarkRed]"Test2"[/COLOR]) = [COLOR=DarkRed]"3"[/COLOR]
        dtable.Rows.Add(row)
        [COLOR=Blue]Dim [/COLOR]obj() = GetDistinctValues(dtable, [COLOR=DarkRed]"Test1"[/COLOR])
        MsgBox(obj.Length)
    [COLOR=Blue]End Sub[/COLOR]

    [COLOR=Blue]Public Function[/COLOR] GetDistinctValues([COLOR=Blue]ByVal [/COLOR]dtable [COLOR=Blue]As [/COLOR]DataTable, [COLOR=Blue]ByVal [/COLOR]colName [COLOR=Blue]As [/COLOR]String) [COLOR=Blue]As Object[/COLOR]()
        [COLOR=Blue]Dim [/COLOR]hTable [COLOR=Blue]As [/COLOR]Hashtable = [COLOR=Blue]New [/COLOR]Hashtable
        [COLOR=Blue]Dim [/COLOR]drow [COLOR=Blue]As [/COLOR]DataRow
        [COLOR=Blue]For Each[/COLOR] drow [COLOR=Blue]In [/COLOR]dtable.Rows
            [COLOR=Blue]Try[/COLOR]
                hTable.Add(drow(colName), String.Empty)
            [COLOR=Blue]Catch[/COLOR] ex [COLOR=Blue]As [/COLOR]Exception
            [COLOR=Blue]End Try[/COLOR]
        [COLOR=Blue]Next
        Dim[/COLOR] objArray [COLOR=Blue]As Object[/COLOR]() 'hTable.Keys.Count)
        [COLOR=Blue]ReDim [/COLOR]objArray(hTable.Keys.Count - 1)
        hTable.Keys.CopyTo(objArray, 0)
        [COLOR=Blue]Return [/COLOR]objArray
    [COLOR=Blue]End Function[/COLOR][/COLOR][/COLOR][/SIZE]

 
Last edited by a moderator:
VB.NET:
[SIZE=2][COLOR=#0000ff][COLOR=Black]
    [COLOR=Blue]For Each[/COLOR] drow [COLOR=Blue]In [/COLOR]dtable.Rows
        [COLOR=Blue]Try[/COLOR]
            hTable.Add(drow(colName), String.Empty)
        [COLOR=Blue]Catch[/COLOR] ex [COLOR=Blue]As [/COLOR]Exception
        [COLOR=Blue]End Try[/COLOR]
    [COLOR=Blue]Next[/COLOR][COLOR=Blue][/COLOR][/COLOR][/COLOR][/SIZE]

It's never good practice to design for expected errors. You should perhaps first check to see if the hashTable contains the value before adding it instead of trying to add it and allowing it to error out.
 
hmm no replies :(

the ultimate objective is to check if the dataset contain no more rows in there

DataSets dont contain rows, they contain DataTables. DataTables contain rows. Please ensure you use the correct terms..

a datatable's rows collection, like all collections, has a .Count property:

myDataTable.Rows.Count



thats how you tell how many rows the table has..
 
thanks cjard, you are right :D

I used .rows.count and managed to calculate how many rows whereby I can proceed to use a loop
 
Back
Top