Solved

Problems comparing values in a datatable

Posted on 2006-11-09
10
199 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice in Xamarin 21 96
VB.net Move a class from Solution Items to a project 2 32
How do sunrise and sunset times change with altitude 14 35
Code enhancement 4 20
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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