ARTICLE

Fetching data into ListBox from database using WCF service

Posted by Vinit Kumar Articles | WCF with VB.NET September 28, 2011
In this article, You will learn how to fetch data into listBox from database using WCF Services.
 
Reader Level:

Introduction:

The ListBox control is used to create a single - or multi-selection drop down list. The function of the ListBox is to present a list of items where the user can click and select the items from the list. In order to add items to the list, we can use the AddItem method.

In this article, I am making a WCF service of fetching data from database into ListBox. For this purpose, read the steps carefully which are given belows:

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:
WCF Service Application

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 display() 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 display() As DataSet
        Dim con As New SqlConnection("server=.;database=student1;uid=sa;pwd=wintellect")
        Dim dr As New SqlDataAdapter("select * from StudentInfo", con)
        Dim ds As New DataSet()
        dr.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:

WCF Test Client

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'.

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.

Add Service Reference

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

Windows Form

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

Code:

Partial Public Class Form1
    Inherits Form
    Public Sub New()
        InitializeComponent()
    End Sub
 
    Private Sub button1_Click(sender As Object, e As EventArgs)
        Dim obj As New ServiceReference1.Service1Client()
        listBox1.DataSource = obj.display().Tables(0)
        listBox1.DisplayMember = "name"
        listBox2.DataSource = obj.display().Tables(0)
        listBox2.DisplayMember = "rollnumber"
        listBox3.DataSource = obj.display().Tables(0)
        listBox3.DisplayMember = "age"
 
    End
Sub
End Class

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

Output

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor