Fetching value into textbox from database using WCF in VB.NET

This article is all about how you will fetch value into textbox using WCF services.
  • 4355

 Introduction: How we fetch values into textbox from database using WCF Service.
In this article, you will learn how to fetch values into two or more textboxes from a database which have any no. of records using the WCF Services into a client application.
Step 1: Open visual studio and click on file menu.
  • Go to new -> project.
  • New project dialog box will appear.
  • Select WCF -> WCF Service Application.
  • Give the name as you desire.
  • Press ok as shown below on figure:


t1.gif 

Step 2: Go to solution explorer.
  • open the Iservice1.vb.
  • write a code like:

Code:
 
' NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
<ServiceContract()> _
Public Interface IService1
 
    <OperationContract()> _
    Function show() As DataSet
 
    ' TODO: Add your service operations here

End Interface

Step 3: Now, open the Service1.svc.vb.

  • write the code like:

Code:

' NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
Public Class Service1
    Implements IService1
    Public Function show() As DataSet
        Dim da As New SqlDataAdapter("select * from employee", "database=vinit;server=.;uid=sa;pwd=wintellect")
        Dim ds As New DataSet()
        da.Fill(ds)
        Return ds
    End Function

End Class

Step 4: Start Debugging by pressing F5.

  • A dialog box will appear i.e 'WCF Test Client'.

  • Copy the highlighted address as shown below in figure:

t7.gif
 

Step 5: Open the visual studio and go to the file menu.

  • Select new -> project.

  • Select any type of application. Suppose we add 'Windows Form Application'.

t2.gif
 

Step 6: Go to Solution Explorer.

  • Right click on 'References' and select 'Add Service Reference...'

  • 'Add Service Reference' dialog box will appear.

  • Paste the address which is copied in step 4.

  • Press OK button.

Step 7: Now, take controls on Form design as shown in the figure:

t3.gif
 

  • Write the code on 'Form1.vb' as:

Code:

Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub
 
        Private i As Integer = 0
        Private Sub button1_Click(sender As Object, e As EventArgs)
 
            Dim obj As New ServiceReference1.Service1Client()
            Try
 
                textBox1.Text = obj.show().Tables(0).Rows(i)(0).ToString()
                textBox2.Text = obj.show().Tables(0).Rows(i)(1).ToString()
                i += 1
            Catch generatedExceptionName As Exception
                MessageBox.Show("record has finished")
            End Try
        End Sub

    End Class

Step 8: Now we press F5 and run this application and result shows like figure below:

t4.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.