Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to count how many times a same Id enter in database table if Id enter more than five times than enter you can not more enter.
Posted
Comments
DominicZA 21-Jul-11 16:40pm    
We are going to need more than that to help you! How are you connecting to the database, what kind of database are you using MSSql, MySql?

My recommendation would be to create a stored procedure to do the saving. So, in SQL (assuming you are using MS SQL)

CREATE PROCEDURE pr_SaveObject
(
@id int
)
AS
BEGIN
IF ((SELECT COUNT(*) FROM MyTable WHERE ID = @id) < 5)
BEGIN
    INSERT INTO MyTable VALUES (@id, /*What ever other values you need*/)
    SELECT 1 /*We return 1 so that we know it succeeded*/
END 
ELSE
    SELECT 0 /*If we get 0 back we know it failed!*/
END


Now just call the proc from your site and see if it passed or failed!
 
Share this answer
 
Comments
DominicZA 21-Jul-11 16:51pm    
Sorry, thats the best I can do with the little I know about your problem!
This question has nothing to do with ASP.NET or C#, it's a database question. Even then, 'question about ASP.NET' is hardly descriptive.

You could also use a trigger that fires on an insert event, but to get back feedback on if your proc succeeded, the other solution seems best. It depends, do you want to use a trigger so if someone side steps your proc, they still can't insert a 6th value ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900