0
Imports System
Imports SnmpSharpNet

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

    End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    DataGridView1.Columns.Add("NameOne", "Column One")
    DataGridView1.Columns.Add("NameTwo", "Column Two")

    Dim dataGridRow As New DataGridViewRow()
    Dim cells As DataGridViewCell() = New DataGridViewCell(1) {}
    Dim txt1A As New DataGridViewTextBoxCell()
    Dim txt1B As New DataGridViewTextBoxCell()
    txt1A.Value = "Host"
    dataGridRow.Cells.Add(txt1A)
    txt1A.[ReadOnly] = False
    dataGridRow.Cells.Add(txt1B)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)



    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}

    Dim txt2A As New DataGridViewTextBoxCell()
    Dim cbo1 As New DataGridViewComboBoxCell()

    cbo1.Items.Add("1.3.6.1.2.1.1.1.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.2.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.3.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.4.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.5.0")

    cbo1.Value = cbo1.Items(0)

    cbo1.[ReadOnly] = False
    txt2A.Value = "OIDs"
    dataGridRow.Cells.Add(txt2A)
    txt2A.[ReadOnly] = True
    dataGridRow.Cells.Add(cbo1)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)

    Dim requestOid() As String

    requestOid = New String() {cbo1.Selected}

    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}
    Dim txt3A As New DataGridViewTextBoxCell()
    Dim cbo2 As New DataGridViewComboBoxCell()

    cbo2.Items.Add("Get")
    cbo2.Items.Add("GetNext")


    cbo2.Value = cbo2.Items(0)

    cbo2.[ReadOnly] = False
    txt3A.Value = "SNMP Operation"
    dataGridRow.Cells.Add(txt3A)
    txt3A.[ReadOnly] = True
    dataGridRow.Cells.Add(cbo2)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)

    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}
    Dim txt4A As New DataGridViewTextBoxCell()
    Dim txt4B As New DataGridViewTextBoxCell()
    txt4A.Value = "Community String"
    txt4B.Value = "public"
    dataGridRow.Cells.Add(txt4A)
    dataGridRow.Cells.Add(txt4B)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)



End Sub
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)

    Dim comboControl As DataGridViewComboBoxEditingControl = TryCast(e.Control, DataGridViewComboBoxEditingControl)
    If comboControl IsNot Nothing Then
        ' Set the DropDown style to get an editable ComboBox
        If comboControl.DropDownStyle <> ComboBoxStyle.DropDown Then
            comboControl.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End If
End Sub


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

    Dim result As Dictionary(Of Oid, AsnType)
    Dim requestoid() As String
    Dim cbo1 As New DataGridViewComboBoxCell()
    Dim txt1B As New DataGridViewTextBoxCell()
    Dim txt4B As New DataGridViewTextBoxCell()
    Dim host As String = txt1B.Value

    Dim community As String
    community = txt4B.Value
    requestoid = New String() {cbo1.Selected}

    Dim snmp As New SimpleSnmp
    snmp = New SimpleSnmp(host, community)

    'abc.Text = txtsnmpaction.SelectedItem
    result = snmp.Get(SnmpVersion.Ver1, requestoid)

    'result = snmp.GetNext(SnmpVersion.Ver1, requestoid)

    'If (txtsnmpaction = "GetBulk")
    'result = snmp.GetBulk(New String() {".1.3.6.1.2", ".1.3.6.1.3"})
    ' End If
    If Not snmp.Valid Then

        MessageBox.Show("Invalid hostname/community")

    End If
    If result IsNot Nothing Then
        Dim kvp As KeyValuePair(Of Oid, AsnType)
        For Each kvp In result
            MessageBox.Show("kvp.Key.ToString")
            MessageBox.Show(SnmpConstants.GetTypeName(kvp.Value.Type))
            MessageBox.Show(kvp.Value.ToString())

        Next kvp
    Else
        MessageBox.Show("No results received")
    End If



    End Sub
End Class

I m getting an error NullReference Exception was handled at this line of code

Dim snmp As New SimpleSnmp
        snmp = New SimpleSnmp(host, community)

Kindly help me..!

1

1 Answer 1

0

The nullreference is because the host and community strings are blank. You are setting them to the value of a new instance of a DataGridViewTextBoxCell which is nothing.

Are you trying to set them to a particular row in the datagridview?

If so you should be usging something like:

host = DataGridView1.Rows(1).Cells(1).Value.ToString
community = DataGridView1.Rows(1).Cells(2).Value.ToString
1
  • @Madi, Your welcome. By the way, on this site (seeing you are new) if an answer is helpful you upvote it (click the up arrow), and if it solves your question, you Accept it (click the checkmark). I'm happy to help nonetheless.
    – Reafidy
    Jul 1, 2011 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.