Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Bridging the Gap: Talking to MySQL From VB...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Bridging the Gap: Talking to MySQL From VB.NET Through PHP and XML
By: Nicholas Clayton
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 33
    2004-02-04

    Table of Contents:
  • Bridging the Gap: Talking to MySQL From VB.NET Through PHP and XML
  • An Adventure in VB.NET
  • PHP: Talking the Universal Language
  • What's Said and Done in the End

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    VB.NET with PHP and XML for MySQL

    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(b0b.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
    .EventArgsHandles 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

    More Visual Basic.NET Articles
    More By Nicholas Clayton


     

    VISUAL BASIC.NET ARTICLES

    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT