Sneha K

Sneha K

  • 1.2k
  • 527
  • 195.9k

Violation of PRIMARY KEY constraint Cannot insert duplicate

Jan 10 2016 2:15 AM
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 one list and call where we want.
My View
The Default guid is already saved in TaxField table
Need to save in format
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.. 
 

Answers (3)