b

Wednesday, 19 December 2012

Returning Datatable from WCF VB.NET


Returning Datatable from WCFVB.NET

 Step1)   Create a WCF Service Application using .NET 4.5 VB.NET
 Step 2)  Add WCF service name it as DatableDemo
    <ServiceContract> _
    Public Interface IDatableDemo
        <OperationContract> _
        Dim GetEmpDetails() As DataTable
    End Interface

Step 3) In the implementation file.
      Public DataTable GetEmpDetails()
        {
          Dim emptable As DataTable =  New DataTable("Employees")
Try
End Try
          {
              Imports (SqlConnection conn = New SqlConnection("server=(local)\sqlexpress2012database=AdventureWorks2012uid=sa;pwd=pwd"))
              {
                  Imports (SqlDataAdapter adapter = New SqlDataAdapter("select * from (AdventureWorks2012).(HumanResources).(Employee)", conn))
                  {
                      adapter.Fill(emptable)
                      emptable.RemotingFormat = SerializationFormat.Xml
                      emptable.AcceptChanges()
                      conn.Close()
                  }
              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
               return emptable;
        }


Step 4)  Create a schema for DataTable using SvcUtil.exe. otherwise "The underlying connection was closed: An unexpected error occurred on a receive" error msg
            will display

Step 5) Open VS2012 Command Prompt in Administrator mode.  


c:\wcf>svcutil http://localhost:3054/idtdemo.svc  /r:"C:\Program Files (x86)\Ref
erence Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll" /r:C:\
inetpub\wwwroot\Tutorials\WCFServices\bin\wcfservices.dll /t:metadata

c:\wcf>svcutil tempuri.org.wsdl schemas.microsoft.com.2003.10.Serialization.xsd
tempuri.org.xsd WCFServices.xsd tempuri.org.samples.xsd System.Data.xsd /language:VB

Step 6) It will generate Proxy class(IDatableDemo.cs) for datatable.  Add this class to any project
              1) Consume WCF DataTable in Console Application
              2) Consume WCF DataTable in Windows Form Application
              3) Consume WCF DataTable in Windows Presentation Application(WPF).
              4) Consume WCF DataTable in Silverlight Application
              5) Consume WCF DataTable in ASP.NET Application

Step 7)   in WPF
               Dim client2 As IdatabledemoClient =  New IdatabledemoClient()
                 datagrid1.ItemsSource = client2.GetEmpDetails().DefaultView

No comments:

Post a Comment