Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 632
  • Last Modified:

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

0
Romacali
Asked:
Romacali
  • 4
  • 3
1 Solution
 
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
 
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
SaedSalmanCommented:
UserID is the primary key ??
_HospitalLogin already contain the UserID that you are trying to insert..!
right ?
0
 
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:
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now