AccessViolationException was unhandled [VB.Net] [Emgucv] - vb.net

Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
This was the error after I set the Image to my PictureBox. Its working fine but later on the error just pop-out.
Here is my code.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim cap As New Capture() 'first line
PictureBox1.Image = cap.QueryFrame.ToBitmap 'this line AccessViolationException
Catch ex As Exception
Timer1.Stop()
MsgBox("CAMERA ERROR " & ex.Message)
End Try
End Sub
Private Sub MetroTile1_Click(sender As Object, e As EventArgs) Handles MetroTile1.Click
Try
Dim cap As New Capture() 'first line
Select Case MetroTile1.Text
Case "Capture"
Timer1.Start()
MetroTile1.Text = "OK"
Case "OK"
Timer1.Stop()
frmStudentAddEdit.picImage.Image = PictureBox1.Image
MetroTile1.Text = "Capture"
Me.Close()
End Select
Catch ex As Exception
Timer1.Stop()
End Try
End Sub
The cap.QueryFrame.ToBitmap is the AccessViolationException was unhandled error.
How can I Fix this ? What causing this error ? Please Help.

Aim for something like the following.
Capture is a member of the form (not created new each time)
oldImage is disposed of after replacing
Private mCapture As Capture
Private Sub Form12_Load(sender As Object, e As System.EventArgs) Handles Me.Load
mCapture = New Capture()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Dim oldImage = PictureBox1.Image
Dim newFrame = mCapture.QueryFrame.ToBitmap
PictureBox1.Image = newFrame.ToBitmap
If oldImage IsNot Nothing Then oldImage.Dispose()
Catch ex As Exception
Timer1.Stop()
MsgBox("CAMERA ERROR " & ex.Message)
End Try
End Sub
Private Sub MetroTile1_Click(sender As Object, e As EventArgs) Handles MetroTile1.Click
Try
Select Case MetroTile1.Text
Case "Capture"
Timer1.Start()
MetroTile1.Text = "OK"
Case "OK"
Timer1.Stop()
frmStudentAddEdit.picImage.Image = PictureBox1.Image
MetroTile1.Text = "Capture"
Me.Close()
End Select
Catch ex As Exception
Timer1.Stop()
End Try
End Sub

Related

DataGridView doesn't show anything when called from another class but works fine using form button

DataGridView doesn't show anything when called from another class but works fine using form button after form has completely loaded.
This works fine:
Private Sub GetValuesButton_Click(sender As Object, e As EventArgs) Handles GetValuesButton.Click
Try
DataGridView1.Rows.Clear()
DataGridView1.Rows.Add("test", "test2")
DataGridView1.Show()
DataGridView1.Refresh()
Application.DoEvents()
Catch ex As System.Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
This doesn't show anything on DataGridView:
Public Sub Update_values(Filepath As String)
Try
Dim form As New AttSyncForm
form.Show()
form.TopMost = True
form.Activate()
DataGridView1.Rows.Clear()
DataGridView1.Rows.Add("test", "test2")
DataGridView1.Show()
DataGridView1.Refresh()
Application.DoEvents()
Catch ex As System.Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
I think this is somehow related to form loading, any ideas?
The code in Form1. The DataGridView1.Visible property is set to True in the designer.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("col1", "Test Name")
DataGridView1.Columns.Add("col2", "Test Number")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Update_values()
End Sub
Public Sub Update_values()
DataGridView1.Rows.Add("test", "test2")
End Sub
The code in Form2.
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.Update_values()
End Sub
End Class
The code produces exactly the same results regardless if you click Button1 on Form1 or you click Button1 on Form2. I removed the .Clear so you can see the rows being added.

How to enable Live Service Controller Reporting?

