IF statement is 'not working'

  • Thread starter Thread starter Maurice Mertens
  • Start date Start date
M

Maurice Mertens

Hi,

I'm using the code underneath in a function:

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim ds As New DataSet
Dim dv As New DataView
Dim strMsg As String, strComputerName As String
Dim row As DataRow
Dim lngCurrentMachineTeller As Long, lngI As Long

cn.ConnectionString = oleDBConn1.ConnectionString
cn.Open()
rs = cn.OpenSchema(ADODB.SchemaEnum.adSchemaProviderSpecific, ,
"{947bb102-5d43-11d1-bdbf-00c04fb92675}")
daAdapter.Fill(ds, rs, "USERS")
ds.Tables("USERS").Columns(0).ColumnName = "Computernaam"
ds.Tables("USERS").Columns(1).ColumnName = "Loginnaam"
ds.Tables("USERS").Columns(2).ColumnName = "Connected"
ds.Tables("USERS").Columns(3).ColumnName = "State"
dv.Table = ds.Tables("USERS")
dv.RowStateFilter = DataViewRowState.CurrentRows

For lngI = 0 To dv.Count - 1
row = dv.Item(lngI).Row
If row.Item("Computernaam").ToString = gsComputerName.ToString Then
msgbox("Same!")
Else
msgbox("Not the same!")
Endif
Next


The problem is strange behaviour in the line "If row.Item
("Computernaam").ToString = gsComputerName.ToString Then
". When in the command window I type: '?row.Item("Computernaam").ToString
= gsComputerName.ToString' it returns true. But when I debug the code
step by step it doesn't go into the if statement. Like it is 'not true'
suddenly. The variable 'gsComputerName' is a global string. They both
contain the same value but the IF STATEMENT "doesn;t work" for some
reason.



--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
 
this is really strange
" If row.Item("Computernaam").ToString = gsComputerName.ToString Then"
do you mean
" If row.Item("Computername").ToString = gsComputerName.ToString Then"??

be careful of spelling ("Computername")

Galin Iliev
 
No,

ds.Tables("USERS").Columns(0).ColumnName = "Computernaam"
The columnname is "Computernaam".

But it is very strange that in the command window the comparison returns
true and in my code the IF statement returns false for exactly the same
comparison. I also did a reboot of my PC which didn't change this result (I
don't know if I would feel more relieved when the results were in fact
different after the reboot :)..)
 
Maurice,

Why do you than not set temporaly these two instructions in your code
dim a as string = row.Item("Computernaam").ToString
dim b as string = gsComputerName.ToString
For lngI = 0 To dv.Count - 1
row = dv.Item(lngI).Row

dim a as string = row.Item("Computernaam").ToString
dim b as string = gsComputerName.ToString
If row.Item("Computernaam").ToString = gsComputerName.ToString Then
msgbox("Same!")
Else

Than it is so easy to see when you debug step by step.

Just my thought,

Cor
 
Hi Cor,

I already tried your example but this also didn't work. The same problem!
After a little research we found the reason for this behaviour. The value
row.item("Computernaam") contains a string which came from the kernel.
These strings are ended with asci character 0. So we now use a replace
and a trim: Replace(Trim(row.Item("Computernaam")), Chr(0), "") and this
is working fine now!

Thx for looking at this problem..



Maurice,

Why do you than not set temporaly these two instructions in your code
dim a as string = row.Item("Computernaam").ToString
dim b as string = gsComputerName.ToString


dim a as string = row.Item("Computernaam").ToString
dim b as string = gsComputerName.ToString


Than it is so easy to see when you debug step by step.

Just my thought,

Cor



--
Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering


Maurice Mertens
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
 
Maurice,

Really a terrible situation to find.

From this newsgroup side I would probably never thought about that when you
had not told it now.

Thanks that you told what it was and congratulated as well with that.

Cor
 
Back
Top