CorteX Developer Forum Community

Re: VB.net Sample - Navigatigating URI's using DataGridView and ReadXML

CorteX Developer

Forums and files for CorteX Developers.

VB.net Sample - Navigatigating URI's using DataGridView and ReadXML

  • rated by 0 users
  • This post has 1 Reply |
  • 2 Followers
  • Here is a simple, helpful VB.net program to navigate the Emprise 5K (and 9K) and to visually display different URI using the dataset.readxml method and web requests/responses. Just click on any "self" link to explore.

     

    To start: create a new windows form project and add the following 5 controls to the form, leaving the default names. Arrange them as you wish.

     

    Add 2 buttons, label button1 as ‘Start’, label button2 as ‘Clear’

     

    Add 1  DataGridView control, will display the URI in a grid view

    Add 1  listbox control - holds the tables  names returned in the dataset.

    Add 1 textbox control – place for entering IP address.

    Paste the code below and give it a wirl..

    Cush

    >>>> code <<<<<<<

    Imports System.IO

    Imports System.Net

    Imports System.Xml

    Imports System.Text.RegularExpressions

     

    Public Class Form1

     

        Dim dsResponse As DataSet

        Dim strFrom As String = String.Empty

     

        Private Sub NavigateTo(ByVal strNext As String)

     

            Dim request As HttpWebRequest

            Dim response As HttpWebResponse = Nothing

     

            ' start that neat wait cursor thingy

            Me.Cursor = Cursors.WaitCursor

     

            ListBox1.Items.Clear()

     

            Try ' Create the web request  

                Me.Text = strNext

                request = DirectCast(WebRequest.Create(strNext), HttpWebRequest)

     

                ' Add authentication to request  - not needed for the query but will not effect anything.

                request.Credentials = New NetworkCredential("administrator", "administrator")

     

                ' Get response  

                response = DirectCast(request.GetResponse(), HttpWebResponse)

     

                dsResponse = New DataSet

     

                ' simple readxml as dataset

                dsResponse.ReadXml(response.GetResponseStream())

     

                ' build selection list of tables from whats in the dataset

                For Each table As DataTable In dsResponse.Tables

                    ListBox1.Items.Add(table.TableName)

                Next

     

            Catch webexception As WebException

                MessageBox.Show("HTTP Error: " & webexception.Message, "HTTP Error")

                '

                ' code here for handling errors based on what is retruned.

                '

            Finally

                If Not response Is Nothing Then response.Close()

            End Try

            Me.Cursor = Cursors.Default

        End Sub

     

     

        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

            Dim dt As DataTable = dsResponse.Tables(ListBox1.SelectedItem)

            DataGridView1.DataSource = dt

        End Sub

     

        Private Function CheckIPAddressValid(ByVal strin As String) As Boolean

            ' check that text is valid IPV4 IP using regex

            Dim strMatch As String = "^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"

            Dim matchit As Match = Nothing

     

            matchit = Regex.Match(strin, strMatch, RegexOptions.IgnoreCase)

            If matchit.Success Then

                Return True

            Else

                Return False

            End If

        End Function

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' Lets start at the "query" URI

            strFrom = "http://" & TextBox1.Text & "/query"

            NavigateTo(strFrom)

            Button2.Enabled = True

        End Sub

     

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Button1.Enabled = False

            Button2.Enabled = False

            DataGridView1.AllowUserToAddRows = False

            DataGridView1.AllowUserToDeleteRows = False

            DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

            DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

        End Sub

     

        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

            ' when textbox contains a valid IP  - button is enabled

            If CheckIPAddressValid(TextBox1.Text) = True Then

                Button1.Enabled = True

            Else

                Button1.Enabled = False

            End If

        End Sub

     

        Private Sub ListBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

            ' Listbox1 now has table names in the dataset

            Dim dt As DataTable = dsResponse.Tables(ListBox1.SelectedItem)

     

            'bind datagridview to the table to be displayed

            DataGridView1.DataSource = dt

     

        End Sub

     

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

            Dim iLinkColumn As Integer = 0

            'get the column index of the columne that is "self"

            iLinkColumn = DataGridView1.Columns.IndexOf(DataGridView1.Columns("self"))

            If (e.ColumnIndex = iLinkColumn) Then

                Dim link As String = DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString()

                NavigateTo(link)

            End If

        End Sub

     

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            DataGridView1.DataSource = Nothing

            ListBox1.Items.Clear()

            TextBox1.Text = String.Empty

            Button1.Enabled = False

            Button2.Enabled = False

        End Sub

    End Class

     

     

  • Hey Cush,

       Awesome post! 

    I had it running in 5 minutes and was poking through my dev ISE!  :)

    Thanks much!

    Butch>

Page 1 of 1 (2 items)