Solved

save / reload datatable with sql server 2005 xml datatype

Posted on 2006-11-26
2
434 Views
Last Modified: 2008-02-01
I'm trying to save a datatable into a sql 2005 xml field and then reload it later into a datatable.

Can't seem to find a method that works.

Here is my code to save:
***********************************************************
        Dim StringWriter As New System.IO.StringWriter
        datatable.WriteXml(StringWriter)
        Dim conn As New SqlConnection(ShortCuts.ConnectionString)
        Dim cmd As New SqlCommand("sSaveSavedStoredGroup", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@Name", saveName)
        cmd.Parameters.AddWithValue("@data", StringWriter.ToString())


It produces the following info in sql:
***********************************************************
<DocumentElement>
  <Table1>
    <idAccount>2351</idAccount>
    <sessionName>Adventure Camp</sessionName>
    <name>ashley, joelle</name>
    <idSession>3949</idSession>
    <age>56</age>
    <gender>Female</gender>
    <idSessionType>1</idSessionType>
    <idSessionAssignment>110481</idSessionAssignment>
    <orderDate>2006-09-03T16:35:41.707-04:00</orderDate>
    <confirmed>true</confirmed>
    <Cabin>?</Cabin>
    <madeBy>Staff</madeBy>
    <idPerson>96033</idPerson>
    <sessionBalance>200.00</sessionBalance>
    <arrived>false</arrived>
  </Table1>
  <Table1>
    <idAccount>42</idAccount>
    <sessionName>Adventure Camp</sessionName>
    <name>AshleyAdded, Daniel</name>
    <idSession>3949</idSession>
    <age>11</age>
    <gender>Male</gender>
    <idSessionType>1</idSessionType>
    <idSessionAssignment>110573</idSessionAssignment>
    <orderDate>2006-11-10T14:19:16.24-05:00</orderDate>
    <confirmed>true</confirmed>
    <Cabin>?</Cabin>
    <madeBy>Staff</madeBy>
    <idPerson>90037</idPerson>
    <sessionBalance>110.00</sessionBalance>
    <arrived>false</arrived>
  </Table1>
  <Table1>
    <idAccount>4711</idAccount>
    <sessionName>Adventure Camp</sessionName>
    <name>test, test</name>
    <idSession>3949</idSession>
    <age>5</age>
    <gender>Male</gender>
    <idSessionType>1</idSessionType>
    <idSessionAssignment>110567</idSessionAssignment>
    <orderDate>2006-10-18T23:17:32.14-04:00</orderDate>
    <confirmed>false</confirmed>
    <Cabin>?</Cabin>
    <madeBy>Staff</madeBy>
    <idPerson>255104</idPerson>
    <sessionBalance>200.00</sessionBalance>
    <arrived>false</arrived>
  </Table1>
</DocumentElement>

Here is the code I'm using to load
******************************************************
        Dim conn As New SqlConnection(ShortCuts.ConnectionString)
        Dim cmd As New SqlCommand("SELECT data FROM tbSavedStoredGroups WHERE idSavedStoredGroup = @idSavedStoredGroup", conn)
        cmd.Parameters.AddWithValue("@idSavedStoredGroup", Me.ddlGroups.SelectedValue)
        conn.Open()
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader)

******************************************

When I load it back it returns a one column - and one row with all the data running together sperated by lots of diferent charaters.

What am I doing wrong?

Thanks,
Daniel
0
Comment
Question by:dbashley1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
gangwisch earned 500 total points
ID: 18015102
you need to have an xslt file on how to transform the data that you receive
0
 

Author Comment

by:dbashley1
ID: 18020646
gangwisch,

Thanks you pointed me in the right direction.

Here is the working code to save:
*******************************************
        Dim StringWriter As New System.IO.StringWriter
        datatable.WriteXml(StringWriter, XmlWriteMode.WriteSchema)
        Dim conn As New SqlConnection(ShortCuts.ConnectionString)
        Dim cmd As New SqlCommand("sSaveSavedStoredGroup", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@Name", saveName)
        cmd.Parameters.AddWithValue("@data", StringWriter.ToString())


Here is the working code to load:
*******************************************
        Dim conn As New SqlConnection(ShortCuts.ConnectionString)
        Dim cmd As New SqlCommand("SELECT data FROM tbSavedStoredGroups WHERE idSavedStoredGroup = @idSavedStoredGroup", conn)
        cmd.Parameters.AddWithValue("@idSavedStoredGroup", Me.ddlGroups.SelectedValue)
        conn.Open()
        Dim dt As New DataTable
        dt.ReadXml(cmd.ExecuteXmlReader)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question