?
Solved

Problems comparing values in a datatable

Posted on 2006-11-09
10
Medium Priority
?
202 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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