Here in my SP for avoiding duplicate entry.
ALTER PROCEDURE [dbo].[tableuser]
@userName varchar(50)
AS
IF EXISTS (SELECT 'True' FROM tbl_user WHERE userName=@userName)
BEGIN
select 'Record already exists'
select @@identity;
END
BEGIN
select 'Record Added'
select @@identity;
END
SP for adding details of user:
ALTER PROCEDURE [dbo].[ADDuser]
-- Add the parameters for the stored procedure here
(
@userName varchar(50),
@password nvarchar(50),
@emailAddress nvarchar(50)
)
AS
BEGIN
INSERT INTO tbl_user(userName,password,emailAddress) values(@userName,@password,@emailAddress)
SELECT @@IDENTITY;
END
This is the C# code:
protected void save_Click(object sender, EventArgs e)
{
ClassBAL obj = new ClassBAL();
DataSet ds = new DataSet();
obj.userName = uname.Text;
obj.password = upass.Text;
obj.emailAddress = umail.Text;
List
foreach (ListItem value in CBL1.Items)
{
if (value.Selected)
{
preferences.Add(Convert.ToInt32(value.Value));
}
}
obj.preferences = preferences;
ds=obj.tableuser();
obj.ADDuser();
Response.Redirect("UserDisplay.aspx");
}
I have used DAL and BAL in my application. Everything goes well but duplicate entry occurs.
Pls help...

Hanook159 reputationPosted Nov 8, 2013, 2:50 AM EST
-- Add the parameters for the stored procedure here
(
@userName varchar(50),
@password nvarchar(50),
@emailAddress nvarchar(50)
)
AS
BEGIN
IF NOT EXISTS (SELECT 'True' FROM tbl_user WHERE userName=@userName)
BEGIN
INSERT INTO tbl_user(userName,password,emailAddress) values(@userName,@password,@emailAddress)
SELECT @@IDENTITY AS UserStatus;
END
ELSE
SELECT -1 AS UserStatus
END
Here -1 means user already exists; any positive value means user is newly added to database
Hanook159 reputationPosted Nov 8, 2013, 2:44 AM EST
SP for adding details of user:
ALTER PROCEDURE [dbo].[ADDuser]
-- Add the parameters for the stored procedure here
(
@userName varchar(50),
@password nvarchar(50),
@emailAddress nvarchar(50)
)
AS
BEGIN
IF NOT EXISTS (SELECT 'True' FROM tbl_user WHERE userName=@userName)
BEGIN
INSERT INTO tbl_user(userName,password,emailAddress) values(@userName,@password,@emailAddress)
SELECT @@IDENTITY;
END
Saumya Agarwal19 reputationPosted Nov 8, 2013, 2:46 AM EST
Saumya Agarwal19 reputationPosted Nov 8, 2013, 3:00 AM EST
One more thing. The user here needs to enter all his details and then it directs to the GRIDVIEW page to show that the entered username is already in use. Is it possible that as soon he enters the username there is a message displaying that the username already exists? Either by using a button of CHECK AVAILABILITY or something like that?
Pls help
Hanook159 reputationPosted Nov 14, 2013, 1:14 AM EST
then write click event for checking existance of UserID ( SELECT count(*) FROM TableName WHERE USerId = YourInput )
if exists display "Already exists"; else "username is available"