I am getting Object reference not set to an instance of an object error on chart creation with threads, It was running fine without threads but I have to do it with threads, please help
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
trd.Start()
End sub
Function
Private Sub ThreadTask()
Dim Data As String() = Functions.runAnalyticsReportSample(100278)
Functions.CredHolder(Session("User"), Session("Pw"))
Dim split As String() = {""}
Dim table As New DataTable()
Dim colB As DataColumn = table.Columns.Add("A", GetType(String))
Dim colD As DataColumn = table.Columns.Add("B", GetType(String))
For Each line In Data
split = line.Split(","c)
Dim row As DataRow = table.NewRow()
row.SetField(colB, split(0))
row.SetField(colD, split(1))
table.Rows.Add(row)
Next
'Chart1.Series.Add("test")
Chart1.Series("Series1").XValueMember = "A"
Chart1.Series("Series1").YValueMembers = "B"
Chart1.Series("Series1").IsValueShownAsLabel = True
Chart1.Series("Series1").IsVisibleInLegend = False
Chart1.DataSource = table
Chart1.DataBind()
End Sub
Related
The following code isn't working when exporting the datagridview data when trying to make it vertical with headers along the left side along with text beside each one. Once this is flipped the user would click on button1 to export to excel.
Imports System.Data.DataTable
Imports System.IO
Imports Microsoft.Office.Interop
Public Class Form1
Dim table As New DataTable(0)
Public checkBoxList As List(Of CheckBox)
Private ds As DataSet = Nothing
Private dt As DataTable = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ds = New DataSet()
dt = New DataTable()
ds.Tables.Add("Table")
Dim my_DataView As DataView = ds.Tables(0).DefaultView
DataGridView1.DataSource = my_DataView
table.Columns.Add("Forename", Type.GetType("System.String"))
table.Columns.Add("Surname", Type.GetType("System.String"))
table.Columns.Add("Food", Type.GetType("System.String"))
checkBoxList = New List(Of CheckBox) From {CheckBox1, CheckBox2, CheckBox3, CheckBox4}
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
Dim values As String = "" &
String.Join(" & ", checkBoxList _
.Where(Function(cb) cb.Checked).Select(Function(cb) cb.Text))
' use values for placing into your DataGridView
CheckBox1.Text = values
CheckBox2.Text = values
CheckBox3.Text = values
CheckBox4.Text = values
table.Rows.Add(TextBox1.Text, TextBox2.Text, values.ToString)
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
DataGridView1.RowTemplate.Height = 100
DataGridView1.AllowUserToAddRows = False
DataGridView1.DataSource = table
'Save to excel with headers
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
'create object of excel
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
For Each column As DataGridViewColumn In DataGridView1.Columns
.cells(1, column.Index + 1) = column.HeaderText
Next
For i = 1 To Me.DataGridView1.RowCount
.cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("Forename").Value
For j = 1 To DataGridView1.Columns.Count - 1
.cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
Next
Next
End With
ExcelApp.Visible = True
'
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Public Function FlipDataSet(ByVal my_DataSet As DataSet) As DataSet
Dim ds As New DataSet()
For Each dt As DataTable In my_DataSet.Tables
Dim table As New DataTable()
For i As Integer = 0 To dt.Rows.Count
table.Columns.Add(Convert.ToString(i))
Next
Dim r As DataRow
For k As Integer = 0 To dt.Columns.Count - 1
r = table.NewRow()
r(0) = dt.Columns(k).ToString()
For j As Integer = 1 To dt.Rows.Count
r(j) = dt.Rows(j - 1)(k)
Next
table.Rows.Add(r)
Next
ds.Tables.Add(table)
Next
Return ds
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
DataGridView1.DataSource = currentDataView
Button2.Enabled = False
End Sub
End Class
When clicking button2 it should flip the data with the following;
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
DataGridView1.DataSource = currentDataView
Button2.Enabled = False
End Sub
End Class
I've tried debugging but it i can't seem to find anything wrong? It will allow me to insert data in the textbox's whilst selecting checkbox's and when clicking button 1 to export it works fine, but it doesn't flip the data.
Please can anyone suggest how to fix this as i have a presentation on the 8th June and this data needs to automatically be flipped
Sourcecode: Download Myproject
Image of target
Answered:
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Public Class Form1
Private ds As DataSet = Nothing
Private dt As DataTable = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.AllowUserToAddRows = False
dt = New DataTable("MyTable")
dt.Columns.Add("Forename", Type.GetType("System.String"))
dt.Columns.Add("Surname", Type.GetType("System.String"))
dt.Columns.Add("Food", Type.GetType("System.String"))
ds = New DataSet
ds.Tables.Add(dt)
Dim my_DataView As DataView = ds.Tables("MyTable").DefaultView
DataGridView1.DataSource = my_DataView
End Sub
Private Sub Button_AddRowData_Click(sender As Object, e As EventArgs) Handles Button_AddRowData.Click
Dim foods As String = String.Join(" & ", CheckedListBox1.CheckedItems.Cast(Of String))
dt.Rows.Add(New Object() {TextBox_Forename.Text, TextBox_Surname.Text, foods})
End Sub
Private Sub Button_FlipAndSave_Click(sender As Object, e As EventArgs) Handles Button_FlipAndSave.Click
FlipAndSave(ds.Tables("MyTable"))
End Sub
Private Sub FlipAndSave(table As DataTable)
Dim ExcelApp As New Excel.Application
Dim WrkBk As Excel.Workbook = ExcelApp.Workbooks.Add()
Dim WrkSht As Excel.Worksheet = CType(WrkBk.Worksheets(1), Excel.Worksheet)
With WrkSht
For ci As Integer = 0 To table.Columns.Count - 1
.Cells(ci + 1, 1) = table.Columns(ci).ColumnName
Next
For ri As Integer = 0 To table.Rows.Count - 1
For ci As Integer = 0 To table.Columns.Count - 1
.Cells(ci + 1, ri + 2) = table.Rows(ri).Item(ci).ToString
Next
Next
End With
ExcelApp.Visible = True
'use this lines if you want to automatically save the WorkBook
'WrkBk.SaveAs("C:\Some Folder\My Workbook.xlsx") '(.xls) if you have an old version of Excel
'ExcelApp.Quit() 'use this line if you want to close the Excel Application
ReleaseObject(ExcelApp)
ReleaseObject(WrkBk)
ReleaseObject(WrkSht)
End Sub
Private Sub ReleaseObject(obj As Object)
Marshal.ReleaseComObject(obj)
obj = Nothing
End Sub
End Class
How can I load Charts one by one. I have made a chart maker function and calling it on page load but I need them to load asynchronously, please help
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
If Not IsPostBack Then
ChartMaker(100288, Chart1, "Series1")
ChartMaker(100289, Chart2, "Series2")
ChartMaker1(100291, Chart3, "Series3")
ChartMaker(100293, Chart4, "series4")
ChartMaker(100295, Chart5, "Series5")
ChartMaker(100278, Chart6, "Series6")
End If
End Sub
Sub ChartMaker(Rnum As Integer, chart As System.Web.UI.DataVisualization.Charting.Chart, Series As String)
Dim Data As String() = Functions.runAnalyticsReportSample(Rnum)
Functions.CredHolder(Session("User"), Session("Pw"))
Dim split As String() = {""}
Dim table As New DataTable()
Dim colB As DataColumn = table.Columns.Add("A", GetType(String))
Dim colD As DataColumn = table.Columns.Add("B", GetType(String))
For Each line In Data
split = line.Split(","c)
Dim row As DataRow = table.NewRow()
row.SetField(colB, split(0))
row.SetField(colD, split(1))
table.Rows.Add(row)
Next
chart.Series(Series).XValueMember = "A"
chart.Series(Series).YValueMembers = "B"
chart.Series(Series).IsValueShownAsLabel = True
chart.Series(Series).IsVisibleInLegend = False
chart.DataSource = table
chart.DataBind()
End Sub
I would like to remove data row from datantable in vb.net.
When I find the row based on column with primary key, it return error . I google but cannot resolve the error .
This is my code
Private Sub frmMatching_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dtFromCustomer = New DataTable()
dtToCusotmer = New DataTable()
dtTemp = New DataTable()
SetColumn()
BindFromCustomer()
BindToCustomer()
End Sub
Private Sub SetColumn()
Dim keyColumn As New DataColumn()
keyColumn.ColumnName = "CV_CODE"
keys(0) = keyColumn
dtFromCustomer.Columns.Add(keyColumn)
dtFromCustomer.Columns.Add("CV_NAME")
dtFromCustomer.PrimaryKey = keys
End Sub
Private Sub BindFromCustomer()
Dim ds As New DataSet
ds.Clear()
ds = GetDataset(sql)
Try
If ds.Tables(0).Rows.Count > 0 Then
dtFromCustomer = ds.Tables(0)
dtFromCustomer.PrimaryKey = Nothing
Dim col As DataColumn = dtFromCustomer.Columns("CV_CODE")
dtFromCustomer.AcceptChanges()
gvFromCustomer.DataSource = dtFromCustomer
End If
End Sub
Private Sub btnAddOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddOne.Click
If gvFromCustomer.SelectedRows.Count > 0 And dtFromCustomer.Rows.Count > 0 Then
Dim dr As DataRow
Dim i As Integer = 0
Try
Dim foundRow As DataRow
Dim findThese(0) As Object
findThese(0) = gvFromCustomer.SelectedRows(0).Cells("CV_CODE").Value.ToString()
foundRow = dtFromCustomer.Rows.Find(findThese)
If Not (foundRow Is Nothing) Then 'row is found
dtFromCustomer.Rows.Remove(foundRow)
Exit Sub
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
dtFromCustomer.AcceptChanges()
gvFromCustomer.DataSource = dtFromCustomer
gvToCustomer.Refresh()
End If
My error get this line
foundRow = dtFromCustomer.Rows.Find(findThese)
I have code that displays information about degrees in a datagridview. This program works but I just found out I need to display this information using arrays. Once I figured out how to display it in a data table it was really easy but I need to do it the other way. Some advice please!
Dim array As New DataTable
Dim array2 As New DataTable
Dim array3 As New DataTable
Private Sub btnQuit_Click(sender As System.Object, e As System.EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub btnDegrees_Click(sender As System.Object, e As System.EventArgs) Handles btnDegrees.Click
array.Clear()
Dim column1 As DataColumn = New DataColumn("Field of Study")
column1.DataType = System.Type.GetType("System.String")
Dim column2 As DataColumn = New DataColumn("1981")
column2.DataType = System.Type.GetType("System.String")
Dim column3 As DataColumn = New DataColumn("2006")
column3.DataType = System.Type.GetType("System.String")
array.Clear()
array.Columns.Add(column1)
array.Columns.Add(column2)
array.Columns.Add(column3)
array.Rows.Add("Business", "200,521", "318,042")
array.Rows.Add("Computer and info. science", "15,121", "47,480")
array.Rows.Add("Education", "108,074", "107,238")
array.Rows.Add("Engineering", "63,642", "67,045")
array.Rows.Add("Social sciences and history", "100,513", "161,485")
Me.dgvStudies.DataSource = array
End Sub
Private Sub btnChanges_Click(sender As System.Object, e As System.EventArgs) Handles btnChanges.Click
array2.Clear()
Dim column4 As DataColumn = New DataColumn("Field of Study")
column4.DataType = System.Type.GetType("System.String")
Dim column5 As DataColumn = New DataColumn("Change (1981-2006)")
column5.DataType = System.Type.GetType("System.String")
array2.Columns.Add(column4)
array2.Columns.Add(column5)
''Display array two containing percentage changes''
array2.Rows.Add("Computer and info. science", "214.0%")
array2.Rows.Add("Social sciences and history", "60.7%")
array2.Rows.Add("Business", "58.6%")
array2.Rows.Add("Engineering", "5.3%")
array2.Rows.Add("Education", "-0.8%")
dgvStudies.DataSource = array2
End Sub
Private Sub btnHistogram_Click(sender As System.Object, e As System.EventArgs) Handles btnHistogram.Click
array3.Clear()
Dim column6 As DataColumn = New DataColumn("Field of Study")
column6.DataType = System.Type.GetType("System.String")
Dim column7 As DataColumn = New DataColumn("")
column7.DataType = System.Type.GetType("System.String")
Dim column8 As DataColumn = New DataColumn("Degrees in 2006")
column8.DataType = System.Type.GetType("System.String")
array3.Clear()
array3.Columns.Add(column6)
array3.Columns.Add(column7)
array3.Columns.Add(column8)
array3.Rows.Add("Computer and info. science", "*****", "47,480")
array3.Rows.Add("Engineering", "*******", "67,045")
array3.Rows.Add("Education", "***********", "107,238")
array3.Rows.Add("Social sciences and history", "****************", "161,485")
array3.Rows.Add("Business", "********************************", "67,045")
dgvStudies.DataSource = array3
End Sub
End Class
If you want to use an array instead of a DataTable you also need to create an object that corresponds to you DataTable structure. This is an example of your "Degrees implementation" using arrays and a Degrees object.
Sub Main
dim arr = new Degrees() { new Degrees("Business", "200,521", "318,042"), new Degrees("Computer and info. science", "15,121", "47,480")}
Me.dgvStudies.DataSource = arr
End Sub
public class Degrees
public property FieldofStudy As string
public property Year1986 As string
public property Year2006 As string
public sub new(byval fieldofStudy As string, byval year1986 As string, byval year2006 As string)
Me.FieldofStudy = fieldofStudy
Me.Year1986 = year1986
Me.Year2006 = year2006
end sub
end class
I have a program which has a datagridview with two combobox and two textbox. The first combobox contains OIDs and the other Combobox contains SNMP Operation (Get & GetNext).
Now I want to add another ComboBox beside my OID’s Combobox which contains description for the OIDs (like 1.3.6.1.2.1.1.1.0 for sysDescr and so on), and also the value of the New ComboBox(the one with description of OIDs) changes automatically as the user changes the OID in the first combobox. Is it possible? If yes then how?
Here is the code:
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"
txt1B.Value = "115.186.118.130"
dataGridRow.Cells.Add(txt1A)
txt1A.[ReadOnly] = True
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 cbo1 As New DataGridViewComboBoxCell()
Dim txt1B As New DataGridViewTextBoxCell()
txt1B.Value = "203.81.211.117"
Dim host As String
Dim community As String
host = DataGridView1.Rows(0).Cells(1).Value.ToString
community = DataGridView1.Rows(3).Cells(1).Value.ToString
Dim txt4B As New DataGridViewTextBoxCell()
txt4B.Value = "public"
Dim snmp As New SimpleSnmp
snmp = New SimpleSnmp(DataGridView1.Rows(0).Cells(1).Value.ToString, DataGridView1.Rows(3).Cells(1).Value.ToString)
'abc.Text = txtsnmpaction.SelectedItem
result = snmp.Get(SnmpVersion.Ver1, New String() {DataGridView1.Rows(1).Cells(1).Value.ToString()})
'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'd make the SNMP Operation a read-only text field and set it programatically. Handle the DataGridView1.CellValueChanged event.
EDIT: Changing description rather than SNMP operation.
Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.ColumnIndex = 3 Then
Dim Row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
Dim Description As String = Nothing
Select Case Row.Cells(3).Value
Case "1.3.6.1.2.1.1.1.0" : Description = "sysDescr"
...
End Select
Row.Cells(5).Value = Description ' Change (5) to description column index.
End If
End Sub