Catch... try block

Discussion in 'ASP .Net' started by JJ297, Oct 31, 2007.

  1. JJ297

    JJ297 Guest

    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
     
    JJ297, Oct 31, 2007
    #1
    1. Advertising

  2. JJ297

    sloan Guest

    The code inside
    Catch ex As Data.SqlClient.SqlException

    is only going to run .. is something blows up.

    .........

    I would go through this blog:
    http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx

    and rework the code.




    "JJ297" <> wrote in message
    news:...
    > 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
    >
     
    sloan, Oct 31, 2007
    #2
    1. Advertising

  3. JJ297

    Guest

    On Oct 31, 11:49 pm, "sloan" <> wrote:
    > The code inside
    > Catch ex As Data.SqlClient.SqlException
    >
    > is only going to run .. is something blows up.
    >
    > ........
    >
    > I would go through this blog:http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx
    >
    > and rework the code.
    >
    > "JJ297" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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- Hide quoted text -

    >
    > - Show quoted text -



    the catch will harm the performance.
    judge the text of Lbloutcome before insert this data to database.
     
    , Oct 31, 2007
    #3
  4. 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!
    |
    *************************************************
    "JJ297" <> wrote in message
    news:...
    > 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
    >
     
    Cowboy \(Gregory A. Beamer\), Oct 31, 2007
    #4
  5. JJ297

    JJ297 Guest

    On Oct 31, 12:58 pm, "Cowboy \(Gregory A. Beamer\)"
    <> wrote:
    > 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!
    > |
    > *************************************************"JJ297" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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- Hide quoted text -

    >
    > - Show quoted text -


    Okay thanks!
     
    JJ297, Oct 31, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bienwell
    Replies:
    4
    Views:
    3,866
    bienwell
    May 27, 2005
  2. GS
    Replies:
    4
    Views:
    5,101
    Karl Seguin [MVP]
    Jan 10, 2006
  3. jtl.zheng
    Replies:
    14
    Views:
    840
    jtl.zheng
    Aug 11, 2006
  4. Julia B
    Replies:
    2
    Views:
    280
    Julia B
    Nov 21, 2007
  5. Savvoulidis Iordanis
    Replies:
    7
    Views:
    570
    Göran Andersson
    Jul 20, 2008
Loading...

Share This Page