469,950 Members | 2,282 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Why does this code not generate an error?

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.
Nov 21 '05 #1
16 1040

"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.

Nov 21 '05 #2

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.

Nov 21 '05 #3
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.
Nov 21 '05 #4
"Daniel" <da****@madridmadridsoleado.com> schrieb
Hmmm, so then, how can I get VB to throw an error if there is
nothing to update?

ExecuteNonQuery is a function.
Armin

Nov 21 '05 #5
Daniel,

Why don't you just try it with a complete update command (nothing variable),
from what you are sure that it is incorrect and sees what it does. Now you
keeping to much dependencies.

Just my thought,

Cor
Nov 21 '05 #6
Cor Ligthert wrote:
Daniel,

Why don't you just try it with a complete update command (nothing variable),
from what you are sure that it is incorrect and sees what it does. Now you
keeping to much dependencies.

Just my thought,

Cor


How would that differ from what I have written? I know that the code is
correct so this seems to be something by design. But I need VB to throw
an error if it cannot find the record. Any ideas?

--

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

--------------------------------------
remove the 2nd madrid from my mail address to contact me.
Nov 21 '05 #7
Armin Zingler wrote:
"Daniel" <da****@madridmadridsoleado.com> schrieb
Hmmm, so then, how can I get VB to throw an error if there is
nothing to update?


ExecuteNonQuery is a function.
Armin


Can you suggest an alternative?

--

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

--------------------------------------
remove the 2nd madrid from my mail address to contact me.
Nov 21 '05 #8
"Daniel" <da****@madridmadridsoleado.com> schrieb
Armin Zingler wrote:
"Daniel" <da****@madridmadridsoleado.com> schrieb
Hmmm, so then, how can I get VB to throw an error if there is
nothing to update?


ExecuteNonQuery is a function.
Armin


Can you suggest an alternative?

Why? (not why suggest but why an alternative)
Armin
Nov 21 '05 #9
Daniel wrote:
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.


This is expected behaviour.. You'll have to check for the existance of
hashline.key and if it exists, update it otherwise add it. A stored
procedure could do it for you so you only have to call the sp with all
the relevant parameters.

--
Rinze van Huizen
C-Services Holland b.v.
Nov 21 '05 #10
Armin Zingler wrote:
"Daniel" <da****@madridmadridsoleado.com> schrieb
Armin Zingler wrote:
> "Daniel" <da****@madridmadridsoleado.com> schrieb
> > > Hmmm, so then, how can I get VB to throw an error if there is
> > nothing to update?
> > > > ExecuteNonQuery is a function.
> > > Armin
>

Can you suggest an alternative?


Why? (not why suggest but why an alternative)
Armin


OK, not an alternative but a solution!!!
--

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

--------------------------------------
remove the 2nd madrid from my mail address to contact me.
Nov 21 '05 #11
C-Services Holland b.v. wrote:
Daniel wrote:
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.


This is expected behaviour.. You'll have to check for the existance of
hashline.key and if it exists, update it otherwise add it. A stored
procedure could do it for you so you only have to call the sp with all
the relevant parameters.


OK, i'll try this. The trouble with checking to see if hashline.key
exists is that if the hashtable has 5000 keys it will take a long time
to query the DB for each hashline.key.

Also, I can't use stored procedures as I am connecting to MSDE.

--

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

--------------------------------------
remove the 2nd madrid from my mail address to contact me.
Nov 21 '05 #12
"Daniel" <da****@madridmadridsoleado.com> schrieb
Armin Zingler wrote:
"Daniel" <da****@madridmadridsoleado.com> schrieb
Armin Zingler wrote:
> "Daniel" <da****@madridmadridsoleado.com> schrieb
> > > Hmmm, so then, how can I get VB to throw an error if there
> > > is
> > nothing to update?
> > > > ExecuteNonQuery is a function.
> > > Armin
>
Can you suggest an alternative?


Why? (not why suggest but why an alternative)
Armin


OK, not an alternative but a solution!!!


Call it alternative or solution, the question is still, why? A solution for
what? Check the return value. If it's 0, nothing's been updated.

Armin