Application Screenshot:
Application Code:
Dim myController As New System.ServiceProcess.ServiceController("SQL Server (SQLEXPRESS)")
Sub ed()
If txtNum.Text = "Started" Then
btnStart.Enabled = False
btnStop.Enabled = True
ElseIf txtNum.Text = "Stopped" Then
btnStop.Enabled = False
btnStart.Enabled = True
End If
End Sub
Sub Na1()
If myController.Status = ServiceProcess.ServiceControllerStatus.Running Then
txtNum.Text = "Started"
ElseIf myController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
txtNum.Text = "Stopped"
End If
End Sub
Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Na1()
ed()
End Sub
Private Sub Test_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Me.Refresh() '////////This one is not required
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
ProgressBar1.Value = 0
Try
myController.Start()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
Na1()'//////////////////////////////////////////////Edit Code here
End Try
End Sub
Private Sub txtNum_TextChanged(sender As Object, e As EventArgs) Handles txtNum.TextChanged
ed()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
ProgressBar1.Value = 0
Try
myController.Stop()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
Na1() '//////////////////////////////////////////////Edit Code here
End Try
End Sub
You need to stop SQL Service to Copy .mdf File and it is very irritating by Stopping/Starting the service manually from 'Services' so I tried to make it easier by using Vb.Net coding...
Edit the Following codes,
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
ProgressBar1.Value = 0
Try
myController.Start()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
Na1()'//////////////////////////////////////////////Edit Code here
End Try
End Sub
To
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
ProgressBar1.Value = 0
Try
myController.Start()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
myController.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)'//Add
Na1()
End Try
End Sub
And
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
ProgressBar1.Value = 0
Try
myController.Stop()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
Na1() '//////////////////////////////////////////////Edit Code here
End Try
End Sub
To
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
ProgressBar1.Value = 0
Try
myController.Stop()
Catch ex As Exception
MsgBox(ex.Message)
Finally
ProgressBar1.Value = 100
myController.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped) '//Add
Na1()
End Try
End Sub
And all is done!

Regain focus after a background task is completed

