473,385 Members | 1,192 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,385 software developers and data experts.

Updating a DB via SQL is giving me a headache

I'm having an issue with an SQL insert statement. It's a very simple
statement, and it's causing too much fuss.
strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID, OrderID, Hidden)
VALUES(" & intTypeID & ", " & intFieldID & ", " & intOrderID & ",0)"
comFields.CommandText = strSQL
comFields.ExecuteNonQuery()
Pretty simple.

Running this query in Query Analyzer will work perfect.

However, when I run this while debugging my ASP.NET project, it updates
the DB 3 times.

There are no loops, no evil goto's etc. It's just a simple 3 update that
is in the middle of a function.
Anyone have any ideas before I just put a try..Catch [evil] around it
then continue on?
Nov 19 '05 #1
5 1624
If it's updating the database 3 times, it's not the SQL Statement. It's
something in your code that is executing the statement 3 times.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Ryan Ternier" <rt******@icompasstech.com> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
I'm having an issue with an SQL insert statement. It's a very simple
statement, and it's causing too much fuss.
strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID, OrderID, Hidden)
VALUES(" & intTypeID & ", " & intFieldID & ", " & intOrderID & ",0)"
comFields.CommandText = strSQL
comFields.ExecuteNonQuery()
Pretty simple.

Running this query in Query Analyzer will work perfect.

However, when I run this while debugging my ASP.NET project, it updates
the DB 3 times.

There are no loops, no evil goto's etc. It's just a simple 3 update that
is in the middle of a function.
Anyone have any ideas before I just put a try..Catch [evil] around it then
continue on?

Nov 19 '05 #2
Kevin Spencer wrote:
If it's updating the database 3 times, it's not the SQL Statement. It's
something in your code that is executing the statement 3 times.

That's what I thought as well... however, when I F10 over the
ExecuteScalar, it throws me to the Catch statement... That's where my
confusion comes from.
Nov 19 '05 #3
Kevin Spencer wrote:
If it's updating the database 3 times, it's not the SQL Statement. It's
something in your code that is executing the statement 3 times.

Here is the entire Function that is called:
Public Function AddNewFieldToType(ByVal intTypeID As Integer, ByVal
intFieldID As Integer) As Boolean
'When we add a field we must also update ALL Items of the
current Item Type in tblItemRecords with the change
'This means we have to add a BLANK field to the DB for this
field.
Dim strSQL As String
Dim comFields As New SqlClient.SqlCommand
Dim astrTemp As String()
Dim objUtil As New Utility
Dim intOrderID As Integer

comFields.Connection = objUtil.GetConnection()
comFields.Connection.Open()
Try
'Get the OrderID
strSQL = "SELECT MAX(OrderID) FROM tblFieldLayouts
WHERE TypeID = " & intTypeID
comFields.CommandText = strSQL
intOrderID =
objUtil.ToSQLValidInteger(comFields.ExecuteScalar( ))

strSQL = "INSERT INTO tblFieldLayouts(TypeID,
FieldID, OrderID, Hidden) VALUES(" & intTypeID & ", " & intFieldID & ",
" & intOrderID & ",0)"
comFields.CommandText = strSQL
comFields.ExecuteNonQuery()

strSQL = "INSERT INTO tblItemRecords(ItemID,
FIeldID, FieldValue) VALUES(" & intTypeID & ", " & intFieldID & ",'')"
comFields.CommandText = strSQL
comFields.ExecuteNonQuery()

Catch ex As Exception
AddNewFieldToType = False
Finally
comFields.Connection.Close()
comFields = Nothing
objUtil = Nothing
End Try
AddNewFieldToType = trueEnd Function
Nov 19 '05 #4
Hi Ryan,

First, try to put all of your information in the same message, please! :)

Now, in your previous message, you said that when you "F10 over the
ExecuteScalar, it throws me to the Catch statement." This means that
SOMETHING being done in that line is throwing an exception, not necessarily
the SQL Statement, which in fact, since you tested it in Query Analyzer
(which you mentioned in the message previous to your previous message),
works. In fact, the SQL Statement that you originally said was causing the
problem will never be executed using the Try/Catch block, so I'm supposing
you recently added the Try/Catch block.

So, first, let me introduce you to the F11 (Step Into) key. This will trace
the execution thread into whatever method calls it visits (that have
debugging symbols) while executing a single line of code. Your erroneous
line of code reads:

intOrderID = objUtil.ToSQLValidInteger(comFields.ExecuteScalar( ))

