title: How to stop inserting records in SQL table when a limit is reached
body: <p>I am using VB.net and SQL Server 2005.</p>
  
  <p>My Problem is that I want to give user a message if the delegate limit has reached for that course.</p>
  
  <p>See I have a course where I have <strong>MinDelegate</strong> and <strong>MaxDelegate</strong> limit. I want to stop inserting and give user message that "<strong>Max Delegate limit has reached can't have more delegates for this course"</strong></p>
  
  <p>And below is the insert commmand which is inserting records in my delegate table.</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[uspInsertDelegate]
  (
      @CourseID int,
      @CPUserID int,
      @StatusID int,
      @CreateUser varchar(25),
      @CourseDate smalldatetime
  
  )
  AS
      SET NOCOUNT OFF;
  IF NOT EXISTS (SELECT *
          FROM tblDelegate
          WHERE CourseID = @CourseID and CPUserID = @CPUserID)
  BEGIN
  INSERT INTO tblDelegate                      
  (
      CourseID, 
      CPUserID, 
      StatusID, 
      CreateUser 
  
  )
  VALUES     
  (
      @CourseID,
      @CPUserID,
      @StatusID,
      @CreateUser
  )
  END
  
  UPDATE tblTraining
  SET 
  TrainingDT = @CourseDate,
  TrainingCompleted = @StatusID
  WHERE CPUserID = @CPUserID
  
  RETURN
  </code></pre>
  
  <p>Please also suggest what do in VB.NET code!</p>
  
  <p>Thanks!</p>
  
  <p>Best Regards,</p>
  
  <p>Yuv</p>