I get this exception thrown whenever I try and submit data through a form to this database :-
-
Exception
Cannot insert explicit value for identity column in table ‘ClientDetails’ when IDENTITY_INSERT is set to OFF.
However, the form doesn’t have a field so data can enter the identity column (PK) so I’m at a loss as to why this is occuring.
At the moment I’m using the standard asp.net mvc submit button but I will link it eventually to a jquery dialog button
The ClientNo Column which is the said column the exception is referring to has the following attributes
- Name – ClientNo
- type – int
- NULLS – NO
- Identity Spec – Yes
- Is Identity – Yes
- Increment – 1
- Seed – 1
The ClientNo has data 900 onwards etc
This exception is also thrown when the client form has no data entered in form
Its thrown on a DataCOntext.SubmitChanges() method
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create1(ClientDetail client)
{
if(ModelState.IsValid)
{
client = new ClientDetail();
UpdateModel(client);
//Debug Code
System.Diagnostics.Debug.WriteLine("AddNewClient Create1");
repo.AddNewClient(client);
//Debug Code
System.Diagnostics.Debug.WriteLine("Save Create1");
// System.Data.SqlClient.SqlException thrown at this line
repo.Save();
//Debug Code - never reached
System.Diagnostics.Debug.WriteLine("Saved Changes");
// return RedirectToAction("Details", new { id = client.ClientNo });
}
return View(client);
}
public void AddNewClient(ClientDetail client)
{
System.Diagnostics.Debug.WriteLine("Start Insert On Submit");
db.ClientDetails.InsertOnSubmit(client);
System.Diagnostics.Debug.WriteLine("Finish Insert On Submit");
}
public void Save()
{
System.Diagnostics.Debug.WriteLine("In Save Method");
db.GetChangeSet();
System.Diagnostics.Debug.WriteLine("Got ChangeSet");
db.SubmitChanges();
System.Diagnostics.Debug.WriteLine("Exit Save Method");
}
Is this the query everyone is talking about
[Column(Storage="_ClientNo", DbType="Int NOT NULL", IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)]
public int ClientNo
{
get
{
return this._ClientNo;
}
set
{
if ((this._ClientNo != value))
{
this.OnClientNoChanging(value);
this.SendPropertyChanging();
this._ClientNo = value;
this.SendPropertyChanged("ClientNo");
this.OnClientNoChanged();
}
}
}
Does anyone know a solution or reason as to why this is occuring?
Thank you
Add IsDbGenerated=true To ClientNo Property