By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

Returning DataTables from Web Services in ASP.NET 2.0

P: n/a
I'm currently using .NET Framework 2.0 with WSE 3.0. I've read many
articles that DataTables are now serializable and can be passed via web
services. However when I have a datatable as my return type of a web
method my client app (windows form) shows the response type as
"XXXResponseXXXResult" where XXX is the name of my web method.

I've only managed to find one supposed solution
(http://forums.microsoft.com/MSDN/Sho...31319&SiteId=1)
to this problem but having to add it to the machine.config file is not
really a option when installing it on client machines. Does anyone know
of a nicer way to have client apps recognise datatable as a return
type? If not does anyone know a way to add a schema importer extension
without updating the machine.config file?

Jan 18 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I managed to get this working using asolution from
http://www.microsoft.com/belux/msdn/...s/wsproxy.mspx

heres a quote from the page that is useful
"First of all, if you don't want to modify the machine.config file, you
can put the configuration section mentioned above in the application's
configuration. For Visual Studio 2005, this file is typically located
at C:\Program Files\Microsoft Visual Studio
8\Common7\IDE\devenv.exe.config. ... Second, if you would like to
refrain from adding assemblies to the GAC, you can put the assembly in
the same directory as the application. Visual Studio even has a
location specifically created for such design-time helper assemblies:
its PrivateAssemblies directory, which is located right below the
directory in which the devenv.exe.config file can be found."

So for those that are looking for the complete solution here it is.

1. Create a Class Library Project called "DataTableSchemaImporter"
(name can be anything you like)

2. Add a Class called "DataTableSchemaImporterExtension" (again name it
what you like)

3. Add the following code the class (converted code from link in my
first post)
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization
Imports System.CodeDom
Imports System.CodeDom.Compiler

Class DataTableSchemaImporterExtension
Inherits Advanced.SchemaImporterExtension

'-- DataTableSchemaImporterExtension is used for WebServices,
'-- it is used to recognize the schema for DataTable within wsdl
Dim importedTypes As Hashtable = New Hashtable()

Public Overrides Function ImportSchemaType(ByVal name As String,
ByVal schemaNamespace As String, _
ByVal context As XmlSchemaObject, ByVal schemas As XmlSchemas,
ByVal importer As XmlSchemaImporter, _
ByVal compileUnit As CodeCompileUnit, ByVal mainNamespace As
CodeNamespace, ByVal options As CodeGenerationOptions, _
ByVal codeProvider As CodeDomProvider) As String
Dim values As IList = schemas.GetSchemas(schemaNamespace)

If Not values.Count = 1 Then
Return Nothing
End If

Dim schema As XmlSchema = CType(values(0), XmlSchema)

If IsNothing(schema) Then
Return Nothing
End If

Dim type As XmlSchemaType = CType(schema.SchemaTypes(New
XmlQualifiedName(name, schemaNamespace)), XmlSchemaType)

Return ImportSchemaType(type, context, schemas, importer,
compileUnit, mainNamespace, options, codeProvider)
End Function
Public Overrides Function ImportSchemaType(ByVal type As
XmlSchemaType, ByVal context As XmlSchemaObject, _
ByVal schemas As XmlSchemas, ByVal importer As XmlSchemaImporter,
ByVal compileUnit As CodeCompileUnit, _
ByVal mainNamespace As CodeNamespace, ByVal options As
CodeGenerationOptions, _
ByVal codeProvider As CodeDomProvider) As String
If IsNothing(type) Then
Return (Nothing)
End If

If importedTypes(type) IsNot Nothing Then
mainNamespace.Imports.Add(New
CodeNamespaceImport(GetType(DataSet).Namespace))
compileUnit.ReferencedAssemblies.Add("System.Data. dll")
Return CType(importedTypes(type), String)
End If

If Not TypeOf context Is XmlSchemaElement Then
Return Nothing
End If

If TypeOf type Is XmlSchemaComplexType Then
Dim ct As XmlSchemaComplexType = CType(type,
XmlSchemaComplexType)

If TypeOf ct.Particle Is XmlSchemaSequence Then
Dim items As XmlSchemaObjectCollection =
(CType(ct.Particle, XmlSchemaSequence)).Items

If items.Count = 2 AndAlso TypeOf items(0) Is
XmlSchemaAny AndAlso TypeOf items(1) Is XmlSchemaAny Then
Dim any0 As XmlSchemaAny = CType(items(0),
XmlSchemaAny)
Dim any1 As XmlSchemaAny = CType(items(1),
XmlSchemaAny)

If any0.Namespace = XmlSchema.Namespace _
AndAlso any1.Namespace =
"urn:schemas-microsoft-com:xml-diffgram-v1" Then
Dim typeName As String =
GetType(DataTable).FullName
importedTypes.Add(type, typeName)
mainNamespace.Imports.Add(New
CodeNamespaceImport(GetType(DataTable).Namespace))

compileUnit.ReferencedAssemblies.Add("System.Data. dll")
Return typeName
End If
End If
End If
End If

Return Nothing
End Function

End Class

4. Build Project

5. Copy project dll to "<VS 2005 installation
folder>\Common7\IDE\PrivateAssemblies"
where <VS 2005 installation folderis usally C:\Program
Files\Microsoft Visual Studio 8

6. Open for editing devenv.exe.config in <VS 2005 installation
folder>\Common7\IDE

7. Above </configurationadd

<system.xml.serialization>
<schemaImporterExtensions>
<add name="DataTableSchemaImporterExtension"

type="DataTableSchemaImporter.DataTableSchemaImpor terExtension,
DataTableSchemaImporter" />
</schemaImporterExtensions>
</system.xml.serialization>

8. Save file, open project and update web references

Jan 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.