Problems comparing values in a datatable

Rogue 1

Member
Joined
Oct 5, 2006
Messages
12
Programming Experience
Beginner
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).

VB.NET:
[FONT=Courier New][FONT=Courier New][COLOR=blue][FONT=Courier New][COLOR=blue]Dim tbl_WinningDrawing As New DataTable()[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Dim Dr As DataRow[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Dim Count As Int16[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]OleDbDataAdapter1.Fill(tbl_WinningDrawing)[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]OleDbDataAdapter1.Fill(DataSet11)[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]Count = 0[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]For Each Dr In tbl_WinningDrawing.Rows[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]   [B]If Me.TextBox1.Text Is Me.DataSet11.tbl_WinningNumbers.WinningDrawingColumn Then[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]  Count = Count + 1[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]End If[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Next[/COLOR][/FONT]
[/COLOR][/FONT][/FONT]


The problem is the syntax that I have bolded 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.

VB.NET:
[FONT=Courier New][B][COLOR=blue][FONT=Courier New]If[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Me[/COLOR].TextBox1.Text [COLOR=blue]Is[/COLOR] [COLOR=blue]Me[/COLOR].DataSet11.tbl_WinningNumbers.WinningDrawingColumn [COLOR=blue]Then[/COLOR][/FONT][/B][/FONT]

Thanks for any help
 
Last edited:
Try comparing it with '=' not 'IS'. Also, compare to the row.

VB.NET:
if dr.WinningDrawingColumn = textBoxValue Then
 
I get the error Dr.WinningDrawingColumn "is not a member of system.data.row"

VB.NET:
If [B]Dr.WinningDrawingColumn[/B] = Me.TextBox1.Text Then

No clue why that does not work.
 
whoops, I just noticed you are on the 1.1 framework. I don't know if stongly typed rows are available in net 1.1. Someone else will know the best way to do this.
 
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).

VB.NET:
[FONT=Courier New][FONT=Courier New][FONT=Courier New][COLOR=blue][FONT=Courier New][COLOR=blue]Dim tbl_WinningDrawing As New DataTable()[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Dim Dr As DataRow[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Dim Count As Int16[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]OleDbDataAdapter1.Fill(tbl_WinningDrawing)[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]OleDbDataAdapter1.Fill(DataSet11)[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]Count = 0[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]For Each Dr In tbl_WinningDrawing.Rows[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue] [B]If Me.TextBox1.Text Is Me.DataSet11.tbl_WinningNumbers.WinningDrawingColumn Then[/B][/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Count = Count + 1[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]End If[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]Next[/COLOR][/FONT]
[/COLOR][/FONT][/FONT][/FONT]

The problem is the syntax that I have bolded 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.

VB.NET:
[FONT=Courier New][B][COLOR=blue][FONT=Courier New]If[/FONT][/COLOR][FONT=Courier New] [COLOR=blue]Me[/COLOR].TextBox1.Text [COLOR=blue]Is[/COLOR] [COLOR=blue]Me[/COLOR].DataSet11.tbl_WinningNumbers.WinningDrawingColumn [COLOR=blue]Then[/COLOR][/FONT][/B][/FONT]

Thanks for any help

Last I checked, IS is for assessing the memory location of two particular objects to see whether they refer to the same object.
While I understand you may be following advice along the notions of this article:
http://www.devx.com/vb2themax/Tip/18788

You would be better off using .Equals() to compare strings, rather than Is (apparently using the = sign is quite slow)
For an idea why, type
?"abc" Is "abc"
into the Immediate window -> false
?"abc.Equals("abc")
--> true


The actual problem you are now facing:

I get the error Dr.WinningDrawingColumn "is not a member of system.data.row"



For Each Dr In tbl_WinningDrawing.Rows

the DR variable, is typed as a DataRow. DataRow has no entry for a WinningDrawingColumn because it is generic.

There's a lot I cannot figure out in your code - You appear to have a typed dataset called DataSet11 - why would you then use untyped datatables?

Your code would be better looking something like this:

VB.NET:
OleDbDataAdapter1.Fill(DataSet11.tblWinningDrawingWhatever)
For Each dr as WinningDrawingWhateverROW in DataSet11.tblWinningDrawingWhatever.Rows
  If Me.TextBox1.Text.Equals(dr.WinningDrawing) Then
    ...

Do please name your variables better - all this DataSet11, TextBox1 doesnt make things easier for you, or us.


To count the number of rows in a data table you use this property:

MyDataTable.Rows.Count



To find something in a datatable, you dont mess with looping over each row, you say:

Dim rows as DataRow() = MyDataTable.Select("[WinningColumn] = 'Some Value Im Looking For'"

And you get an array of datarows (yes, even on a typed datatable i think this method returns generic dataRow array but it should be castable into a typed array or typed rows) that match the criteria
 
You would be better off using .Equals() to compare strings, rather than Is (apparently using the = sign is quite slow)
This is good to know. I will start using .Equals(). I will need to research other methods that are more efficient.
There's a lot I cannot figure out in your code - You appear to have a typed dataset called DataSet11 - why would you then use untyped datatables?
Not suprised. I am new to programming in general. I need to do some reading on typed vs untyped datatables. The reason why I did it that way was because I didn't know any better. I assume that you do not want to use both typed & untyped.
To find something in a datatable, you dont mess with looping over each row, you say:

Dim rows as DataRow() = MyDataTable.Select("[WinningColumn] = 'Some Value Im Looking For'"
I will work with this method which seems much more efficient. I usually have a good idea of what I want to do. However I have no clue about these methods, the syntax needed, or the most effiecent ways of doing things. It will just take time and effort for me to get it right. Thanks
 
This is good to know. I will start using .Equals(). I will need to research other methods that are more efficient.

vis781 dug up some wisdom from somewhere that had tested .Equals to be the fastest string comparator except in some rare cases. = apparently does something weird and non-.NET which involves translation in and out of .NET managed space, thereby slower. In a loop this might add up.

Another tip for .Equals(), if youre testing something against a known value, write it like this (the strange way round):

If "Hello".Equals(myVar) Then

You could write it like this (the readable way round):
If myVar.Equals("Hello") Then
but if myVar was Nothing, the program would crash.. The strange way round, you know that "Hello" is always going to have a value and it saves writing this:

If myVar IsNot Nothing AndAlso myVar.Equals("Hello") Then

Which is what you'd have to do to avoid a crash when doing things the readable way round

Not suprised. I am new to programming in general. I need to do some reading on typed vs untyped datatables. The reason why I did it that way was because I didn't know any better. I assume that you do not want to use both typed & untyped.

Untyped is fine but it is prone to error because the IDE cannot help you write the code or check what you have written.

Suppose you had a DataSet called MyDataSet that had a table called MyDataTable. Either of these will work:

MyDataSetInstance.MyDataTable
MyDataSetInstance.Tables("MyDataTable")

But with the second one you will get the MyDataTable object passed back to you wrapped up looking like a generic DataTable. If you pressed "period" to get the properties on intellisense you would only see what a basic DataTable has available - none of your specific columns, even though they are there. To get round this, you would write:

DirectCast(MyDataSetInstance.Tables("MyDataTable"), MyDataSet.MyDataTable)

Starting to get long winded this, isnt it? Get the table based on its name and cast it from generic representation to specific..


Basically, always worked with typed things if you have them available, it reduces your brain-burn in remembering names, reduces wasted time in looking up things you cant remember, reduces coding legwork and typing DirectCast all over the place

I will work with this method which seems much more efficient. I usually have a good idea of what I want to do. However I have no clue about these methods, the syntax needed, or the most effiecent ways of doing things. It will just take time and effort for me to get it right. Thanks
It might or might not be more efficient - Microsoft might have just written a loop same as you , or they may have some funky trick to make it faster. The important part is they already invented that wheel - use their version if it will do, it will reduce the clutter in your code and make your coding life easier.
 
Back
Top