Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:32:29+00:00 2026-06-17T18:32:29+00:00

I am trying to insert some data to the sql server but I am

  • 0

I am trying to insert some data to the sql server but I am getting the error message like “Violation of PRIMARY KEY constraint ‘PK_login1’. Cannot insert duplicate key in object ‘dbo.login1’. The statement has been terminated.”. I have tried to remove the primary key constraint from the table, after that I see that it is inserting more then two similar kind of data into the table. My code is

protected void btn_Submit_Click(object sender, EventArgs e)
{
    try
    {
        SqlCommand cmd;
        str = "Insert into login1 values ('" + txtbx_Uname.Text + "', '" + txtbx_Pwd.Text + "', '" + txtbx_Email.Text + "', '" + txtbx_Dob.Text + "', " + txtbx_Phone.Text + ")";
        con.Open();
        cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        lbl_Error.Visible = true;
        lbl_Error.Text = "Registration Success";
        int n = Convert.ToInt32(cmd.ExecuteScalar());
        if(n==1)
            Response.Redirect("Login.aspx");
        con.Close();
    }
    catch
    {
        lbl_Error.Visible = true;
        lbl_Error.Text = "SQL Server Error. Pleaase try after sometime";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:32:30+00:00Added an answer on June 17, 2026 at 6:32 pm

    You are using
    cmd.ExecuteNonQuery(); and
    int n = Convert.ToInt32(cmd.ExecuteScalar()); which will do the same job here.
    If you want the status of new record that has been inserted to the table, then change the query to stored procedure or use another query for find out the count of record with the field mentioned.

    try the following code.

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmd;
                str = "Insert into login1 values ('" + txtbx_Uname.Text + "', '" + txtbx_Pwd.Text + "', '" + txtbx_Email.Text + "', '" + txtbx_Dob.Text + "', " + txtbx_Phone.Text + ")";
         " _    
        & "SELECT @@IDENTITY AS int32;"
                con.Open();
                cmd = new SqlCommand(str, con);    
          int n = Convert.ToInt32(cmd.ExecuteScalar());  
    
                if(n==1)
                {
         lbl_Error.Visible = true;
                lbl_Error.Text = "Registration Success";  
                    Response.Redirect("Login.aspx");
                con.Close();
               }
         }
            catch
            {
                lbl_Error.Visible = true;
                lbl_Error.Text = "SQL Server Error. Pleaase try after sometime";
            }
    
    }
    

    Hope this will fix the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert some text data into a table in SQL Server
i am trying to insert some info into an sql server table but instead
I'm trying to insert some data into mysql database, but I get an error
Hello I'm trying to insert some XML data into a table on SQL Server
I'm trying to insert some data to a database on a linked server but
I have a form that is trying to insert some data into an SQL
I am trying to insert some data into a pysqlite database but even tho
I have some troubles trying to move data from SQL Server 2000 (SP4) to
I am trying to import some data to SQL Server 2008 by means of
i'm trying to insert some data in my table: golden-book. All is good ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.