Object reference not set to an instance of an object.

  • Thread starter Thread starter christing
  • Start date Start date
C

christing

Guest
I have 2 datagridview below code is my second datagridview update data by i face error message on this code (Object reference not set to an instance of an object.) i have try solve many time but still face same error message.

Private Sub RefreshData1()
Dim cn3 As New OleDb.OleDbConnection
cn3.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=mydataX.mdb;"
If Not cn3.State = ConnectionState.Open Then
'open connection
cn3.Open()
End If

Dim da As New OleDb.OleDbDataAdapter("SELECT cid, checkercid as [ID], " & _
"CheckerName as [CheckerName], ShipQuantity as [Quantity], Date as [Date], CompanyName as [CompanyName], CheckerDetail as [CheckerDetail]" & _
" FROM stock ORDER BY cid", cn3)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
Me.DataGridView1.DataSource = dt
Me.DataGridView1.Columns("checkercid").Visible = True
'close connection
cn3.Close()
End Sub


Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
Dim cn3 As New OleDb.OleDbConnection
cn3.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=mydataX.mdb;"
Dim cmd As New OleDb.OleDbCommand


cmd.CommandText = "UPDATE stock " & _
" SET" & _
", [CheckerDetail]='" & Me.txtdetail1.Text & "'" & _
" WHERE [checkercid]=" & Me.txtbarcode.Tag
MsgBox("Update Data Successful", MsgBoxStyle.OkOnly, "Message")

cmd.ExecuteNonQuery()

cn3.Close()
'refresh data in list
RefreshData1()
End Sub

Private Sub Edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Edit.Click
Dim cn3 As New OleDb.OleDbConnection
cn3.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=mydataX.mdb;"

LoginForm.boolresetPassword = True
LoginForm.ShowDialog()
Try

If Me.DataGridView1.Rows.Count > 0 Then
If Me.DataGridView1.SelectedRows.Count > 0 Then
Dim intcid1 As Integer = Me.DataGridView1.SelectedRows(0).Cells("ID").Value
'open connection
If Not cn3.State = ConnectionState.Open Then
cn3.Open()
End If
'get data into datatable
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM stock " & _
" WHERE cid=" & intcid1, cn3)


Dim dt As New DataTable
da.Fill(dt)
Me.txtbarcode.Text = intcid1
Me.txtdetail1.Text = dt.Rows(0).Item("CheckerDetail")

Me.txtbarcode.Tag = intcid1
'change button add to update
Me.save.Text = "Update"
'disable button edit
Me.Edit.Enabled = True
End If
End If

Catch ex As Exception
End Try

cn3.Close()
'refresh data in list
RefreshData1()
End Sub

Continue reading...
 
Back
Top