XML INSERT into Sql Server 2005

Posts   
 
    
tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 24-Apr-2007 06:09:21   

Using 2.0.07.0308 of the support libraries, 2.0.07.0129 of the driver.

I have an XML column on my table, and LLBLGen generates a string field on my Self-Servicing Business Entity. I assign the XML to the string, and Save and get an exception. It seems from the SQL generated that LLBLGen may not be casting the string to an XML type before doing the insert.

Any help?

The exception message is:

An exception was caught during the execution of an action query: XML parsing: line 1, character 38, unable to switch the encoding. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception."

The SQL generated is as follows:


    Query: INSERT INTO [ReportManager].[dbo].[Report] ([CommID], [PeriodFrom], [PeriodTo], [StatusName], [Filename], [FormType], [AmendmentNum], [GeneratorSettings])  VALUES (@CommId, @PeriodFrom, @PeriodTo, @StatusName, @Filename, @FormType, @AmendmentNum, @GeneratorSettings);SELECT @Id=SCOPE_IDENTITY()
    Parameter: @Id : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: <undefined value>.
    Parameter: @CommId : AnsiString. Length: 20. Precision: 0. Scale: 0. Direction: Input. Value: "1292765".
    Parameter: @PeriodFrom : DateTime. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1/1/2007 12:00:00 AM.
    Parameter: @PeriodTo : DateTime. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 4/14/2007 12:00:00 AM.
    Parameter: @StatusName : AnsiString. Length: 50. Precision: 0. Scale: 0. Direction: Input. Value: "New".
    Parameter: @Filename : AnsiString. Length: 250. Precision: 0. Scale: 0. Direction: Input. Value: "C:\AIMCAM\1292765_4-14-2007_Original.CAL".
    Parameter: @FormType : AnsiString. Length: 10. Precision: 0. Scale: 0. Direction: Input. Value: "460".
    Parameter: @AmendmentNum : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 0.
    Parameter: @GeneratorSettings : Xml. Length: 2147483647. Precision: 0. Scale: 0. Direction: Input. Value: <?xml version="1.0" encoding="utf-8"?>
<FilingGeneratorSpec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Info>
    <ReportRange>
      <From>2007-01-01T00:00:00</From>
      <To>2007-04-14T00:00:00</To>
    </ReportRange>
    <AggregateRange>
      <From>2007-01-01T00:00:00</From>
      <To>2007-04-14T00:00:00</To>
    </AggregateRange>
    <CommitteeID>1299999</CommitteeID>
    <CommitteeName>Test Comm</CommitteeName>
    <Government>State</Government>
    <Form>956/Form>
    <FilingData>
      <item>
        <key>
          <string>CashOnHand</string>
        </key>
        <value>
          <string>16369.82</string>
        </value>
      </item>
      <item>
        <key>
          <string>Report Period</string>
        </key>
        <value>
          <string>Pre-Election</string>
        </value>
      </item>
      <item>
        <key>
          <string>DateSigned</string>
        </key>
        <value>
          <string>4/23/2007</string>
        </value>
      </item>
      <item>
        <key>
          <string>FormType</string>
        </key>
        <value>
          <string>956</string>
        </value>
      </item>
      <item>
        <key>
          <string>AmendNum</string>
        </key>
        <value>
          <string>000</string>
        </value>
      </item>
      <item>
        <key>
          <string>CommID</string>
        </key>
        <value>
          <string>7403</string>
        </value>
      </item>
      <item>
        <key>
          <string>ElectionDate</string>
        </key>
        <value>
          <string>3/6/2007</string>
        </value>
      </item>
    </FilingData>
    <UserData />
  </Info>
  <TemplatePath>\\server3\Filing.Specs</TemplatePath>
</FilingGeneratorSpec>.

Walaa avatar
Walaa
Support Team
Posts: 14951
Joined: 21-Aug-2005
# Posted on: 24-Apr-2007 09:21:56   

This sounds like a bug that was solved lately, would you please try the latest runtime library version?

tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 24-Apr-2007 09:33:51   

Same result with the newest libraries and drivers.

Walaa avatar
Walaa
Support Team
Posts: 14951
Joined: 21-Aug-2005
# Posted on: 24-Apr-2007 10:25:45   

I think you might need to re-generate the code with the latest release. (ref: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=9335)

tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 24-Apr-2007 11:27:20   

I saw that post, but it seemed Oracle specific. Anyhow, I downloaded the latest Tasks/Templates, regenerated, still no luck.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 24-Apr-2007 12:42:01   

the utf-8 remark in the <? xml > root element might be the problem. Could you please try without that encoding specification?

LLBLGen Pro correctly generates an xml type parameter.

Frans Bouma | Lead developer LLBLGen Pro
tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 24-Apr-2007 20:12:06   

Yes removing the utf-8 remark does solve the problem when llblgen attempts to persist. But then how come I can submit that very xml to the database with straight SQL? Like so:

DECLARE @x XML SET @x = CAST('<TheXmlWithEncodingTag></TheXmlWithEncodingTag>' AS XML) UPDATE Report SET GeneratorSettings=@x WHERE ID=27564

tomahawk
User
Posts: 169
Joined: 02-Mar-2005
# Posted on: 24-Apr-2007 21:59:02   

Turns out SQL Server doesn't like text submitted as anything but Unicode. The CAST operator takes care of converting the string to utf-16.

So I made sure the xml is tagged utf-16, and now it works ok.

Some forum q/a for reference: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=468480&SiteID=1 http://geekswithblogs.net/vkamat/archive/2006/01/20/66561.aspx

Thanks for the help.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39618
Joined: 17-Aug-2003
# Posted on: 24-Apr-2007 22:48:18   

Thanks for the extra info simple_smile I'm sure anyone else who searches on this issue is now helped also simple_smile

Frans Bouma | Lead developer LLBLGen Pro