Stack Exchange
Error on POST FiDDLER
This is the error i get from the JSON TAB in Fiddler

ExceptionMessage=Violation of PRIMARY KEY constraint 'PK__product__9F8A5DAA2DC12F46'. Cannot insert duplicate key in object 'dbo.product'. The duplicate key value is (13                            ).
  The statement has been terminated.

The command i enter in the body is

{"ib_itemcode1":"13                ","transtatuscode":"13","invtid":"14                            ","descr":"13                                                          "}

This is my function which i think it should correct.
public HttpResponseMessage Post([FromBody] product productrecord)
    {
        try
        {
            using (estocktakeEntities entities = new estocktakeEntities())
            {
                entities.products.Add(productrecord);
                entities.SaveChanges();

                var message = Request.CreateResponse(HttpStatusCode.Created, productrecord);
                message.Headers.Location = new Uri(Request.RequestUri + productrecord.ib_itemcode1.ToString());
                return message;
            }

        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        }
    }

This is in my database SQL

this is my db

i wanted to add another item that share the same ib_itemcode1.
how do i go about this?

Do you know about PK (Primary Key) Constraint ?

@gypsyCoder yes i know that there can not have 2 same PK, i am adding 2 different pk with the same ib_itemcode1.

Before Adding the product in this line 
entities.products.Add(productrecord);

Check if the product which you are going to add is already available or not. If already available, update it instead of add.
If not available then add it. 

"Violation of PRIMARY KEY constraint"

ERROR appears when you have the same item in the primary key and you are trying to add a new record with the same key. Check it.
To Update, you can try with this below line as you are using entity framework.
 entities.Entry(productrecord).State = System.Data.Entity.EntityState.Modified;
 entities.SaveChanges();


.state? do i need to import anything?

".State" is the property used in entity framework to define the current state for a specific entity.
