Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: ping using vb.error ??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    ping using vb.error ??

    i have tried altering some code i have to ping an ip address and store the result in an access db.i had it working when i had the ip hardcoded in as a string but I am now gettin an error.instead of having the ip address hardcoded in to the code i want it to be read in from the access db but im now getting this error:System.nullreferenceExceptionbject reference not set to an instance of an object at...button1.click event args e
    i have underlined the piece of code i tried to change

    Code:
    Imports System.Data
    
    
    Public Class Form1
    
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
       
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
    
            Try
                Ping = New Net.NetworkInformation.Ping
    
                pReply = Ping.Send(ds.Tables("monitor").Rows(0).Item(0))
    
                Label1.Text = pReply.Status.ToString
    
    
    
    
                If pReply.Status = Net.NetworkInformation.IPStatus.Success Then
    
    
                    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
                    con.Open()
    
                    sql = "SELECT * FROM tblone"
                    da = New OleDb.OleDbDataAdapter(sql, con)
                    da.Fill(ds, "monitor")
    
    
                    Dim cb As New OleDb.OleDbCommandBuilder(da)
    
                    ds.Tables("monitor").Rows(0).Item(1) = Label1.Text
    
                    da.Update(ds, "monitor")
    
    
    
    
    
    
    
                Else
                    Label1.BackColor = Color.Red
                End If
    
            Catch ex As Exception
    
                MessageBox.Show(ex.ToString())
    
            End Try
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
            End
    
        End Sub
    End Class

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    You didn't need to open a new thread for this...

    You are trying to access the data to early in the code, you have your fill method after the ds.Tables("monitor").Rows(0).Item(0)...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    sorry my bad ! new to the whole forum thing thought cos i marked it as resolved that i would have had to start a new one ! yeah that worked perfect thanks mickey !! got one more question tho ! my access db contains lots of entries how could i get this code to loop through the db and ping all the ip's and store the results in the column next to it ? also the number of ip's will vary so the looping process would have to be 'dynamic' if that makes any sense ??

    Code:
    Imports System.Data
    
    
    Public Class Form1
    
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Open()
    
            sql = "SELECT * FROM tblone"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
    
            Try
                Ping = New Net.NetworkInformation.Ping
    
                pReply = Ping.Send(ds.Tables("monitor").Rows(0).Item(0))
    
                Label1.Text = pReply.Status.ToString
    
    
                Dim cb As New OleDb.OleDbCommandBuilder(da)
    
                ds.Tables("monitor").Rows(0).Item(1) = Label1.Text
    
                da.Update(ds, "monitor")
    
            Catch ex As Exception
    
                MessageBox.Show(ex.ToString())
    
            End Try
    
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
            End
    
        End Sub
    End Class

  4. #4
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    You can do that using something like this:

    Drag a new datagridview to your form, add two columns to the datagridview and then use this code

    VB.NET Code:
    1. Dim dgvr As DataGridViewRow
    2.  
    3.         For i = 0 To ds.Tables("monitor").RowsCount - 1
    4.             dgvr = New DataGridViewRow
    5.             DataGridView1.Rows.Add(dgvr)
    6.             DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0)
    7.             DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status
    8.             If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then
    9.                 DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
    10.             Else
    11.                 DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
    12.             End If
    13.  
    14.         Next

    I'm leaving to lunch... good luck

    EDIT - You must place the ping inside the for...
    Last edited by mickey_pt; Jul 8th, 2009 at 06:39 AM. Reason: Forgot something

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    how do i use this datagridview ? is it def in 2008 express edition ?-forgive the ignorance but ive avoided using any data connection wizards so far to try and better understand the code(something which i struggle to do!) !!

  6. #6
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    I think it's but i don't use the express edition...

    You don't need any real data connection, just go to the toolbox, in the data section, just drag and drop a datagridview to the form, then in the little arrow in the top right you can edit columns, click there and add two columns... you can uncheck the enable adding,deleting and editing.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    perfect found it anyways ! tried editing the code but im gettin the following error at run-time when i press button1:Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
    ive underlined the code thats throwing the error. any ideas ??


    Code:
    Imports System.Data
    
    
    Public Class Form1
    
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'VBtestDataSet.tblone' table. You can move, or remove it, as needed.
            Me.TbloneTableAdapter.Fill(Me.VBtestDataSet.tblone)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Open()
    
            sql = "SELECT * FROM tblone"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
    
            Dim dgvr As DataGridViewRow
            For i = 0 To ds.Tables("monitor").RowsCount - 1           dgvr = New DataGridViewRow
                DataGridView1.Rows.Add(dgvr)
                DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0)
                DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status
    
                Try
                    Ping = New Net.NetworkInformation.Ping
    
                    pReply = Ping.Send(ds.Tables("monitor").Rows(0).Item(0))
    
                    Label1.Text = pReply.Status.ToString
    
    
                    Dim cb As New OleDb.OleDbCommandBuilder(da)
    
                    ds.Tables("monitor").Rows(0).Item(1) = Label1.Text
    
                    da.Update(ds, "monitor")
    
                Catch ex As Exception
    
                    MessageBox.Show(ex.ToString())
    
                End Try
    
    
                If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then
    
    
                    datagridview1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
                Else
                    datagridview1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
                End If
            Next
    
    
    
    
          
    
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
            End
    
        End Sub
    
        Private Sub TbloneBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TbloneBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.TbloneBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.VBtestDataSet)
    
        End Sub
    End Class

  8. #8
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    You added a datasource to your project...


    In that arrow that i told you before, in choose datasource just put none...


    Then in this code remove this in form load
    VB Code:
    1. Me.TbloneTableAdapter.Fill(Me.VBtestDataSet.tblone)

    If you wish to clean the form and code

    Go to the solution explorer and remove VBtestDataSet

    In the form remove the toolbar that you have in the top (arrows,save,plus,...), and all components that you have in the bottom (data related, table adapter, binding source, dataset,...). Remove also this code
    VB Code:
    1. Private Sub TbloneBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TbloneBindingNavigatorSaveItem.Click
    2.         Me.Validate()
    3.         Me.TbloneBindingSource.EndEdit()
    4.         Me.TableAdapterManager.UpdateAll(Me.VBtestDataSet)
    5.  
    6.     End Sub


    And the order in your code isn't the correct one...

    Try this version:
    VB.NET Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim ds As New DataSet
    3.         Dim da As OleDb.OleDbDataAdapter
    4.         Dim sql As String
    5.         Dim con As New OleDb.OleDbConnection
    6.  
    7.         con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
    8.         con.Open()
    9.         sql = "SELECT * FROM tblone"
    10.         da = New OleDb.OleDbDataAdapter(sql, con)
    11.         da.Fill(ds, "monitor")
    12.  
    13.  
    14.         Dim cb As New OleDb.OleDbCommandBuilder(da)
    15.         Ping = New Net.NetworkInformation.Ping
    16.  
    17.         Dim dgvr As DataGridViewRow
    18.         For i = 0 To ds.Tables("monitor").RowsCount - 1
    19.             dgvr = New DataGridViewRow 'Creates a new row
    20.             DataGridView1.Rows.Add(dgvr) 'Add the row
    21.  
    22.             Try
    23.                 pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(0)) 'Sends the ping to the current item
    24.  
    25.                 DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0) 'Updates the value of the new Item
    26.                 DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned
    27.  
    28.                 If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell
    29.                     DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
    30.                 Else
    31.                     DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
    32.                 End If
    33.  
    34.  
    35.                 ds.Tables("monitor").Rows(i).Item(1) = pReply.Status 'Write in the original datatable
    36.                 da.Update(ds, "monitor") 'Updates the database
    37.  
    38.             Catch ex As Exception
    39.                 MessageBox.Show(ex.ToString())
    40.             End Try
    41.         Next
    42.     End Sub

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    thanks ! i added those to colums in the dataviewgrid but i didnt bind them to any data was this correct thing to do ? I made all the changes but still getting one error:

    System.IndexOutOfRangeException:There is no row at position 5.
    at System.Data.RBTree`1.getnodeByIndex(Int32 user Index).......
    .....vbline 35 - maybe something to do with there being only 5 entries in the db ??

    Code:
    Imports System.Data
    
    
    Public Class Form1
    
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Open()
    
            sql = "SELECT * FROM tblone"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Ping = New Net.NetworkInformation.Ping
    
            Dim dgvr As DataGridViewRow
            For i = 0 To ds.Tables("monitor").Rows.Count - 1
            Next
            dgvr = New DataGridViewRow 'Creates a new row            
            DataGridView1.Rows.Add(dgvr) 'Add the row             
    
            Try
           
                pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(0)) 'Sends the ping to the current item                
    
                DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0) 'Updates the value of the new Item                
    
                DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned                 
    
                If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell         
    
                    DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
                Else
                    DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
                End If
    
                ds.Tables("monitor").Rows(i).Item(1) = pReply.Status 'Write in the original datatable
                da.Update(ds, "monitor") 'Updates the database
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            'Next
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
            End
    
        End Sub
    
    
    End Class

  10. #10
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    Replace this:
    vb Code:
    1. For i = 0 To ds.Tables("monitor").Rows.Count - 1
    for this
    vb Code:
    1. For i = 0 To ds.Tables("monitor").Rows.Count - 2

    and check what happens

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    another error im afraid:

    System.ArguementOutOfRangeException: Index was out of range.Must be non negative and less than the size of the collection.
    Parameter name: index
    at System.Collections.ArrayList.get_Item(Int32 index)

    i declared i as an integer(which wasnt in your code) as it wasnt recognising i,might that have somethin to do with it?

    Code:
    Imports System.Data
    
    
    Public Class Form1
    
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Open()
    
            sql = "SELECT * FROM tblone"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Ping = New Net.NetworkInformation.Ping
    
            Dim dgvr As DataGridViewRow
            For i = 0 To ds.Tables("monitor").Rows.Count - 2
            Next
            dgvr = New DataGridViewRow 'Creates a new row            
            DataGridView1.Rows.Add(dgvr) 'Add the row             
    
            Try
           
                pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(0)) 'Sends the ping to the current item                
    
                DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0) 'Updates the value of the new Item                
    
                DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned                 
    
                If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell         
    
                    DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
                Else
                    DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
                End If
    
                ds.Tables("monitor").Rows(i).Item(1) = pReply.Status 'Write in the original datatable
                da.Update(ds, "monitor") 'Updates the database
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            'Next
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
            End
    
        End Sub
    
    
    End Class

  12. #12
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    Here works fine...

    VB.NET Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim i As Integer
    3.         Dim ds As New DataSet
    4.         Dim da As OleDb.OleDbDataAdapter
    5.         Dim sql As String
    6.         Dim con As New OleDb.OleDbConnection
    7.  
    8.         con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
    9.         con.Open()
    10.  
    11.         sql = "SELECT * FROM tblone"
    12.         da = New OleDb.OleDbDataAdapter(sql, con)
    13.         da.Fill(ds, "monitor")
    14.  
    15.         Dim cb As New OleDb.OleDbCommandBuilder(da)
    16.         Ping = New Net.NetworkInformation.Ping
    17.  
    18.         Dim dgvr As DataGridViewRow
    19.         For i = 0 To ds.Tables("monitor").Rows.Count - 2
    20.         dgvr = New DataGridViewRow 'Creates a new row            
    21.         DataGridView1.Rows.Add(dgvr) 'Add the row            
    22.  
    23.         Try
    24.        
    25.             pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(0)) 'Sends the ping to the current item                
    26.  
    27.             DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0) 'Updates the value of the new Item                
    28.  
    29.             DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned                
    30.  
    31.             If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell        
    32.  
    33.                 DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
    34.             Else
    35.                 DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
    36.             End If
    37.  
    38.             ds.Tables("monitor").Rows(i).Item(1) = pReply.Status 'Write in the original datatable
    39.             da.Update(ds, "monitor") 'Updates the database
    40.         Catch ex As Exception
    41.             MessageBox.Show(ex.ToString())
    42.         End Try
    43.         Next
    44.     End Sub

    Just replace the button click with this code.... You have added a next after the for... and commented the correct next...

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    ha your a genius ! congrats you've earned the rest of the day off !!

  14. #14
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    By the way the for should be till -1 and not -2, check your database if the last field has all values...

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    okay for the next part i tried using the code on a copy of the actual table im using ,i changed tblone to tblSwitch and ip and status are now the 6th and 7th column (item(s)5,6) but im getting an error because of the other columns in the table ? is there a way to get the code to ignore these other columns ? and also to read in and output the results so that the table is in the same order as it was before the vb app runs ?

    error: System.Data.oledb.oledbException:syntax error (missing operator) in query expression '((switchid=?) and ((?=1 and Switchname is null)....... and it continues to name out the headings of the rest of the columns in the table



    Code:
    Imports System.Data
    
    
    Public Class Form1
    
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Open()
    
            sql = "SELECT * FROM tblSwitch"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Ping = New Net.NetworkInformation.Ping
    
            Dim dgvr As DataGridViewRow
            For i = 0 To ds.Tables("monitor").Rows.Count - 1
    
                dgvr = New DataGridViewRow 'Creates a new row            
                DataGridView1.Rows.Add(dgvr) 'Add the row             
    
                Try
    
                    pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(5)) 'Sends the ping to the current item                
    
                    DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(5) 'Updates the value of the new Item                
    
                    DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned                 
    
                    If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell         
    
                        DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
                    Else
                        DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
                    End If
    
                    ds.Tables("monitor").Rows(i).Item(6) = pReply.Status 'Write in the original datatable
                    da.Update(ds, "monitor") 'Updates the database
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
            Next
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
            End
    
        End Sub
    
    End Class
    any ideas whats wrong ?

  16. #16
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    I think that you must set some values in the new table, like you said, now you have more columns, and you have to set the values for that columns...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    so what your saying is i have to set values for the columns in vb even though the columns arent in the datagrid view or arent referenced in the code ?

  18. #18
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    yes, because those columns are required for the table...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    im a little confused , is this something along the right lines or am i way off ??

    vb Code:
    1. dim switchID as integer
    2. ds.Tables("monitor").Rows(0).Item(0) = switchID

    sorry for answering my own thread but im pretty sure this would only set switchID to an integer value no ?

  20. #20
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    You can do something like that, but don't forget to set all values in the row...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  21. #21
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ping using vb.error ??

    Im confused, you are just trying to update ONE column in the database when you find out if the ping is successful right? If that is the case then you shouldn't have to specify values for all of the other columns... or am I missing something and you are trying to add a new row rather than update an existing one?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  22. #22

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    yeah thats the idea chris, read in the ip from one column (item5) and update the result into the next column (item6) im a bit confused aswell as to why the code cant just ignore the other colums ?

  23. #23
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    I thought that to, but the error tells that is missing all the values:

    in query expression '((switchid=?) and ((?=1 and Switchname is null)....... and it continues to name out the headings of the rest of the columns in the table

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  24. #24
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ping using vb.error ??

    To be honest I found it hard to understand and had problems like this when I started trying to work with databases and in the end I found it much easier to understand and work with if I didnt use the DataSets and DataAdapters etc. So I just update the database manually using an SqlCommand (guessing its OledbCommand for you with Access though), so I would just read in the IPs and the unique ID of each row from the database, send the pings and then update each row in the database manually with the result. The SqlCommand statement would look like this:
    vb Code:
    1. Dim updatecommand As New SqlCommand("UPDATE MyTable SET ResultColumn=@Result WHERE ID=@ID", MySqlConnection)
    obviously thats a rough example but you get the idea (@result and @ID are parameters, I just havent bothered to write that bit of code that adds them as its just a brief example)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  25. #25

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    the code above(#15) still reads in the two columns into the datagrid viewer and pings the ip address but it just doesnt write the result to the db . that make any sense ?

  26. #26
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ping using vb.error ??

    OK here's something that works fine for me, it updates the relevant row with the result from each ping. This is my preferred method of doing things like this but I am not saying it is any better or worse than the existing way you are doing it, I just know it works.

    vb.net Code:
    1. Private Sub PingBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PingBtn.Click
    2.  
    3.         'Create our connection to the Access database
    4.         Dim AccessConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test\pingDB.mdb;User Id=admin;Password=;")
    5.         'Create the command that will be used to select all of the data in the IP and ID columns
    6.         Dim SelectCommand As New OleDbCommand("SELECT IP,ID FROM PingTable", AccessConnection)
    7.  
    8.         Try
    9.             'Open the connection to the database
    10.             AccessConnection.Open()
    11.  
    12.             'Use a DataReader to access the data that the SELECT command returns
    13.             Dim Reader As OleDbDataReader = SelectCommand.ExecuteReader
    14.  
    15.             'Start to loop through the results
    16.             'This loop will be restarted for each row in the database
    17.             Do While Reader.Read
    18.  
    19.                 Dim PingSend As New Net.NetworkInformation.Ping
    20.  
    21.                 'As you can see we use the DataReader to get the value of the
    22.                 'column named IP in the current row that we are looping through and we
    23.                 'send a ping to that
    24.                 Dim PingResult As Net.NetworkInformation.PingReply = PingSend.Send(Reader("IP"))
    25.                 Dim UpdateCommand As OleDbCommand 'will use this in a minute
    26.  
    27.                 'Check the result of the ping and update the database accordingly
    28.                 If PingResult.Status = Net.NetworkInformation.IPStatus.Success Then
    29.                     'Create the command object that will update the table and set the
    30.                     'command text to set the Result column to True
    31.                     UpdateCommand = New OleDbCommand("UPDATE PingTable SET Result=True WHERE ID=@ID", AccessConnection)
    32.                     'Write to richtextbox on the form
    33.                     logbox.AppendText("Reply from " & Reader("IP").ToString & vbNewLine)
    34.                 Else
    35.                     'Create the command object that will update the table and set the
    36.                     'command text to set the Result column to False
    37.                     UpdateCommand = New OleDbCommand("UPDATE PingTable SET Result=False WHERE ID=@ID", AccessConnection)
    38.                     'Write to richtextbox on the form
    39.                     logbox.AppendText(Reader("IP").ToString & " did not respond" & vbNewLine)
    40.                 End If
    41.  
    42.                 'Add the parameter @ID that is referenced in the update command string
    43.                 'We set this to the value of the ID column in the row that the DataReader
    44.                 'is currently looping through
    45.                 UpdateCommand.Parameters.Add("@ID", OleDbType.Integer).Value = Reader("ID")
    46.  
    47.                 'Execure the update command. This is the point where the Result column is
    48.                 'actually updated
    49.                 UpdateCommand.ExecuteNonQuery()
    50.  
    51.                 'Start the loop again for the next row in the database
    52.             Loop
    53.         Catch ex As Exception
    54.             'Very basic error handling
    55.             logbox.AppendText("ERROR: " & ex.Message & vbNewLine)
    56.         Finally
    57.             'Make sure we close the connection
    58.            
    59.                 AccessConnection.Close()
    60.            
    61.         End Try
    62.  
    63.     End Sub
    It is by no means perfect and you wont be able to just copy and paste it because I have probably got different column names to you and will also have different database path etc. It also freezes while it is executing the pings because it is not using a background thread etc but its just an example. Also, the object you see in there named "logbox" is a richtextbox on my form that I just used to output something on the screen so that I didnt have to keep checking the database each time I ran it etc Hope it helps and feel free to ask any questions.
    Last edited by chris128; Jul 9th, 2009 at 06:30 AM. Reason: added comments to code
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  27. #27
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ping using vb.error ??

    I've updated my last post to add comments to the code so that should help you see what its doing
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  28. #28
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    Just for testing...

    Add a new datagridview to your form... resize it if you have to...

    Then in the for cycle just add this after this lines
    VB.NET Code:
    1. sql = "SELECT * FROM tblSwitch"
    2.         da = New OleDb.OleDbDataAdapter(sql, con)
    3.         da.Fill(ds, "monitor")

    VB.NET Code:
    1. Datagridview2.datasource=ds.tables("monitor")

    And comment the da.Update line, and check what changes do you see in the datagridview2 after running the code...

    *EDIT* - If you want you can try this, or use the method of chris128
    Last edited by mickey_pt; Jul 9th, 2009 at 06:23 AM. Reason: To slow

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    tried using your method chris but got this error in the logbox:
    ERROR: IErrorInfo.GetDescription failed with E_FAIL(0x80004005).

    i did have to make a few changes to the code also to make it compatible
    ie OleDbCommand => OleDb.OleDbCommand

    Code:
    Imports System.Data
    
    
    Public Class Form1
    
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
        Private Sub PingBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PingBtn.Click
    
            'Create our connection to the Access database       
            Dim AccessConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb;User Id=admin;Password=;")
            'Create the command that will be used to select all of the data in the IP and ID columns        
            Dim SelectCommand As New OleDb.OleDbCommand("SELECT SwitchID,SwitchName,ComId,Make/Model,Port Type(s),IP,Result,Number of ports FROM tblSwitch", AccessConnection)
    
            Try
    
                'Open the connection to the database        
                AccessConnection.Open()
                'Use a DataReader to access the data that the SELECT command returns           
                Dim Reader As OleDb.OleDbDataReader = SelectCommand.ExecuteReader
                'Start to loop through the results            
                'This loop will be restarted for each row in the database
                Do While Reader.Read
                    Dim PingSend As New Net.NetworkInformation.Ping
                    Dim PingResult As Net.NetworkInformation.PingReply = PingSend.Send(Reader("IP"))
                    Dim UpdateCommand As OleDb.OleDbCommand
                    If PingResult.Status = Net.NetworkInformation.IPStatus.Success Then
    
                        UpdateCommand = New OleDb.OleDbCommand("UPDATE PingTable SET Result=True WHERE ID=@ID", AccessConnection)
                        logbox.AppendText("Reply from " & Reader("IP").ToString & vbNewLine)
                    Else
                        UpdateCommand = New OleDb.OleDbCommand("UPDATE PingTable SET Result=False WHERE ID=@ID", AccessConnection)
                        logbox.AppendText(Reader("IP").ToString & " did not respond" & vbNewLine)
                    End If
    
                    UpdateCommand.Parameters.Add("@ID", OleDb.OleDbType.Integer).Value = Reader("ID")
                    UpdateCommand.ExecuteNonQuery()
    
                Loop
            Catch ex As Exception  'Very basic error handling           
                logbox.AppendText("ERROR: " & ex.Message & vbNewLine)
            Finally 'Make sure we close the connection
    
                If AccessConnection.State = ConnectionState.Open Then
    
    
                    AccessConnection.Close()
                End If
            End Try
        End Sub
    
    
    
    
    
    
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
            End
        End Sub
    
    End Class

  30. #30

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    will also try your method mickey !

  31. #31
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    My method it's only to see what changes the data table suffers in the process...

    Doesn't really solve your problem, but if all rows in the data table have values, then calling the update method at end of the cycle it must do the job.

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  32. #32

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    i added the second datagridview (columns 3 and 4) but wasnt clear on the parts of the code you wanted me to edit can you show below please

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Open()
    
            sql = "SELECT * FROM tblone"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Ping = New Net.NetworkInformation.Ping
    
            Dim dgvr As DataGridViewRow
            For i = 0 To ds.Tables("monitor").Rows.Count - 1
    
                dgvr = New DataGridViewRow 'Creates a new row            
                DataGridView1.Rows.Add(dgvr) 'Add the row             
    
                Try
    
                    pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(0)) 'Sends the ping to the current item                
    
                    DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0) 'Updates the value of the new Item                
    
                    DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned                 
    
                    If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell         
    
                        DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
                    Else
                        DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
                    End If
    
                    ds.Tables("monitor").Rows(i).Item(1) = pReply.Status 'Write in the original datatable
                    da.Update(ds, "monitor") 'Updates the database
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
            Next
        End Sub

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    okay ive tried yet another approach,working off the code in post#12, i changed the sql command from select * from tblSwitch to just Select IP,Status from tblSwitch. and ive moved the two columns(IP,Status) so that they are the first two columns in the table (items 0 and 1) but im still getting the following error:

    System.InvalidOperationExceptionynamic SQL generation for the updatecommand is not supported against a selectcommand that does not return any key column information....

    any ideas how to solve this error ?? - code is below


    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Open()
    
            sql = "SELECT IP,Status FROM tblSwitch"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Ping = New Net.NetworkInformation.Ping
    
            Dim dgvr As DataGridViewRow
            For i = 0 To ds.Tables("monitor").Rows.Count - 1
    
                dgvr = New DataGridViewRow 'Creates a new row            
                DataGridView1.Rows.Add(dgvr) 'Add the row             
    
                Try
    
                    pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(0)) 'Sends the ping to the current item                
    
                    DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0) 'Updates the value of the new Item                
    
                    DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned                 
    
                    If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell         
    
                        DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
                    Else
                        DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
                    End If
    
                    ds.Tables("monitor").Rows(i).Item(1) = pReply.Status 'Write in the original datatable
                    da.Update(ds, "monitor") 'Updates the database
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
            Next
        End Sub

  34. #34
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    The datagridview2 doesnt have any columns, let it empty...

    VB.NET Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim i As Integer
    3.         Dim ds As New DataSet
    4.         Dim da As OleDb.OleDbDataAdapter
    5.         Dim sql As String
    6.         Dim con As New OleDb.OleDbConnection
    7.  
    8.         con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
    9.         con.Open()
    10.  
    11.         sql = "SELECT * FROM tblone"
    12.         da = New OleDb.OleDbDataAdapter(sql, con)
    13.         da.Fill(ds, "monitor")
    14.         DataGridView2.DataSource = ds.Tables("monitor")
    15.  
    16.         Dim cb As New OleDb.OleDbCommandBuilder(da)
    17.         Ping = New Net.NetworkInformation.Ping
    18.  
    19.         Dim dgvr As DataGridViewRow
    20.         For i = 0 To ds.Tables("monitor").Rows.Count - 1
    21.  
    22.             dgvr = New DataGridViewRow 'Creates a new row            
    23.             DataGridView1.Rows.Add(dgvr) 'Add the row            
    24.  
    25.             Try
    26.                 pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(0)) 'Sends the ping to the current item                
    27.                 DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(0) 'Updates the value of the new Item                
    28.                 DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned                
    29.                 If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell        
    30.                     DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
    31.                 Else
    32.                     DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
    33.                 End If
    34.  
    35.                 ds.Tables("monitor").Rows(i).Item(1) = pReply.Status 'Write in the original datatable
    36.                 'da.Update(ds, "monitor") 'Updates the database
    37.             Catch ex As Exception
    38.                 MessageBox.Show(ex.ToString())
    39.             End Try
    40.         Next
    41.     End Sub

    If you can't solve this,and if you don't mind attach your project and your db and i take a look to it... and correct the code...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  35. #35

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    perfect thanks ! can i attach straight to the forum ? cant see how !! if not i can email it to you its only 111k when zipped !

  36. #36
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ping using vb.error ??

    Quote Originally Posted by markhorgan1 View Post
    tried using your method chris but got this error in the logbox:
    ERROR: IErrorInfo.GetDescription failed with E_FAIL(0x80004005).

    i did have to make a few changes to the code also to make it compatible
    ie OleDbCommand => OleDb.OleDbCommand

    Code:
    Imports System.Data
    
    
    Public Class Form1
    
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
        Private Sub PingBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PingBtn.Click
    
            'Create our connection to the Access database       
            Dim AccessConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb;User Id=admin;Password=;")
            'Create the command that will be used to select all of the data in the IP and ID columns        
            Dim SelectCommand As New OleDb.OleDbCommand("SELECT SwitchID,SwitchName,ComId,Make/Model,Port Type(s),IP,Result,Number of ports FROM tblSwitch", AccessConnection)
    
            Try
    
                'Open the connection to the database        
                AccessConnection.Open()
                'Use a DataReader to access the data that the SELECT command returns           
                Dim Reader As OleDb.OleDbDataReader = SelectCommand.ExecuteReader
                'Start to loop through the results            
                'This loop will be restarted for each row in the database
                Do While Reader.Read
                    Dim PingSend As New Net.NetworkInformation.Ping
                    Dim PingResult As Net.NetworkInformation.PingReply = PingSend.Send(Reader("IP"))
                    Dim UpdateCommand As OleDb.OleDbCommand
                    If PingResult.Status = Net.NetworkInformation.IPStatus.Success Then
    
                        UpdateCommand = New OleDb.OleDbCommand("UPDATE PingTable SET Result=True WHERE ID=@ID", AccessConnection)
                        logbox.AppendText("Reply from " & Reader("IP").ToString & vbNewLine)
                    Else
                        UpdateCommand = New OleDb.OleDbCommand("UPDATE PingTable SET Result=False WHERE ID=@ID", AccessConnection)
                        logbox.AppendText(Reader("IP").ToString & " did not respond" & vbNewLine)
                    End If
    
                    UpdateCommand.Parameters.Add("@ID", OleDb.OleDbType.Integer).Value = Reader("ID")
                    UpdateCommand.ExecuteNonQuery()
    
                Loop
            Catch ex As Exception  'Very basic error handling           
                logbox.AppendText("ERROR: " & ex.Message & vbNewLine)
            Finally 'Make sure we close the connection
    
                If AccessConnection.State = ConnectionState.Open Then
    
    
                    AccessConnection.Close()
                End If
            End Try
        End Sub
    
    
    
    
    
    
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
            End
        End Sub
    
    End Class
    Well you have not altered my code properly to run with your table
    You haven't updated the UpdateCommand text, so it is still trying to update a table called PingTable. Also your select command is retrieving unnecessary columns (and as some of these have spaces and special characters in then your command is probably not working correctly anyway). You only need to select the ID and IP columns as they are the only ones that the Reader tries to use.

    Oh and this bit:
    Code:
    Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\VBtest.mdb"
            con.Close()
    Is totally pointless I'm afraid as all it is doing is creating a new connection and then closing it. Besides the fact that the connection is not ever Opened, this is also pointless because it is not affecting the connection that you used in the rest of the code.
    Last edited by chris128; Jul 9th, 2009 at 10:26 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  37. #37
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    The problem was in your database, the name of the columns can't have spaces and some special character like the '/' that you have....

    Remove all the spaces, and the bars and use this code...
    Attached Files Attached Files

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  38. #38

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    98

    Re: ping using vb.error ??

    i have this code:

    Code:
    Public Class Form1
        Dim Ping As Net.NetworkInformation.Ping
        Dim pReply As Net.NetworkInformation.PingReply
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim sql As String
            Dim con As New OleDb.OleDbConnection
           
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\Network Map.mdb"
            con.Open()
    
            sql = "SELECT * FROM tblSwitch"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "monitor")
    
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Ping = New Net.NetworkInformation.Ping
    
            Dim dgvr As DataGridViewRow
            For i = 0 To ds.Tables("monitor").Rows.Count - 1
    
                ' Set up the progress bar's properties
                ProgressBar1.Minimum = 0
                ProgressBar1.Maximum = 100
                ProgressBar1.Value = 0
    
    
    
                dgvr = New DataGridViewRow 'Creates a new row            
                DataGridView1.Rows.Add(dgvr) 'Add the row             
    
                Try
    
                    pReply = Ping.Send(ds.Tables("monitor").Rows(i).Item(5)) 'Sends the ping to the current item                
    
                    DataGridView1.Rows.Item(i).Cells(0).Value = ds.Tables("monitor").Rows(i).Item(5) 'Updates the value of the new Item                
    
                    DataGridView1.Rows.Item(i).Cells(1).Value = pReply.Status 'The value of the status returned                 
    
                    If pReply.Status <> Net.NetworkInformation.IPStatus.Success Then 'Sets the color of the cell         
    
                        DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Red
                    Else
                        DataGridView1.Rows.Item(i).Cells(1).Style.BackColor = Color.Green
                    End If
    
                    ds.Tables("monitor").Rows(i).Item(6) = pReply.Status 'Write in the original datatable
                    da.Update(ds, "monitor") 'Updates the database
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
            Next
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Dim con As New OleDb.OleDbConnection
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\mhorgan\Desktop\Network Map.mdb"
            con.Close()
            End
    
        End Sub
    
      
    End Class



    to ping a list of ip's in a db , it works perfectly on a small sample db but when i switch it over to the db i intend to use it on it throws the error below

    the db im using has over 400 ip's so i think the problem is that vb cant hold enough of the results of the pings before it commits them to the db/displays them in the datagridview.

    getting this error: The CLR has been unable to transition from COM context 0x33adf80 to COM context 0x33ae0f0 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

    anyone know how these CoWaitForMultipleHandles work ? would appreciate any help !

  39. #39
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: ping using vb.error ??

    That error happens because the application stay's long time without responses...

    You have two options, create a new thread that do the job in the background. Or you can simple put Application.doEvents(), to the application don't freeze...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  40. #40
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ping using vb.error ??

    The best thing to do would be to put the ping work in a background thread but you may find that a bit complicated if you are new to VB.NET in general. Sticking Application.DoEvents() in the start of the loop will probably get rid of the error you are getting but you will still find that your program appears to freeze for a few seconds while it sends each ping.

    PS you might want to create a new thread for problems like this instead of just adding on to this already quite long thread each time you have a problem with this same program
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width