Senin, 07 Mei 2012

Web Service Client C#.NET Mengakses Web Service PHP NUSOAP

Web Service Client - C#.NET Mengakses (Consume) Web Service PHP NUSOAP

Web Service Client ini akan mengkonsumsi Web Service PHP NUSOAP seperti yang diterangkan pada halaman Web Service PHP NUSOAP

Buat project baru dengan cara klik File - New - Project : Visual C# - Windows - Visual C# Form Application. Jangan lupa untuk memilih.NET Framework 2.0, bukan 3.0 atau 3.5, walaupun memakai Visual C# .NET 2008 tetap pilih .NET Frameworks 2.0 karena Framework ini menyediakan Web Reference yang mampu mengakses Web Service PHP NUSOAP.

Buat form seperti di bawah ini :


Kemudian tambahkan Web Reference dengan cara klik menu, Project - Add Web Reference, tambahkan Web Reference yang mengarah pada Web Service yang telah disiapkan :



Kemudian tuliskan kode program berikut :

Public Class formWSClientPHP



Private Sub formWSClientPHP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim ds As New DataSet()

ds.Tables.Add(New DataTable("tb"))

ds.Tables("tb").Columns.Add(New DataColumn("id", Type.GetType("System.String")))

ds.Tables("tb").Columns("id").ReadOnly = True

ds.Tables("tb").Columns.Add(New DataColumn("nm", Type.GetType("System.String")))

ds.Tables("tb").Columns("nm").ReadOnly = True

ds.Tables("tb").Columns.Add(New DataColumn("dsc", Type.GetType("System.String")))

ds.Tables("tb").Columns("dsc").ReadOnly = True

ds.Tables("tb").Columns.Add(New DataColumn("dt", Type.GetType("System.String")))

ds.Tables("tb").Columns("dt").ReadOnly = True

ds.Tables("tb").Columns.Add(New DataColumn("prc", Type.GetType("System.String")))

ds.Tables("tb").Columns("prc").ReadOnly = True



Dim ws As New wsPHP.Serv

Dim result() As wsPHP.outputarray

result = ws.getTb

If Not result Is Nothing Then

For i = 0 To result.Length - 1

Dim myRow As DataRow = ds.Tables("tb").NewRow

myRow("id") = result(i).id

myRow("nm") = result(i).nm

myRow("dsc") = result(i).dsc

myRow("dt") = result(i).dt

myRow("prc") = result(i).prc

ds.Tables("tb").Rows.Add(myRow)

Next

Else

MsgBox("Empty!")

End If

dg.DataSource = ds

dg.DataMember = "tb"



End Sub



Private Sub dg_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellContentClick



End Sub



Private Sub dg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dg.Click

If (dg.SelectedRows(0).Index >= 0) Then

txtid.Text = dg.Rows.Item(dg.SelectedRows(0).Index).Cells("id").Value

txtnm.Text = dg.Rows.Item(dg.SelectedRows(0).Index).Cells("nm").Value

txtdsc.Text = dg.Rows.Item(dg.SelectedRows(0).Index).Cells("dsc").Value

txtdt.Text = dg.Rows.Item(dg.SelectedRows(0).Index).Cells("dt").Value

txtprc.Text = dg.Rows.Item(dg.SelectedRows(0).Index).Cells("prc").Value

End If

End Sub



Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

Dim ws2 As New wsPHP.Serv

ws2.insertTb(txtid.Text, txtnm.Text, txtdsc.Text, txtdt.Text, txtprc.Text)



formWSClientPHP_Load(Me, e)

End Sub



Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

Dim ws2 As New wsPHP.Serv

ws2.updateTb(txtid.Text, txtnm.Text, txtdsc.Text, txtdt.Text, txtprc.Text)



formWSClientPHP_Load(Me, e)

End Sub



Private Sub btDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDelete.Click

Dim ws2 As New wsPHP.Serv

ws2.deleteTb(txtid.Text)



formWSClientPHP_Load(Me, e)

End Sub



Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

formWSClientPHP_Load(Me, e)

End Sub

End Class


Hasilnya dapat dilihat dengan klik Debug - Start Debugging (Run - F5)