Why does this code not generate an error?

Question

Hi Group,

Why does the below code not generate an error? I have a table with x
number of records. Each record is read from the DB into a hashtable.
Then I add to the hashtable additional keys. From here, i run through
each key in the hashtable and change the associated value. Now, when I
update the DB with the keys and values from the hashtable, all works
well. However, I also added to the hashtable extra items. So, as far
as I am concerned, when the code tries to do an update for a record that
does not exist in the table it should generate an error because it could
not find it. However, it just ignores it and continues as if everything
is great. I have not added any error handling to the code so in theory
it should complain.

Here is the code.

Dim FileTable as new HashTable

ScanDA = New SqlDataAdapter
ScanConStr = "server=localhost;uid=SA;password=123;database=myD B"
ScanConn = New SqlConnection(ScanConStr)
ScanQuery = "SELECT FilePath,Comment FROM myTable"
ScanCMD = New SqlCommand(ScanQuery, ScanConn)

ScanDA.SelectCommand = ScanCMD
ScanConn.Open()
For Each HashLine As DictionaryEntry In FileTable

UpdateMD5Cmd = "UPDATE myTable SET " & Chr(34) & "newMD5" & Chr(34) &
"=''updated'' WHERE " & Chr(34) & "FilePath" & Chr(34) & "=''" &
HashLine.Key & "''"

ScanCMD.CommandText = UpdateMD5Cmd
ScanCMD.ExecuteNonQuery()

Next

ScanConn.Close()

So, it loads all the file names and comments into the hashtable
(filename is the key and the comment is the value). Then it processes
the comments. After that, it uploads the keys back into the FilePath
column and the values back into the comments column.
So, it should throw an error because it cannot find the new files i
added to the hashtable.

I hope this is clear! Thanks.

--

Daniel
MCSE, MCP I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.

Solution

"Daniel" <da****@madridmadridsoleado.com> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
Hi Group,

Why does the below code not generate an error? I have a table with x
number of records. Each record is read from the DB into a hashtable. Then
I add to the hashtable additional keys. From here, i run through each key
in the hashtable and change the associated value. Now, when I update the
DB with the keys and values from the hashtable, all works well. However,
I also added to the hashtable extra items. So, as far as I am concerned,
when the code tries to do an update for a record that does not exist in
the table it should generate an error because it could not find it.
However, it just ignores it and continues as if everything is great. I
have not added any error handling to the code so in theory it should
complain.

Here is the code.

Dim FileTable as new HashTable

ScanDA = New SqlDataAdapter
ScanConStr = "server=localhost;uid=SA;password=123;database=myD B"
ScanConn = New SqlConnection(ScanConStr)
ScanQuery = "SELECT FilePath,Comment FROM myTable"
ScanCMD = New SqlCommand(ScanQuery, ScanConn)

ScanDA.SelectCommand = ScanCMD
ScanConn.Open()
For Each HashLine As DictionaryEntry In FileTable

UpdateMD5Cmd = "UPDATE myTable SET " & Chr(34) & "newMD5" & Chr(34) &
"=''updated'' WHERE " & Chr(34) & "FilePath" & Chr(34) & "=''" & HashLine.Key
& "''"

ScanCMD.CommandText = UpdateMD5Cmd
ScanCMD.ExecuteNonQuery()

Next

ScanConn.Close()

So, it loads all the file names and comments into the hashtable (filename
is the key and the comment is the value). Then it processes the comments.
After that, it uploads the keys back into the FilePath column and the
values back into the comments column.
So, it should throw an error because it cannot find the new files i added
to the hashtable.

I hope this is clear! Thanks.

--

Daniel
MCSE, MCP I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.

You are allowed to update 0 records, so if it cannot find the record, it
will simply not update anything.

Perhaps I am not understanding, but it seems that this is the case.



1) You might try using string.format for creating the command string -
makes things a touch more readable

e.g.
private const CMD_STRING as string = "UPDATE myTable SET newMD5 =
''updated'' WHERE FilePath = ''{0}''"

UpdateMD5Cmd= string.Format(CMD_STRING, cstr(HashLine.Key))
2) Debug.writeline() the command strings and them try to run them by
hand to see if they update anything - it is possible that they are
finding no records to update (say a case comparison problem) and thus
are not updating anything but not giving any errors.
Alan.


Rick Mogstad wrote:
"Daniel" <da****@madridmadridsoleado.com> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
Hi Group,

Why does the below code not generate an error? I have a table with x
number of records. Each record is read from the DB into a hashtable. Then
I add to the hashtable additional keys. From here, i run through each key
in the hashtable and change the associated value. Now, when I update the
DB with the keys and values from the hashtable, all works well. However,
I also added to the hashtable extra items. So, as far as I am concerned,
when the code tries to do an update for a record that does not exist in
the table it should generate an error because it could not find it.
However, it just ignores it and continues as if everything is great. I
have not added any error handling to the code so in theory it should
complain.

Here is the code.

Dim FileTable as new HashTable

ScanDA = New SqlDataAdapter
ScanConStr = "server=localhost;uid=SA;password=123;database=myD B"
ScanConn = New SqlConnection(ScanConStr)
ScanQuery = "SELECT FilePath,Comment FROM myTable"
ScanCMD = New SqlCommand(ScanQuery, ScanConn)

ScanDA.SelectCommand = ScanCMD
ScanConn.Open()
For Each HashLine As DictionaryEntry In FileTable

UpdateMD5Cmd = "UPDATE myTable SET " & Chr(34) & "newMD5" & Chr(34) &
"=''updated'' WHERE " & Chr(34) & "FilePath" & Chr(34) & "=''" & HashLine.Key
& "''"

ScanCMD.CommandText = UpdateMD5Cmd
ScanCMD.ExecuteNonQuery()

Next

ScanConn.Close()

So, it loads all the file names and comments into the hashtable (filename
is the key and the comment is the value). Then it processes the comments.
After that, it uploads the keys back into the FilePath column and the
values back into the comments column.
So, it should throw an error because it cannot find the new files i added
to the hashtable.

I hope this is clear! Thanks.

--

Daniel
MCSE, MCP I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.


You are allowed to update 0 records, so if it cannot find the record, it
will simply not update anything.

Perhaps I am not understanding, but it seems that this is the case.

Hmmm, so then, how can I get VB to throw an error if there is nothing to
update?

--

Daniel
MCSE, MCP I, MCP in Windows 2000/NT

--------------------------------------
remove the 2nd madrid from my mail address to contact me.