Hi
Error - Cannot insert explicit value for identity_column in table Meet when Identiy_Insert is set to Off
public void UploadSave(DataTable dt)
{
try
{
foreach (DataRow colum in dt.Rows)
{
if (colum[0].ToString().Trim() != "")
{
BALLink bALLink = new BALLink();
Link Result = new Link();
{
Result.AccountID = Convert.ToInt32(colum[0].ToString());
Result.Link1 = (colum[1].ToString());
int ID = bALLink.Add(Result);
}
}
}
}
catch (Exception ex)
{
ShowMessage("Oops...", ex.Message, "error");
}
}
Thanks
Sachin SinghPosted Mar 24, 2022, 5:27 PM
AccountID is identity column in your table means it is autoincremented so you can't insert the value for it until you make identity insert on.
so you have two options
1st:- Don't specify the identity column as it is identity database will take care of it.
try
{
foreach (DataRow colum in dt.Rows)
{
if (colum[0].ToString().Trim() != "")
{
BALLink bALLink = new BALLink();
Link Result = new Link();
{
Result.Link1 = (colum[1].ToString());
int ID = bALLink.Add(Result);
}
}
}
}
catch (Exception ex)
{
ShowMessage("Oops...", ex.Message, "error");
}
2nd option:- modify your insert statement as
SET IDENTITY_INSERT YourTableName ON
INSERT INTO YourTableName(AccountID, Link1)
VALUES(@AccountID,@Link1)
SET IDENTITY_INSERT YourTableName OFF