Database PRIMARY KEY

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • umairin
    New Member
    • Nov 2007
    • 2

    Database PRIMARY KEY

    hi,
    i am new to programming and database. please help

    when ever i am submitting the i am getting the ERROR "Violation of PRIMARY KEY constraint 'PK_property'. Cannot insert duplicate key in object 'dbo.property'. The statement has been terminated."

    every time when i insert new data. and if i am removing primery key records entering two in database. whats the problem?

    you can check the code

    Protected Sub btn_submit_Clic k(ByVal sender As Object, ByVal e As System.EventArg s) Handles btn_submit.Clic k
    Try
    Dim con As New SqlConnection
    con.ConnectionS tring = "Data Source=72.18.12 9.86,1533;Initi al Catalog=rocklan d;Database=rock land;Uid=udi;pw d=pwd;"
    con.Open()
    Dim cmd As New SqlCommand
    cmd.CommandText = "INSERT INTO property (...) VALUES ('" .... "')"
    cmd.Connection = con
    cmd.CommandType = CommandType.Tex t
    cmd.ExecuteNonQ uery()
    con.Close()
    Label1.Visible = True
    Label1.Text = "Submitted"

    Catch ex As Exception
    Label1.Text = ex.Message
    End Try

    End Sub


    i am using sql server 2005
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by umairin
    hi,
    i am new to programming and database. please help

    when ever i am submitting the i am getting the ERROR "Violation of PRIMARY KEY constraint 'PK_property'. Cannot insert duplicate key in object 'dbo.property'. The statement has been terminated."

    every time when i insert new data. and if i am removing primery key records entering two in database. whats the problem?

    you can check the code

    Protected Sub btn_submit_Clic k(ByVal sender As Object, ByVal e As System.EventArg s) Handles btn_submit.Clic k
    Try
    Dim con As New SqlConnection
    con.ConnectionS tring = "Data Source=72.18.12 9.86,1533;Initi al Catalog=rocklan d;Database=rock land;Uid=udi;pw d=pwd;"
    con.Open()
    Dim cmd As New SqlCommand
    cmd.CommandText = "INSERT INTO property (...) VALUES ('" .... "')"
    cmd.Connection = con
    cmd.CommandType = CommandType.Tex t
    cmd.ExecuteNonQ uery()
    con.Close()
    Label1.Visible = True
    Label1.Text = "Submitted"

    Catch ex As Exception
    Label1.Text = ex.Message
    End Try

    End Sub


    i am using sql server 2005
    But the exception is already telling you what the problem is.
    You have to insert a record with a different Id everytime. Perhaps you should have defined your table with the id column set to auto increment if you don't want to supply the keys all the time.

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      The error message itself is self explanatory. If there is a primary key defined on any column of a table the data in that coumn must be unique and not null. Just try to follow that.

      Comment

      • umairin
        New Member
        • Nov 2007
        • 2

        #4
        Originally posted by debasisdas
        The error message itself is self explanatory. If there is a primary key defined on any column of a table the data in that coumn must be unique and not null. Just try to follow that.

        very very thanks for your replies. now i set id to auto increment. but the problem is each record getting two times in database.(ex: id = 2 name =abc and id = 3 name = abc) when i am posting and i am clicking onces on the submit button . actual this is the problem from database can you please help me in these issue

        Thanks in advance

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by umairin
          very very thanks for your replies. now i set id to auto increment. but the problem is each record getting two times in database.(ex: id = 2 name =abc and id = 3 name = abc) when i am posting and i am clicking onces on the submit button . actual this is the problem from database can you please help me in these issue

          Thanks in advance
          Are you sure the code for inserting the data is not being called twice?
          Unless you've got a before/after insert trigger on the table which inserts the record again ...

          Comment

          Working...