473,378 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Returning DataTables from Web Services in ASP.NET 2.0

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
1 8306
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Karuppasamy | last post by:
H I have a WebService calling a Method in a Remote Object. The Remote method Returns an Object. This Webservice also returns the same object. All things are working fine when this Web Service is...
2
by: Z D | last post by:
Hello, I'm currently using Remoting (HTTP/Binary) to remote a simple object. Everything is working fine except for one function that returns an arraylist of datatables. When I call this...
1
by: Knecke | last post by:
Hi all. I have a problem with returning a custom Result object with webservice. The classes i use is described below (some fields and properties is removed) public class Result { int...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
0
by: TK | last post by:
Is it possible to return an datatable the is not strongly typed from a web service using 2.0? When I try it the return type in my proxy class is MyWebMethodResponseMyWebMethodNameResult. I have...
7
by: sameer | last post by:
Hi all, Application environment : VB.Net desktop application,.NET 1.1 Framework, VS 2003. communicates between the database and the application is done over webservices using ADO.NEt Datasets....
3
by: Khurram | last post by:
Hi, Firstly, I will apologise now if I have posted in the wrong discussion group. Please let me know if I have for future reference. Below is the code to a WebMethod that is querying an Access...
15
by: Joseph Geretz | last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or Datatables between application tiers. http://support.microsoft.com/kb/306134 ...
10
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm using this coding to get 2 resultsets thru datareader and then load them into 2 datatables and bind the datatables to datagridviews. But sdrGrid.NextResult() is returning false for some...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.