Nov 21 '05 #13
Armin Zingler wrote:
"Daniel" <da****@madridmadridsoleado.com> schrieb
Armin Zingler wrote:
> "Daniel" <da****@madridmadridsoleado.com> schrieb
>
> > Armin Zingler wrote:
> > > "Daniel" <da****@madridmadridsoleado.com> schrieb
> > > > > Hmmm, so then, how can I get VB to throw an error if there
> > > > > is
> > > > nothing to update?
> > > > > > ExecuteNonQuery is a function.
> > > > > Armin
> > >
> > Can you suggest an alternative?
>
>
>
> Why? (not why suggest but why an alternative)
>
>
> Armin


OK, not an alternative but a solution!!!

Call it alternative or solution, the question is still, why? A solution
for what? Check the return value. If it's 0, nothing's been updated.

Armin


ahhhh, so i will get a value returned?!!! Therefore is this what you'd
suggest?

Dim UpdateVal as Integer

UpdateVal = ExecuteNonQuery

If UpdateVal = 0 Then ......
--

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

--------------------------------------
remove the 2nd madrid from my mail address to contact me.
Nov 21 '05 #14
"Daniel" <da****@madridmadridsoleado.com> schrieb
Armin Zingler wrote:
"Daniel" <da****@madridmadridsoleado.com> schrieb
Armin Zingler wrote:
> "Daniel" <da****@madridmadridsoleado.com> schrieb
>
> > Armin Zingler wrote:
> > > "Daniel" <da****@madridmadridsoleado.com> schrieb
> > > > > Hmmm, so then, how can I get VB to throw an error if
> > > > > there is
> > > > nothing to update?
> > > > > > ExecuteNonQuery is a function.
> > > > > Armin
> > >
> > Can you suggest an alternative?
>
>
>
> Why? (not why suggest but why an alternative)
>
>
> Armin

OK, not an alternative but a solution!!!

Call it alternative or solution, the question is still, why? A
solution for what? Check the return value. If it's 0, nothing's
been updated.

Armin


ahhhh, so i will get a value returned?!!! Therefore is this what
you'd suggest?

Dim UpdateVal as Integer

UpdateVal = ExecuteNonQuery

If UpdateVal = 0 Then ......

I thought, writing "ExecuteNonQuery is a function" is sufficient to put you
on the right track. :-)

Armin

Nov 21 '05 #15
Armin Zingler wrote:
"Daniel" <da****@madridmadridsoleado.com> schrieb
Armin Zingler wrote:
> "Daniel" <da****@madridmadridsoleado.com> schrieb
>
> > Armin Zingler wrote:
> > > "Daniel" <da****@madridmadridsoleado.com> schrieb
> > >
> > > > Armin Zingler wrote:
> > > > > "Daniel" <da****@madridmadridsoleado.com> schrieb
> > > > > > > Hmmm, so then, how can I get VB to throw an error if
> > > > > > > there is
> > > > > > nothing to update?
> > > > > > > > ExecuteNonQuery is a function.
> > > > > > > Armin
> > > > >
> > > > Can you suggest an alternative?
> > >
> > >
> > >
> > > Why? (not why suggest but why an alternative)
> > >
> > >
> > > Armin
> >
> > OK, not an alternative but a solution!!!
>
>
> Call it alternative or solution, the question is still, why? A
> solution for what? Check the return value. If it's 0, nothing's
> been updated.
>
> Armin
>


ahhhh, so i will get a value returned?!!! Therefore is this what
you'd suggest?

Dim UpdateVal as Integer

UpdateVal = ExecuteNonQuery

If UpdateVal = 0 Then ......


I thought, writing "ExecuteNonQuery is a function" is sufficient to put
you on the right track. :-)

Armin


Thanks a lot for your help!

--

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

--------------------------------------
remove the 2nd madrid from my mail address to contact me.
Nov 21 '05 #16
Daniel,

Why don't you just try it with a complete update command (nothing
variable), from what you are sure that it is incorrect and sees what it
does. Now you keeping to much dependencies.


How would that differ from what I have written? I know that the code is
correct so this seems to be something by design. But I need VB to throw
an error if it cannot find the record. Any ideas?

It would probably led that you see earlier the problem. Most people are
blind for the code they have checked and checked. Than there should be taken
another approach.

I hope this helps,

Cor
Nov 21 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Aldo | last post: by
16 posts views Thread by Jason | last post: by
13 posts views Thread by Jason Huang | last post: by
4 posts views Thread by Chris Bower | last post: by
1 post views Thread by A Traveler | last post: by
14 posts views Thread by Anoop | last post: by
5 posts views Thread by Anil Gupte | last post: by
45 posts views Thread by Bob Altman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.