Solved

Problems comparing values in a datatable

Posted on 2006-11-09
10
201 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:rogue_1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Author Comment

by:rogue_1
ID: 17910911
I forgot to state that I have VS 2003 with 1.1 framework
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17910991
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
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 17911042
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
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:rogue_1
ID: 17911164
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.
0
 

Author Comment

by:rogue_1
ID: 17911185
Ok gangwisch, let me work with you suggestion.  My previous response was using newyuppie's response
0
 

Author Comment

by:rogue_1
ID: 17911248
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.  
0
 
LVL 6

Accepted Solution

by:
riyazthad earned 500 total points
ID: 17911451
try this


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 Convert.todate(Me.TextBox1.Text) Is Convert.toDate(Dr("WinningDrawingColumn")) Then
  Count = Count + 1
End If
Next
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17912963
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
0
 

Author Comment

by:rogue_1
ID: 17913822
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
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17913937
rogue_1

No problem.  Glad it's sorted.

Roger
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question