A solution is to write a "before INSERT" trigger which will enforce the business rules by checking how many students are readily registered for the class etc.) and prevent the insert to take place.
This error condition can then be detected at higher levels, and a proper error message provided to the user.
In re-reading your question, the business rule could as well be checked in the stored procedure you have in place, it would then just be a matter of making the store procedure return a condition code (say an integer: 0 = insert ok and -1 = class is full), and to test this value at the level of the application.
Edit: more details (but not quite the full code...)
Yuvraj, this looks seriously like homework, so I'd like to put you on the right track but also let you work at it enough that you learn the process of figuring things out.
With regards to the Store Procedure (SP), bniwredyc readily provided you the code:
It is a slight modification compared with what you have in your question:
@minDelegate int,
@maxDelegate int
set @delegatesCount = (select count(*) from tblDelegate
where CourseID = @CourseId)
if (@delegatesCount >= maxDelegate)
return -1
Essentially you add 2 extra arguments to the procedure: minDelegate and maxDelegate and return prematurely from the procedure, with a -1 return value, in case there are too many delegates. (1 in bniwredyc 's example, but I prefer negative values for error conditions). I don't think minDelegate is used at all; you know better which rules have to applied...
Now, you need to write a VB program which will invoke this SP by way of ADO. This will involve using the ADODB.Command object This Microsoft MSDN page provides reference info about this object and following a few links not too far from this page, you'll also find details about the Connection object and the RecordSet Object.
There are 3 ways that a stored procedure can return some data to the calling method.
1. By returning an integer value in the RETURN value. This value is
the return value of the Command object's Execute() method.
This is the simpler approach and can be used in your case
2. By returning values (integer, text or other) in one or several OUTPUT
Parameters
3. By returning a recordset
Method 2 can be combined with 1 or 3, however 1 and 3 are mutually
exclusive since they both use return value of the Execute() method
to provide an integer (1) or a Recordset (3).
The example at this page shows +/- all that you will need, but it uses a recordset for the data, which is not necessary in your case; Instead use an integer value for storing the return value of Execute(), and test it. If 0 : Record was added ok, If-1 : Failed the "too many" test.
Now, get to work :-) and please do tag your questions as "Homework" when appropriate.