473,411 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 1144

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Aldo | last post by:
hello i'm trying to translate this asp code to php, but i can't get it work right. could anyone help me? thanks! a. ASP CODE ---------------------
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
4
by: Chris Bower | last post by:
Reposted from aspnet.buildingcontrols: Ok, I've got a bunch of derived controls that all have a property Rights of type Rights (Rights is an Enumerator). I wrote a custom TypeConverter so that I...
1
by: A Traveler | last post by:
Hello, i am having this problem. The exact error message is: "Unable to generate code for a value of type 'System.Web.UI.Page'. This error occurred while trying to generate the property value for...
7
by: Nalaka | last post by:
Hi, I created a sinple web service that returns a dataSet. Then I created a client program that uses this web service (that returns the Dataset). My question is, how did the client figure...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
5
by: Anil Gupte | last post by:
I get two errors void DisplayProjectedValue(double amount, int years) { Random __gc * r = new Random(); int randomRate = r->Next(0, 20); DisplayProjectedValue(amount, years, randomRate); } ...
45
by: Bob Altman | last post by:
This code (Visual Studio 2005) should generate a compilation warning to the effect that I'm accessing a shared member through an instance variable (the "color" variable): Private Sub X(ByVal...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.