Catch... try block

J

JJ297

If I enter a new topic I don't get your topic was submitted...

If I enter in a duplicate I do get teh duplicate error message. What
am I doing wrong?


Catch ex As Data.SqlClient.SqlException
'Throw New ApplicationException("An error occurred while
trying to insert the record")
If TopicTxt.Text = "" Then
Lbloutcome.Text = "Your topic was submitted into the
database."
Else
Lbloutcome1.Text = "Duplicate entry topic already in
database, topic was not submitted"
End If
 
C

Cowboy \(Gregory A. Beamer\)

First, I would design it so you do not have to "blow up" with a duplicate
insert. I would, instead, use ExecuteNonQuery on a stored procedure and
return 0 if there is already a record. In this way, you can return an
"error" message without actually throwing an exception (which is expensive).

As for why you are not getting the success message, the answer is simple:
You are succeeding. Catch only works when you have an error. Since the data
entered the database without problem, there was no error, and there is no
message.

I would redesign for success (the rule) rather than failure (the exception).
 
J

JJ297

First, I would design it so you do not have to "blow up" with a duplicate
insert. I would, instead, use ExecuteNonQuery on a stored procedure and
return 0 if there is already a record. In this way, you can return an
"error" message without actually throwing an exception (which is expensive).

As for why you are not getting the success message, the answer is simple:
You are succeeding. Catch only works when you have an error. Since the data
entered the database without problem, there was no error, and there is no
message.

I would redesign for success (the rule) rather than failure (the exception).

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|







- Show quoted text -

Okay thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top