Bridging the Gap: Talking to MySQL From VB.NET Through PHP and XML - An Adventure in VB.NET
(Page 2 of 4 )
All it takes is a Form with a DataGrid placed on it, along with a couple of buttons to read new and old messages. Name the form frmMsg.vb and then name the three buttons btnNew, btnRead, btnClose and the DataGrid dgMsg

You will also need to create a new class named WebBridge.vb. Since it is the most important piece of the front-end, we will start here. Refer to the comments within the code for explanations.
'Import these namespaces to make our programming life a
'little easier
Imports System
Imports System.Net
Imports System.Text
Imports System.IO
Public Class WebBridge
Private _acc As String = "12345"
'Our test key
Private _url As String
'Target URL
Private _pdata As Object
'Post Data
Private _rsp As String
'Response from php page
Public ReadOnly Property Account()
As String 'Key to access page
Get 'ReadOnly ensures the value
Return _acc 'cannot be changed
End Get
End Property
Public Property URL() As String
'Target URL
Get
Return _url
End Get
Set(ByVal Value As String)
_url = Value
End Set
End Property
Public Property PostData() As Object
'Posting Data
Get
'Add Account value to post data
Return "acc=" & Account & "&" & _pdata
End Get
Set(ByVal Value As Object)
_pdata = Value
End Set
End Property
Public ReadOnly Property
XMLResponse() As String
'php page Response
Get
'Again ReadOnly so
Return _rsp
'it cannot be changed
End Get
'by outside influence.
End Property
Public Sub Connect()
'Primary procedure
'Create a new WebRequest object
'with the specified URL
Dim rq As WebRequest =
WebRequest.Create(URL)
'Set Method and Header information
With rq
.Method = "POST"
.ContentType =
"application/x-www-form-urlencoded"
End With
'Send the data to the php page
SendData(rq, PostData)
'Assign the response from the
'php page to the variable for XMLResponse
_rsp = GetXMLResponse(rq)
End Sub
Private Sub SendData(ByRef req As WebRequest,
ByVal data As String)
'Store the post data as a byte array
'for passing to the page
Dim b As Byte() =
Encoding.ASCII.GetBytes(data)
'Alert the request of the length of
'the post contents
Req.ContentLength = b.Length
'Create a Stream to send the
'RequestStream to the php page
Dim sr As Stream = req.GetRequestStream()
'Send the data
sr.Write(b, 0, b.length)
'Close the stream
sr.Close()
End Sub
Private Function GetXMLResponse(ByRef
req As WebRequest) As String
'Create a WebResponse object to get
'response from php page
Dim rs As WebResponse = req.GetResponse()
'Create a stream to capture the response
Dim sr As Stream = rs.GetResponseStream()
'Create a StreamReader to convert
'the stream to text
Dim xr As StreamReader = New StreamReader(sr)
'Return the response as text
Return xr.ReadToEnd()
End Function
End Class
This will handle all of the communication with the target php page as you will see shortly. There are only a few procedures needed for frmMsg. These will establish a connection to a target php page through a WebBridge object and will then display the appropriate information in our DataGrid. Our DataGrid’s table will be structured like our XML output from the target php page. You will see the connection later in the PHP section of the tutorial.
Private Sub ShowMessages
(ByVal
View As Byte)
'Create a new WebBridge Object
Dim wbg As New WebBridge
'Set up the WebBridge object’s
'values and call Connect()
With wbg
.URL=http://www.xyz.com/getmessagelist.php
.PostData = "view=" & View
.Connect()
'Display the XML data in dgMsg
CreateMessageList(.XMLResponse)
End With
End Sub
Private Sub CreateMessageList(ByVal
XMLString As String)
'Create a data set to hold the
'XML data
Dim ds As New DataSet
'Create a StringReader to store the
'XML data
Dim sr As New StringReader(XMLString)
'Create a new table in our dataset
'called "Message". You will see why soon.
ds.Tables.Add("Message")
'Add columns to the table. Again, the
'names will be explained soon.
With ds.Tables(0).Columns
.Add("MsgID")
.Add("From")
.Add("Email")
.Add("MsgDate")
.Add("MsgTime")
End With
'Read in the XMLString to populate the table
Ds.ReadXml(sr, XmlReadMode.IgnoreSchema)
'Display the Data in the DataGrid
dgMsg.SetDataBinding(ds,"Message")
End Sub
Private Sub btnNew_Click(ByVal sender
As System.Object, _ ByVal e As
System.EventArgs) Handles btnNew.Click
'Show New Messages
ShowMessages(0)
End Sub
Private Sub btnRead_Click(ByVal sender
As System.Object, _ ByVal e As
System.EventArgs) Handles btnRead.Click
'Show Read Messages
ShowMessages(1)
End Sub
Next: PHP: Talking the Universal Language >>
More Visual Basic.NET Articles
More By Nicholas Clayton