Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hey guys!
i need some help to set IDENTITY_INSERT to ON ...
i enetered this command to query editor of sql server managment :
SQL
set IDENTITY_INSERT CustomerPI ON

well sql represent me this :
SQL
Command(s) completed successfully.

but when i run the website and try to enter some data to my database , an error will show me that cant excute
C#
db.SubmitChanges();
when IDENTITY_INSERT is set to OFF !
but as i said , i set IDENTITY_INSERT to ON !
what is problem friends? is my command line incorrect?
tnx
Posted
Comments
[no name] 27-Jul-12 21:19pm    
Why do you think you need to do that at all?
ali_heidari_ 27-Jul-12 22:32pm    
cause my asp.net page Raises this error :

Cannot insert explicit value for identity column in table 'ProductDesign' when IDENTITY_INSERT is set to OFF.

have any solution?
[no name] 27-Jul-12 23:56pm    
Yes absolutely. Don't do that. That defeats the purpose of the identity column. Or get rid of the identity column since you are not using it.
ali_heidari_ 28-Jul-12 0:30am    
as you see 'sandeep' solution can reply you! we can set it ON and after our operates can Set it OFF again!
[no name] 28-Jul-12 0:40am    
Just because there is a workaround does not mean that it's right. Always use the proper tool for the proper job.

1 solution

I think, it's not a one time thing. You need to set it ON before insertion and then reset it back to OFF for keeping consistency.

You can try the following. Executes these queries in your submitchanges() method:
SQL
SET IDENTITY_INSERT tblOrderItemStatus ON

Insert Into tblTest(Test_Id,Test_Name) values(4, "TestTemp4″)

SET IDENTITY_INSERT tblOrderItemStatus OFF
 
Share this answer
 
Comments
Kenneth Haugland 27-Jul-12 23:31pm    
I think so too. Had the same problem myself... 5
ali_heidari_ 27-Jul-12 23:50pm    
you are right ! i saw this solution many times in many sites!
but i need a solution that solve my problem programmatically (in .cs file)!
i am using a class to enter data in database!
this is a example of my way:
<pre lang="c#">
[System.Data.Linq.Mapping.Table(Name="GeneralNewsTable")]
public class GettingTable
{
private int _NewsID;
[System.Data.Linq.Mapping.Column(Name="NewsID",IsPrimaryKey=true,IsDbGenerated=true)]
public int newsid
{
get
{
return _NewsID;
}
set
{
_NewsID = value;
}
}
private String _NewsTitle;
[System.Data.Linq.Mapping.Column(Name = "NewsTitle")]
public String newstitle
{
get
{
return _NewsTitle;
}
set
{
_NewsTitle = value;
}
}

}
in my aspx.cs for example button1_click

System.Data.Linq.DataContext db = new System.Data.Linq.DataContext(@"D:\DSource\DSource\Site\FirstPattern\Dios\Dios\App_Data\PublicNewsDB.mdf");
NewsTableNameSpace.GettingTable gt = new NewsTableNameSpace.GettingTable
{
newstitle = TXTNewsTitle.Text,
};
db.GetTable<newstablenamespace.gettingtable>().InsertOnSubmit(gt);
db.SubmitChanges();



</pre>

i hope that you understand my mean!
tnx all
Sandeep Mewara 28-Jul-12 0:19am    
Posting earlier, your 'original' need with details on what & why would had helped.

I don't know, how to do with linq.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900