Swetha
Swetha

Reputation: 19

Violation of PRIMARY KEY constraint Cannot insert duplicate key error?

I have six fields TinNo, CstNo, PanNo, CinNo, ServiceTaxNo, ExciseRegNo in my view .Each field have default Guid eg TinNo means(TinNo= FD713788-B5AE-49FF-8B2C-F311B9CB0CC4). I need to save those six fields in same column but not in same row and same cell of TaxInfoTaxField Table . TaxInfoTaxFiled table contain TAXINFOTAXFIELDID, TAXFIELDID,FIELDVALUE,that is i need to save those six field values (which is entered in view) in FieldValue column and their id's in TaxFieldID column .it need to save row by row . so i decided to put that default guid's in one list and fields in another list and call where we want.And in TaxInfoTaxFieldID is new new guid for each row .

TaxField Table

Saving Format

so i decided to put that default GUID's in one list and that 6 fields in another list

 ArrayList objValue = new ArrayList();
{
    objValue.Add(TITFVM.TinNo);
    objValue.Add(TITFVM.CstNo);
    objValue.Add(TITFVM.PanNo);
    objValue.Add(TITFVM.CinNo);
    objValue.Add(TITFVM.ExciseRegNo);
    objValue.Add(TITFVM.ServiceTaxNo);
   }
         List<Guid> LG = new List<Guid>();
         LG.Add(new Guid("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4"));
         LG.Add(new Guid("64B512E7-46AE-4989-A049-A446118099C4"));
         LG.Add(new Guid("376D45C8-659D-4ACE-B249-CFBF4F231915"));
         LG.Add(new Guid("59A2449A-C5C6-45B5-AA00-F535D83AD48B"));
         LG.Add(new Guid("03ADA903-D09A-4F53-8B67-7347A08EDAB1"));
         LG.Add(new Guid("2F405521-06A0-427C-B9A3-56B8931CFC57"));


      var taxinfotaxfieldID = Guid.NewGuid();
      var listFiled = new List<TaxInfoTaxFiled>();

     for (var item = 0; item < objValue.Count; item++)
        {
      TaxInfoTaxFiled taxInfoObj = new TaxInfoTaxFiled()
      {
  TaxInfoTaxFieldID = taxinfotaxfieldID,
    TaxFieldID = new Guid(LG[item].ToString()),
     FieldValue = objValue[item].ToString()
     };

     listFiled.Add(taxInfoObj);
      db.TaxInfoTaxFileds .Add(taxInfoObj);
       db.SaveChanges();
     }

  return View();
    }

All are working fine but for second loop i got one error Violation of PRIMARY KEY constraint 'PK_TaxInfoTaxFiled'. Cannot insert duplicate key in object 'dbo.TaxInfoTaxFiled'. The statement has been terminated.

In TaxInfoTaxFieldID it calculating same id for all iteration so only i got this error i think so . For that what shall I do? please help me to rectify this issue?

Advance Thanks..

Upvotes: 1

Views: 1184

Answers (1)

questionMark
questionMark

Reputation: 54

Your taxinfotaxfieldID will always be the same guid thats why you get that error also you should no insert Primary keys into the database table you could create the sql table like this

 
Create table TaxField
(
 TaxInfoTaxFiled INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
 TaxFieldID varchar(50),
FieldValue varchar(50)
)

or if you really want to use a Guid
Create table TaxField
(
 ID INT NOT NULL IDENTITY (1,1) PRIMARY KEY,
 TaxInfoTaxFiled UNIQUE DEFAULT GUID(),
 TaxFieldID varchar(50),
FieldValue varchar(50)
)

This way you can avoid inserting primary key duplicates.

Upvotes: 0

Related Questions