﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » Business Objects and Data Access (How do I?)  » Catching Specific Exception question...</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Wed, 16 Sep 2026 09:58:05 GMT</lastBuildDate><ttl>20</ttl><item><title>Catching Specific Exception question...</title><link>http://forum.strataframe.net/FindPost7410.aspx</link><description>How would I catch a specific exception? For instance I have a table with a compound primary key... the primary key is not an auto increment but I dont want to allow duplicates to be inserted. When I try to insert a duplicate it throws an exception, naturally. I am able to trap the SQLException but I'm not sure how to catch that specific 'Violation of PRIMARY KEY constraint'... &lt;br&gt;
&lt;br&gt;
Should I query the table before I attempt the insert instead of trying to catch the exception?</description><pubDate>Mon, 12 Mar 2007 12:07:56 GMT</pubDate><dc:creator>StarkMike</dc:creator></item><item><title>RE: Catching Specific Exception question...</title><link>http://forum.strataframe.net/FindPost7413.aspx</link><description>Well, you cannot override the exception since the error is actually happening on the SQL Server itself and you will never be able to work around the constraint.&amp;nbsp; We generally test for the uniquness of a value using a scalar command within the CheckRulesOnCurrentRow event.&lt;/P&gt;&lt;P&gt;[codesnippet]Public Function CheckForDuplicate(ByVal MyPassedTestValue As String) As Integer&lt;BR&gt;Dim loCommand As New SQLCOmmand()&lt;BR&gt;Dim lnPK As Integer&lt;/P&gt;&lt;P&gt;'-- Skip the current record&lt;BR&gt;IF Me.Count &amp;gt; 0 Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lnPK = Me.PrimaryKeyField&lt;BR&gt;Else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lnPK = -1&lt;BR&gt;End If&lt;/P&gt;&lt;P&gt;'-- Build the command text&lt;BR&gt;loCommand.CommandText = "SELECT COUNT(*) FROM MyTable WHERE MyPrimaryKeyField != @MyPrimaryKeyField And MyTestField = @MyTestField"&lt;/P&gt;&lt;P&gt;'-- Create the parms&lt;BR&gt;loCommand.Parameters.Add("@MyPrimaryKeyField",SqlDataType.Int)&lt;BR&gt;loCommand.Parameters.Add("@MyTestField",SqlDataType.VarChar)&lt;/P&gt;&lt;P&gt;'-- Set the parms&lt;BR&gt;loCommand.Parameters("@MyPrimaryKeyField").Value = lnPK&lt;BR&gt;loCommand.Parameters("@MyTestField").Value = MyPassedTestValue&lt;/P&gt;&lt;P&gt;'-- Return the results&lt;BR&gt;Return CType(Me.ExecuteScalar(loCommand), Integer)&lt;BR&gt;End Sub[/codesnippet]&lt;BR&gt;</description><pubDate>Mon, 12 Mar 2007 12:07:56 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item></channel></rss>