Link to home
Start Free TrialLog in
Avatar of rogue_1
rogue_1

asked on

Problems comparing values in a datatable

This is the first time that I have tried to loop through a datatable. I know there are better methods to count rows but I was making sure that the loop worked. My next step is to take a value (TextBox1) and compare it to a value in each row of the datatable (tbl_WinningNumbers).

Code:
Dim tbl_WinningDrawing As New DataTable()
Dim Dr As DataRow
Dim Count As Int16
 
OleDbDataAdapter1.Fill(tbl_WinningDrawing)
OleDbDataAdapter1.Fill(DataSet11)
 
Count = 0
For Each Dr In tbl_WinningDrawing.Rows
   If Me.TextBox1.Text Is Me.DataSet11.tbl_WinningNumbers.WinningDrawingColumn Then
  Count = Count + 1
End If
Next


The problem is the syntax that I have below between the If Then part of the statement. It seems that all the tutorials and examples I find do not any reference to objects such as a textboxes, datatables, etc.  


Code:
If Me.TextBox1.Text = Me.DataSet11.tbl_WinningNumbers.WinningDrawingColumn Then

Thanks for any help
Avatar of rogue_1
rogue_1

ASKER

I forgot to state that I have VS 2003 with 1.1 framework
try the loop like this:

Count = 0
For Each Dr In tbl_WinningDrawing.Rows
   If Me.TextBox1.Text = dr("WinningDrawingColumn").ToString() Then
  Count = Count + 1
End If
Next
dim dr() as datarow = Me.DataSet11.tbl_WinningNumbers.Select("WinningDrawingColumn=myvalue")
for i as integer=0 to dr.getupperbound(0)
'to loop through all winningdrawingcolumns
next

or

dr.getupperbound(0)+1 will equal the count of records
Avatar of rogue_1

ASKER

It still does not work.  At least the code you suggested removed an error.

The table has 1 column "WinningDrawing" with 4 records:

10/11/2006
10/14/2006
10/18/2006
10/21/2006

I am passing 10/11/2006 in TextBox1.Text.  It should match just 1 record however it doesn't.
I have tried the same method with a string instead of dates and still did not work.
Avatar of rogue_1

ASKER

Ok gangwisch, let me work with you suggestion.  My previous response was using newyuppie's response
Avatar of rogue_1

ASKER

The loop or the count is not the problem.  I can loop thru the table with no problem.  The problem is comparing a textbox value to
a column in a datatable.  
ASKER CERTIFIED SOLUTION
Avatar of riyazthad
riyazthad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One reason why your original code would not work may be that were referring to the wrong datatable.  Compare this

   OleDbDataAdapter1.Fill(tbl_WinningDrawing)

and this

   For Each Dr In tbl_WinningDrawing.Rows

in both of which you are referencing tbl_WinningDrawing with this

   If Me.TextBox1.Text Is Me.DataSet11.tbl_WinningNumbers.WinningDrawingColumn Then

in which you are referencing tbl_WinningNumbers.  It is conceivable, I suppose, depending on declarations that you do not show, that they are the same object.  But my guess is that there is an error in the reference in the last lines I have quoted and that it was probably that error that was got rid of when you tried newyuppie's code.

The problem then is perhaps that you are trying to compare different things.  Even if they are ostensibly the same datatype - e.g. by converting the value in the column to string, or by converting the value in the textbox to a date - they may look the same but have different values.  To illustrate what I mean, try this code

        Dim s As String = "01/01/2006"
        Dim d As Date = CDate(s)
        MsgBox(s & vbCrLf & d.ToString)

You may be able to overcome this with Convert.ToDateTime() on both sides of the equation - as riyazthad suggests (although referring Convert.ToDate, which I don't think exists, rather than Convert.ToDateTime; and the approach would need "=" rather than "Is") - but that will depend on precisely what datatype and value is in your datatable column.  Access databases, for instance, always store "dates" as DateTime, even if they only ever show the date part.

So what I suggest you do is put in some temporary debugging code to see what dr("WinningDrawingColumn").ToString() is returning, and adapt the precise form of the comparison you use to take account of that.

Roger
Avatar of rogue_1

ASKER

Yeah, riyazthad got me on the right path and it did require the = operator and Convert.ToDateTime.

Great comments Sancler.  A guy I work with is a VB6 dev and he told me that comparing dates can be a pain.  Being new to programming and going into VB.net can be quite a challenge but I am learning.  I was going to give you an assisted answer but this is my first question and have no clue how to do that yet.  Sorry
rogue_1

No problem.  Glad it's sorted.

Roger