Solved

Problems comparing values in a datatable

Posted on 2006-11-09
10
196 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
Comment Utility
I forgot to state that I have VS 2003 with 1.1 framework
0
 
LVL 13

Expert Comment

by:newyuppie
Comment Utility
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
Comment Utility
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
 

Author Comment

by:rogue_1
Comment Utility
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
Comment Utility
Ok gangwisch, let me work with you suggestion.  My previous response was using newyuppie's response
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:rogue_1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
rogue_1

No problem.  Glad it's sorted.

Roger
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now