Validate if userID exist

I'm creating a page that the userid has to be unique. All the time I save the user and it already exist it gives me an error. And i want to just say in front of the userID field "user already exist"
can someone please help me? see below my save button code.
error:
Server Error in '/' Application.
Violation of PRIMARY KEY constraint 'PK__HospitalLogin'. Cannot insert duplicate key in object '_HospitalLogin'.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK__HospitalLogin'. Cannot insert duplicate key in object '_HospitalLogin'.
The statement has been terminated.

protected void btnSave_Click(object sender, EventArgs e)
    {
 
        SqlCommand cmd;
        cmd = new SqlCommand("sp_AddNew_HospitalLogin", myConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@OSHPDID", txtOSHPDID.Text.Trim()));
        cmd.Parameters.Add(new SqlParameter("@FullName", txtFullName.Text.Trim()));
        cmd.Parameters.Add(new SqlParameter("@UserID", txtUserID.Text.Trim()));
 
        cmd.Parameters.Add(new SqlParameter("@Pwd", txtPwd.Text.Trim()));
        cmd.Parameters.Add(new SqlParameter("@Email", txtEmail.Text.Trim()));
        cmd.Parameters.Add(new SqlParameter("@PhoneNo", txtPhoneNo.Text.Trim()));
 
        myConnection.Open();
        cmd.ExecuteNonQuery();
 
        Response.Redirect("Updates_Inserts.aspx", true);
        myConnection.Close();
 
    }

Open in new window

RomacaliAsked:
Who is Participating?
 
drunk_irishmanCommented:
Could you try something like:


try
{
  myConnection.Open();
  cmd.ExecuteNonQuery();
 
  Response.Redirect("Updates_Inserts.aspx", true);  
}
catch (Exception s)
{
   //handling for the error
}
finally
{
  myConnection.Close();
  myConnection.Dispose();
}

Open in new window

0
 
SaedSalmanCommented:
You are going to insert new row which violates a primary key constraint named 'PK__HospitalLogin', Cannot insert duplicate key in object '_HospitalLogin'.

Are you try to INSERT to save changes instead of Update ??
Show up your SQL statment...
0
 
RomacaliAuthor Commented:
I'm trying to Insert a new user:

here is my procedure:
ALTER    PROCEDURE dbo.sp_AddNew_HospitalLogin(
@UserID as varchar(25),
@OSHPDID as int,
@Pwd as varchar(25),
@FullName as varchar(50),
@PhoneNo as varchar(25),
@Email as varchar(150))

AS
      
BEGIN

INSERT INTO _HospitalLogin(
                    
                    UserID,
                    OSHPDID,
                    Pwd,
                    FullName,
                          Email,
                        PhoneNo)
VALUES
                       (
                    
                    
                          @UserID,
                    @OSHPDID,
                    @Pwd,
                    @FullName,
                      @Email,
                    @PhoneNo)

END
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
RomacaliAuthor Commented:
notes.. if we are able to create the user it redirects to a page called Updates_Inserts.aspx which is my search page. Otherwise it should ask me to enter a new UserID
0
 
SaedSalmanCommented:
UserID is the primary key ??
_HospitalLogin already contain the UserID that you are trying to insert..!
right ?
0
 
SaedSalmanCommented:
try to change the value of txtUserID "txtUserID.Text.Trim()", set to a value that is not existing in HospitalLogin.
0
 
RomacaliAuthor Commented:
I tried what you said and I got this error:

 Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'exception' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 41:             Response.Redirect("Updates_Inserts.aspx", true);
Line 42:         }
Line 43:         catch (exception s)
Line 44:         {
Line 45:
0
 
SaedSalmanCommented:
> Compiler Error Message: CS0246: The type or namespace name 'exception' could not be found (are you missing a using directive or an assembly reference?)

Exception, should starts with capital letter.
Please be informed that handling error in this way "Try catch block" will not insert the new user to you database once an exception thrown.

Saed Salman
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.