﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » Business Objects and Data Access (How do I?)  » Identify fields - converting data</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Sun, 27 Sep 2026 04:22:18 GMT</lastBuildDate><ttl>20</ttl><item><title>Identify fields - converting data</title><link>http://forum.strataframe.net/FindPost23581.aspx</link><description>I need to migrate some data from an old table to a new table. The old table and new table both have an auto incremented identity field as the primary key and I need to ensure the values remain the same in the new table.&lt;/P&gt;&lt;P&gt;I have a business object for the old and new and have written the conversion program but when I save the new business object I get a error message 'Cannot insert explicit value for identity column in table 'ContactNew' when IDENTITY_INSERT is set to OFF.'&lt;/P&gt;&lt;P&gt;I tried setting the IDENTITY_INDENTITY ON before the save and IDENTITY_INSERT OFF after the same but I still get the error.&lt;FONT color=#a31515 size=2&gt;&lt;FONT color=#a31515 size=2&gt;&lt;/P&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;FONT color=#111111&gt;loCommand.CommandText&lt;/FONT&gt; = &lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;&lt;FONT color=#a31515 size=2&gt;"SET IDENTITY_INSERT "&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &amp;amp; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;Me&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;.TableNameAndSchema &amp;amp; &lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;&lt;FONT color=#a31515 size=2&gt;" ON"&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#a31515 size=2&gt;&lt;FONT color=#a31515 size=2&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;Me&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt;.ExecuteNonQuery(loCommand)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt;Any ideas how to solve this issue?&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#000000&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#000000&gt;Thanks,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#000000&gt;Tim&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;</description><pubDate>Fri, 19 Jun 2009 08:33:03 GMT</pubDate><dc:creator>Tim Dol</dc:creator></item><item><title>RE: Identify fields - converting data</title><link>http://forum.strataframe.net/FindPost23606.aspx</link><description>Greg is correct.  The reason is this.  When you call the ExecuteNonQuery, the connection session is only open for that one call.  So when you set the IDENTITY_INSERT ON, execute, then execute the command.  The IDENTITY_INSERT is back to off.&lt;br&gt;
&lt;br&gt;
When I do this type of thing I generally like to create a stored procedure as it makes it a little easier.</description><pubDate>Fri, 19 Jun 2009 08:33:03 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Identify fields - converting data</title><link>http://forum.strataframe.net/FindPost23584.aspx</link><description>you probably need to add the SET IDENTITY_INSERT ON and OFF to your main batch:&lt;br&gt;
&lt;br&gt;
[codesnippet]Dim sqlBuilder As New System.Text.StringBuilder(512)&lt;br&gt;
With sqlBuilder&lt;br&gt;
.AppendFormat("SET IDENTITY_INSERT {0} ON", Me.TableNameAndSchema).Append(ControlChars.NewLine)&lt;br&gt;
&lt;br&gt;
'-- baseSQL is the actual SQL that would be executed to copy values&lt;br&gt;
.Append(baseSQL)&lt;br&gt;
&lt;br&gt;
.AppendFormat("SET IDENTITY_INSERT {0} OFF", Me.TableNameAndSchema).Append(ControlChars.NewLine)&lt;br&gt;
End With&lt;br&gt;
&lt;br&gt;
loCommand.CommandText = sqlBuilder.ToString()&lt;br&gt;
Me.ExecuteNonQuery(loCommand)[/codesnippet]&lt;br&gt;
&lt;br&gt;
I haven't tried this, but it should work.</description><pubDate>Thu, 18 Jun 2009 10:58:29 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item></channel></rss>