I am working on University Management System on which I am using a WCF service and in the service I am using DataTables and DataSets for getting data from database and database is sql server.
My questions are
- Is using DataTables and Datasets "Good Practice" or "Bad Practice" ?
- If it is bad, what is the alternative of DataTable/DataSet ?
- If it is bad, what are the main reasons ?
Returning data sets from web services is not typically considered a “good practice”. The issues have been documented thoroughly in the following links:
http://msdn.microsoft.com/en-us/magazine/cc163751.aspx
http://www.4guysfromrolla.com/articles/051805-1.aspx
http://msdn.microsoft.com/en-us/magazine/cc188755.aspx
In summary, the biggest issues with returning DataSet
objects from web services seem to involve serialization performance, non-.net interoperability. In addition, the generic, polymorphic nature of the DataSet
generally high the data structure until runtime, as such, the WSDL definition does not provide a complete description of the method signature. As with any design decision, however, you need to weigh the costs vs the benefits and determine the best fit given your specific goals and constraints.
In terms of alternatives, you could consider using a generic collection (e.g. List<yourClassHere>
) or maybe even consider some architecture revisions to permit the use of ODATA.
The following links provide some good background reference for returning entities via web services.
http://msdn.microsoft.com/en-us/library/orm-9780596520281-01-14.aspx
http://www.codeproject.com/Articles/127395/Implementing-a-WCF-Service-with-Entity-Framework
http://msdn.microsoft.com/en-us/data/hh237663.aspx
There are 3 reason for failed return type as datatable
in WCF services
You have to specify data table name like:
MyTable=new DataTable("tableName");
When you are adding reference on client side of WCF service select reusable dll system.data
Specify attribute on datatable
member variable like
[DataMember]
public DataTable MyTable{ get; set; }
I am using DataTable
in my WCF application, but was getting an error. This is how I fixed the error.
When returning a DataTable from a web service in WCF to windows form application. I was getting this exception.
System.ServiceModel.CommunicationException: 'An error occurred while receiving the HTTP response to http://localhost:52968/MunerahService1.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.'
It happened any time I tried to retrieve a DataTable
To fix the exception I needed to give the DataTable
a name.
code with the error
tab = new DataTable();
Fixed code to remove the excption
tab = new DataTable("Doctor_info");