Check for existing record in SQL Server database using stored procedure
In this example I will explain, how to check for record exist before insert data into a table in sql server 2005 or 2008.
In one of my web project I had to implement some sort of validation to check for existing record in the database before inserting. This is quite important when you try to avoid duplicate entry or multiple registrations and so on. Specially when you have self registering facility in the website, it is a good practice to maintain data quality by checking for any duplicate record in the database before insert action. If record exists with the same information provided by the user it will flag user with error message and prevent from inserting another record. To implement this functionality in asp.net website I have used stored procedure.
Store procedures are considered to be better approach for database interaction than normal sql statement for this type of operation. I already have a resident table with those fields showing below, now creating a procedure to work with that table.
-- Add the parameters for the stored procedure here
04
@first_name varchar(60),
05
@last_name varchar(50),
06
@Address1 varchar(250),
07
@City varchar(50),
08
@country varchar(50),
09
@Telephone varchar(50),
10
@Email varchar(50),
11
@Website varchar(50),
12
@address2 varchar(150),
13
@ip_address varchar(50),
14
@Mobile varchar(50)
15
16
ASBEGIN
17
18
-- SET NOCOUNT ON added to prevent extra result sets from
19
-- interfering with SELECT statements.
20
21
SETNOCOUNT ON;
22
IF (EXISTS(SELECTlast_name FROMdbo.temp_resident
23
WHERElower(First_name)=LOWER(@first_name)
24
andlower(Last_name)=lower(@Last_name)
25
andlower(Address1)=lower(@Address1)
26
andLOWER(city)=LOWER(@city)))
27
28
BEGIN
29
--raise error
30
31
(N'Record exist with the same name.',16,-1);
32
33
RETURN(0);
34
35
END
36
37
ELSE
38
39
BEGIN
40
41
INSERTINTOdbo.temp_resident
42
(first_name,
43
Last_name,
44
Address1,
45
City,
46
Country,
47
Telephone,
48
Email,
49
Website,
50
create_date,
51
address2,
52
ip_address,
53
Mobile,
54
approved)
55
56
VALUES
57
(@first_name,
58
@last_name,
59
@Address1,
60
@City,
61
@country,
62
@Telephone,
63
@Email,
64
@Website,
65
GETDATE(),
66
@address2,
67
@ip_address,
68
@Mobile,
69
'NO')
70
71
RETURN(1);
72
END
73
74
END
In the aspx page I have created all the associated fields with a submit button id btnRegister. I have not included the aspx page code here. Now in the aspx code page, I have used the following VB code for calling the store procedure and execute.
lblMsg.Text = "The business name you are trying to submit is already exist in our database, please make sure your business name is not exist in our system before submitting. Please contact us if you have any query regarding this."
lblMsg.Text = "The business name you are trying to submit is already exist in our database, please make sure your business name is not exist in our system before submitting. Please contact us if you have any query regarding this.";
6 thought on “Check for existing record using stored procedure in ASP.NET”
I’m extremely inspired together with your writing abilities and also with the format to your weblog. Is this a paid topic or did you customize it yourself? Anyway keep up the excellent high quality writing, it’s rare
to look a nice weblog like this one today..
My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using WordPress on numerous
websites for about a year and am worried about switching to another platform.
I have heard great things about blogengine.
net. Is there a way I can import all my wordpress content into it?
Any help would be really appreciated!
Howdy would you mind letting me know which web host you’re utilizing? I’ve loaded your
blog in 3 different browsers and I must say this blog loads a lot faster then most.
Can you suggest a good web hosting provider at a fair price?
I’m extremely inspired together with your writing abilities and also with the format to your weblog. Is this a paid topic or did you customize it yourself? Anyway keep up the excellent high quality writing, it’s rare
to look a nice weblog like this one today..
My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using WordPress on numerous
websites for about a year and am worried about switching to another platform.
I have heard great things about blogengine.
net. Is there a way I can import all my wordpress content into it?
Any help would be really appreciated!
Howdy would you mind letting me know which web host you’re utilizing? I’ve loaded your
blog in 3 different browsers and I must say this blog loads a lot faster then most.
Can you suggest a good web hosting provider at a fair price?
Thanks, I appreciate it!
hi, I used your sp model but it’s generating an error after the set nocount:
Msg 102, Level 15, State 1, Procedure spICEPAGregistrationData2, Line 29
Incorrect syntax near ‘Record exist with the same name.’.
using sql server 2008.
Please send your procedure code otherwise it is hard to say what are you doing wrong in the code.
I am new to asp. Can you please share your aspx page coding. It will help a lot. thanks