But before we begin to analyze that, we need to take a look at the setup for
this. You have created a Connection using your objUtil instance. Since no
code from that class was posted, I can't guarantee that, while the
Connection was certainly created and opened (no exceptions thrown yet), I
don't know everything about the Connection. For example, a SQL Server
Connection string contains the user login information for the user account
(Windows or SQL Server) that is being connected. Therefore, it could be a
simple matter of the user account not having the necessary permissions to
perform the operation requested in the command.

Assuming that that is not the case, what else could it be? Well, you're
calling a method of the objUtil object that is probably expecting an
Integer. But what if the field is null? I can't assume that the "OrderId"
field in the table is an Identity field, a Primary Key field, or any kind of
field that requires a value to be in it. IOW, it could return a null value.
If so, null (Nothing) is not an Integer, and that could throw an exception.
Hence, my introduction to the F11 key, which would further narrow down where
the exception occurred.

Now, on to another debugging tip. It's no wonder you consider a Try/Catch
block to be "evil" (mentioned in your first message). You're not doing
anything useful with it. For example, one excellent use of a Catch block is
to log the Exception details. The Exception details would probably have
given you the information you seek. At the very least, you should have put a
break point in that Catch block, so you could do a Quick Watch and see the
Exception details for yourself.

Also, in the same "misuse of Try/Catch" department, your Catch block is
setting the return value of your function, but note that when an Exception
is handled, execution continues. This means that the last line of code in
the function,

AddNewFieldToType = true

IS executed, RE-setting the return value of the function to true, and
thereby defeating the purpose of the line of code in the Catch block that
sets the return value to false. IOW, your function will ALWAYS return true,
unless an unhandled exception occurs OUTSIDE of the Try/Catch block.

Finally, I'm not sure what made you think that your database was being
inserted into 3 times. In the code you posted, the database would not have
ANY records inserted into it, as the exception occurs while SELECTING a
record, at which point the rest of the code in that block is NOT executed,
but execution skips right down to the Catch block. I suspect that since the
function always returns true, you may be getting the wrong impression from
the return value. But that is pure speculation.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

Now, accoriding to the code you posted, objUtil is a custom class you
created, or at least used in this function.
"Ryan Ternier" <rt******@icompasstech.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Kevin Spencer wrote:
If it's updating the database 3 times, it's not the SQL Statement. It's
something in your code that is executing the statement 3 times.

Here is the entire Function that is called:
Public Function AddNewFieldToType(ByVal intTypeID As Integer, ByVal
intFieldID As Integer) As Boolean
'When we add a field we must also update ALL Items of the
current Item Type in tblItemRecords with the change
'This means we have to add a BLANK field to the DB for this
field.
Dim strSQL As String
Dim comFields As New SqlClient.SqlCommand
Dim astrTemp As String()
Dim objUtil As New Utility
Dim intOrderID As Integer

comFields.Connection = objUtil.GetConnection()
comFields.Connection.Open()
Try
'Get the OrderID
strSQL = "SELECT MAX(OrderID) FROM tblFieldLayouts WHERE
TypeID = " & intTypeID
comFields.CommandText = strSQL
intOrderID =
objUtil.ToSQLValidInteger(comFields.ExecuteScalar( ))

strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID,
OrderID, Hidden) VALUES(" & intTypeID & ", " & intFieldID & ", " &
intOrderID & ",0)"
comFields.CommandText = strSQL
comFields.ExecuteNonQuery()

strSQL = "INSERT INTO tblItemRecords(ItemID, FIeldID,
FieldValue) VALUES(" & intTypeID & ", " & intFieldID & ",'')"
comFields.CommandText = strSQL
comFields.ExecuteNonQuery()

Catch ex As Exception
AddNewFieldToType = False
Finally
comFields.Connection.Close()
comFields = Nothing
objUtil = Nothing
End Try
AddNewFieldToType = trueEnd Function

Nov 19 '05 #5
Kevin Spencer wrote:
Hi Ryan,

First, try to put all of your information in the same message, please! :)

Now, in your previous message, you said that when you "F10 over the
ExecuteScalar, it throws me to the Catch statement." This means that
SOMETHING being done in that line is throwing an exception, not necessarily
the SQL Statement, which in fact, since you tested it in Query Analyzer
(which you mentioned in the message previous to your previous message),
works. In fact, the SQL Statement that you originally said was causing the
problem will never be executed using the Try/Catch block, so I'm supposing
you recently added the Try/Catch block.

So, first, let me introduce you to the F11 (Step Into) key. This will trace
the execution thread into whatever method calls it visits (that have
debugging symbols) while executing a single line of code. Your erroneous
line of code reads:

intOrderID = objUtil.ToSQLValidInteger(comFields.ExecuteScalar( ))