I have a datagridView(dgv) in the active tab(tabControl) that is filled with data obtained from a web service, when i start the query i create a separated thread that will assign the data to the dgv on his RunWorkerCompleted event, but then the dgv doesn't select a row when i make a click on it, in order to work properly i have to select the other tab and select again the tab containing this dgv
-the dgv still responds to the event cellContentClick
some code:
Private Sub backgroundWorker1_DoWork(sender As System.Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
Try
If UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray IsNot Nothing Then
For i As Integer = 0 To UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray.Length - 1
Dim a As sv.edu.uca.wsprb.alumnos = UCA1.getAlumnos(codCarrrera, anio, ciclo, carnet_opc).alumnosArray(i)
'here i add the rows
'TablaFAlumno.Rows.Add(...)
If i < 10 Then
BackgroundWorker1.ReportProgress(10)
End If
Next
Else
TablaFAlumno.Rows.Clear()
End If
Catch ex As System.Net.WebException
MessageBox.Show("->" + ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub backgroundWorker1_ProgressChanged(sender As System.Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If ProgressBar1.Value = 0 Then
ProgressBar1.Visible = True
End If
If ProgressBar1.Value < 100 Then
ProgressBar1.Value += 10
End If
End Sub
Private Sub backgroundWorker1_RunWorkerCompleted(sender As System.Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Error IsNot Nothing Then
MessageBox.Show("E -->" + e.Error.Message)
ElseIf e.Cancelled = True Then
ProgressBar1.Visible = False
MessageBox.Show("C -->" + e.Error.Message)
End If
ProgressBar1.Visible = False
dgvAlumnos.DataSource = TablaFAlumno
End Sub
try ad the end :
ProgressBar1.Visible = False
'TabControl name with Ficha Alumno
Me.TabControl1.SelectedIndex = 0
dgvAlumnos.DataSource = TablaFAlumno
dvgAlumnos.Select()
End Sub

open selected row in datagridview to edit in Another form

I am using vb.net designer to connect to access database .
On my Form1 I have a DataGridView And Two Button For Add And Edit
I Make Form2 To Add Data Into Database And Worked OK ..
Imake Form3 Wiht Same form2 Content
Now I need When i selcet row in DataGridView And Clic Edit Button The data of selected row show on form3 for Edit it
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SalesTableAdapter.Fill(Me.OrdersDataSet.sales)
Me.DateTimePicker1.Value = Date.Today
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
SalesBindingSource.Filter = String.Format("date = '{0}'", DateTimePicker1.Value.ToShortDateString())
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form3.Show()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub SalesDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles SalesDataGridView.CellContentClick
End Sub
End Class
You need to approach this in a modal/dialog way. You only need one form for both add and edit.
Add/Edit form
Add a parameterized constructor to the form.
Public Sub New(row As DataRowView)
Me.InitializeComponent()
'Me.ctlAge: NumericUpDown control.
'Me.ctlBirthday: DateTimePicker control.
'Me.ctlName: TextBox control.
If (row Is Nothing) Then
'Add mode, set default values:
Me.ctlAge.Value = 0
Me.ctlBirthday.Value = Date.Now
Me.ctlName.Text = String.Empty
Else
'Edit mode, set current values:
Me.ctlAge.Value = CDec(row.Item("AGE"))
Me.ctlBirthday.Value = CDate(row.Item("BIRTHDAY"))
Me.ctlName.Text = CStr(row.Item("NAME"))
End If
End Sub
You also need an accept button and a cancel button.
Friend Sub btnAcceptClicked(sender As Object, e As EventArgs) Handles btnAccept.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Friend Sub btnCancelClicked(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Main form
Add method:
Private Sub btnAddClicked(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
Using f As New AddOrEditForm(CType(Nothing, DataRowView))
If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Dim view As DataView = TryCast(Me.SalesDataGridView.DataSource, DataView)
If (view Is Nothing) Then
Throw New InvalidCastException()
End If
Dim viewRow As DataRowView = view.AddNew()
viewRow.EndEdit()
viewRow.Item("AGE") = f.ctlAge.Value
viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value
viewRow.Item("NAME") = f.ctlName.Text
viewRow.EndEdit()
End If
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Edit method:
Private Sub btnEditClicked(sender As Object, e As EventArgs) Handles btnEdit.Click
Try
Me.SalesDataGridView.EndEdit()
If (Me.SalesDataGridView.SelectedRows.Count > 0) Then
Dim gridRow As DataGridViewRow = Me.SalesDataGridView.SelectedRows(0)
Dim viewRow As DataRowView = TryCast(gridRow.DataBoundItem, DataRowView)
If (viewRow Is Nothing) Then
Throw New InvalidCastException()
End If
Using f As New AddOrEditForm(viewRow)
If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then
viewRow.BeginEdit()
Try
viewRow.Item("AGE") = f.ctlAge.Value
viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value
viewRow.Item("NAME") = f.ctlName.Text
viewRow.EndEdit()
Catch ex As Exception
viewRow.CancelEdit()
Throw ex
End Try
End If
End Using
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

vb.net chart control point onclick event

Hello i am trying to add a click event to chart points but i am getting the following Error when i click the chart "Object reference not set to an instance of an object"
here is my code
Private Sub Chart1_Click(sender As Object, e As System.EventArgs) Handles Chart1.Click
Try
Dim pointindex As Integer
If result.ChartElementType = ChartElementType.DataPoint Then
pointindex = result.PointIndex
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
result = Chart1.HitTest(e.X, e.Y)
End Sub
If the mouse cursor is above a control only the control will receive events but not the form (for workarounds see e.g. this question: Winforms : Intercepting Mouse Event on Main Form first, not on Controls).
So Form1_MouseDown will not fire and result will still be Nothing in Chart1_Click.
A workaround could look like this:
Private Sub Chart1_Click(sender As Object, e As System.EventArgs) Handles Chart1.Click
Try
Dim pointindex As Integer
Dim result As HitTestResult
result = Chart1.HitTest(Cursor.Position.X, Cursor.Position.Y)
If result.ChartElementType = ChartElementType.DataPoint Then
pointindex = result.PointIndex
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub