I'm trying to create login form in C#.
I store username and password in SqlServer 2008 express using the stored procedure

ALTER procedure [dbo].[InsertNewUser]
(
  @UserName varchar(30), 
  @Password varchar(30)
)
as 
If exists(Select 'True' from Users where UserName = @UserName)
begin
--This means that the user name is taken
select 'User already exists'
end
else
begin
--This means that the username is available
select 'New user created'
Insert into Korisnici (UserName,UserPassword) values (@UserName, @Password)
end

How to check in C# when the new user is available, and when the username is taken?
When the selected username is available do the insert part and display the message "new user created" and when the username is taken display the message "username taken" and do not do the insert.

Edited 6 Years Ago by Michael27: n/a

well,
the procedure that u have written works for insert, it ll insert the new answer absolutely fine. It is in Backend that you are checking if the user is already present or not.
but, if u want to check if the user is already present, write another SP,

Create procedure [dbo].[CheckUser]
(
    @UserName varchar(30)
)
as
begin
set nocount on
select * from users where UserName = @UserName
end

now that you have this SP, call that in to your Data access layer,
and then in the code page,
take a datatable, populate your result on to that.
check the below condition

if (datatable.Rows.Count > 0)
{
  MessageBox.Show("user exists, try another.");
}
else
{
 MessageBox.Show("Inserting new user.");
// call your insert SP here.
}

Mark it as solved once this works.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This question has already been answered. Start a new discussion instead.