But before we begin to analyze that, we need to take a look at the setup for
this. You have created a Connection using your objUtil instance. Since no
code from that class was posted, I can't guarantee that, while the
Connection was certainly created and opened (no exceptions thrown yet), I
don't know everything about the Connection. For example, a SQL Server
Connection string contains the user login information for the user account
(Windows or SQL Server) that is being connected. Therefore, it could be a
simple matter of the user account not having the necessary permissions to
perform the operation requested in the command.

Assuming that that is not the case, what else could it be? Well, you're
calling a method of the objUtil object that is probably expecting an
Integer. But what if the field is null? I can't assume that the "OrderId"
field in the table is an Identity field, a Primary Key field, or any kind of
field that requires a value to be in it. IOW, it could return a null value.
If so, null (Nothing) is not an Integer, and that could throw an exception.
Hence, my introduction to the F11 key, which would further narrow down where
the exception occurred.

Now, on to another debugging tip. It's no wonder you consider a Try/Catch
block to be "evil" (mentioned in your first message). You're not doing
anything useful with it. For example, one excellent use of a Catch block is
to log the Exception details. The Exception details would probably have
given you the information you seek. At the very least, you should have put a
break point in that Catch block, so you could do a Quick Watch and see the
Exception details for yourself.

Also, in the same "misuse of Try/Catch" department, your Catch block is
setting the return value of your function, but note that when an Exception
is handled, execution continues. This means that the last line of code in
the function,

AddNewFieldToType = true

IS executed, RE-setting the return value of the function to true, and
thereby defeating the purpose of the line of code in the Catch block that
sets the return value to false. IOW, your function will ALWAYS return true,
unless an unhandled exception occurs OUTSIDE of the Try/Catch block.

Finally, I'm not sure what made you think that your database was being
inserted into 3 times. In the code you posted, the database would not have
ANY records inserted into it, as the exception occurs while SELECTING a
record, at which point the rest of the code in that block is NOT executed,
but execution skips right down to the Catch block. I suspect that since the
function always returns true, you may be getting the wrong impression from
the return value. But that is pure speculation.

Aw common Kevin, giving me a hard time haha.

I through the Try..Catch block there to see why it was erroring. The
Code calling that function had it's own try catch, however you are right
about returning true through the function even though it failed.
I never had an error with:

intOrderID = objUtil.ToSQLValidInteger(comFields.ExecuteScalar( ))
It was the First insert (2nd query ran). After I posted we did more
testing. On others development platforms and my Laptop, the program
worked as expected. Only on my development machine was it inserting (or
trying to) 3 times.

If I changed the table around I could see the 3 inserts. It's fixed now,
I went home, and came back today and it's working fine (I didn't even
change anything :( ) but man it gave me a headache trying to figure out
why it was doing that.

I've considered using the Error Logs within windows, but with over 200
client websites using this peice of software and all of them QueryString
manipulation happy, it would give us some fun.

We've tweaked the exception classes so it sends emails without us having
to call it. If we are expecting an error, we just turn it off before
hand, then re-initiate it.
Thanks for the help though man :D
/RT
Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: J P Singh | last post by:
Hi All I am trying to query a database with a combination of surname and date of birth but it is giving me wrong results in certain conditions. It is the mm/dd/yyyy and dd/mm/yyyy stuff that...
2
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
0
by: David | last post by:
On every web browser except Safari, this website works great. (Well, by "every" I mean Mozilla, Netscape, and Internet Explorer, for Mac and Windows). The site is: http://www.ruleofthirds.com ...
1
by: Gerry Abbott | last post by:
Hi all, I've got two subforms on an unbound form, frmMain, frmSubOne, frmSubTwo. Ive got a control on frmSubTwo, cboList, a list box, which draws its source from the table underlying frmSubOne....
4
by: Darrel | last post by:
I'm creating a table that contains multiple records pulled out of the database. I'm building the table myself and passing it to the page since the table needs to be fairly customized (ie, a...
2
by: Kejpa | last post by:
Hi, I've got a number of objects that each have it's own thread. When the value changes I raise an event. Now, I want to handle the event in a form and show the value in a listview. With my first...
22
by: Mal Ball | last post by:
I hope I have the right forum for this question. I have an existing Windows application which uses a SQL Server database and stored procedures. I am now developing a web application to use the same...
10
by: chimambo | last post by:
Hi All, I have a little problem. I am retrieving records from a table and I want to update the records using checkboxes. I am able to display the database record quite alright and I have created...
5
by: rosaryshop | last post by:
I'm working a jewelry/rosary design web site at http://www.rosaryshop.com/rosariesAndKits2.php. As the user makes selections, it updates images of various parts, giving them a preview of the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.