Click here to Skip to main content
15,947,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have an issue that I have tried the MSSQL data table convert into XML format.
But I could Not do "
Error: Cannot serialize the DataTable. DataTable name is not set.


Pls advice me

Thank you

Maideen

What I have tried:

ASP.NET
Public Function ConvertDatatableToXML(ByVal dt As DataTable) As String
    Dim str As MemoryStream = New MemoryStream()
    dt.WriteXml(str, True)
    str.Seek(0, SeekOrigin.Begin)
    Dim sr As StreamReader = New StreamReader(str)
    Dim xmlstr As String
    xmlstr = sr.ReadToEnd()
    Return (xmlstr)
End Function

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim dt As DataTable = New DataTable()
    dt = GetData()
    Dim XMLstring As String = clsAPI.ConvertDatatableToXML(dt)
    Me.txtComments.Text = XMLstring

End Sub

ASP.NET
Public Function GetData() As DataTable
    Dim dt As New DataTable
    Dim adp As New SqlDataAdapter("usp_TR_Data", conn)
    adp.SelectCommand.CommandType = CommandType.StoredProcedure
    adp.SelectCommand.CommandTimeout = 150

    With adp.SelectCommand.Parameters
        .Clear()
        .Add("@Action", SqlDbType.NVarChar).Value = "CONVERT-XML"
        .Add("@SONo", SqlDbType.NVarChar).Value = Me.txtSONo.Text
    End With
    adp.Fill(dt)
    Return dt
End Function

SQL
SQL Query
		Select 
			[sname],[saddress1],[saddress2],[scity],[spostcode],[sstate],
			[dairport],[scountry],	[detailOfContent],[weight],[productPackages],
			[dimensionX],[dimensionY],[dimensionZ],[chargeableRate],[rname],[rphone],
			[raddress1],[raddress2],[rpostcode],[rcity],[rstate],[rairport],[rcountry],
			[rremark],[rvia],[serviceModeId],[agentId],[trackingno]
		From TR_Data
		where SONo=@sono
Posted
Updated 21-Dec-22 20:01pm
v2

1 solution

Read the error error message - it's pretty explicit!
Cannot serialize the DataTable. DataTable name is not set.

So ... set the DataTable.TableName property [^] before you try to serialize it:
VB
dt.TableName = "MyTable"
 
Share this answer
 

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