Link to home
Start Free TrialLog in
Avatar of Romacali
Romacali

asked on

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

Avatar of SaedSalman
SaedSalman
Flag of Jordan image

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...
Avatar of Romacali
Romacali

ASKER

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
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
UserID is the primary key ??
_HospitalLogin already contain the UserID that you are trying to insert..!
right ?
ASKER CERTIFIED SOLUTION
Avatar of drunk_irishman
drunk_irishman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try to change the value of txtUserID "txtUserID.Text.Trim()", set to a value that is not existing in HospitalLogin.
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:
> 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