Datatable indexing problem

  • Thread starter Thread starter Thomas Scheiderich
  • Start date Start date
T

Thomas Scheiderich

I am having a problem with a Datatable access.

This statement apparently works fine:

response.write(GetRows.Rows(ktr)(1))

and this statement does not:

if e.day.date = GetRows(ktr)(1) then

Here is the snippet of code that has these statements
**********************************************************************
Dim GetRows as DataTable = sqlDS.tables("eventCalendar")
Dim totalRows as integer
Dim totalColumns as Integer

totalRows = GetRows.Rows.Count
totalColumns = GetRows.Columns.count
response.write("Rows read again = " & totalRows & " columns = " &
totalColumns & "<br>")

for ktr = 0 to GetRows.Rows.Count - 1
response.write(GetRows.Rows(ktr)(1))
response.write( e.day.date)
if CDate(e.day.date) = _
CDate(GetRows(ktr)(1)) then
****************************************************************************
*******

Here is the error I get.
****************************************************************************
************
Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: BC30367: Class 'System.Data.DataTable' cannot be
indexed because it has no default property.

Source Error:

Line 124: response.write( e.day.date)
Line 125: if CDate(e.day.date) = _
Line 126: CDate(GetRows(ktr)(1)) then
Line 127: response.write("Dates equal")
Line 128: else
****************************************************************************
*

Why cannot the GetRows Datatable not be indexed in the if statement, but it
works fine in the response.write?

Thanks,

Tom.
 
Rows collection has no indexes by name - only by index
so if you want to get the value from the first row whitin the ktr column you
have to write

GetRows.Rows(1)(ktr)


Regards
Martin
 
Martin Marinov said:
Rows collection has no indexes by name - only by index
so if you want to get the value from the first row whitin the ktr column you
have to write

GetRows.Rows(1)(ktr)

Actually, it is the other way around. The row is the number that is
changing. Column 1 is a date I was trying to get out of the table which
happens to be in the 2nd column (starting at 0).

ktr is going to be equal to 0 and 1 (as there are 2 rows in the table at the
moment). Totalrows is = 2.

What I am confused is why the response.write line works and the if test does
not.

Tom.
 
It is may be because DataTable.Rows(RowIndex)(ColumnIndex) return object and
you have to convert it to datatime object like

if Not GetRows(ktr)(1) = Nothing Then
if e.day.date = DateTime.Parse(GetRows(ktr)(1).ToString()) then

Hope This Helps
Regards
Martin
 
Martin Marinov said:
It is may be because DataTable.Rows(RowIndex)(ColumnIndex) return object and
you have to convert it to datatime object like

if Not GetRows(ktr)(1) = Nothing Then
if e.day.date = DateTime.Parse(GetRows(ktr)(1).ToString()) then

Tried it. Still doesn't work.

Here is the result and the new code:

*************************************************************************
Compiler Error Message: BC30367: Class 'System.Data.DataTable' cannot be
indexed because it has no default property.

Source Error:

Line 124: response.write( e.day.date)
Line 125: if e.day.date = _
Line 126: DateTime.Parse(GetRows(ktr)(1).ToString) then
Line 127: response.write("Dates equal")
Line 128: else


Source File: c:\inetpub\wwwroot\contour\Company Info\TMP1lpu3zq39q.aspx
Line: 126
****************************************************************************
****
for ktr = 0 to GetRows.Rows.Count - 1
response.write(GetRows.Rows(ktr)(1))
response.write( e.day.date)
if e.day.date = _
DateTime.Parse(GetRows(ktr)(1).ToString) then
response.write("Dates equal")
else
response.write("dates not equal")
end if
next
****************************************************************************
*

It is interesting that the response.write works but the compare does not.

Tom
 
Hi Thomas,

Please excuse me but i was in hurry and missed something
this line
DateTime.Parse(GetRows(ktr)(1).ToString) then
should be
DateTime.Parse(GetRows.Rows(ktr)(1).ToString) then

as you can see i've missed the Rows collection that need to be indexed

Regards
Martin
 
Back
Top