Question Error in add, update, delete

chandrajit85

New member
Joined
May 8, 2011
Messages
2
Programming Experience
1-3
My code is as follows but in runtime throws an error "Object reference not set to an instance of the object" when trying to update, delete and/or insert--pls help


My DB is in Access


Imports System.Data.OleDb
Imports System.Xml
PublicClass Employee
Dim con As OleDbConnection
Dim cmd As OleDbCommandBuilder
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim cd As OleDbCommand
Public Sub Employee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loaddata()
Me.BindingSource1.DataSource = dt
Me.ComboBox1.DisplayMember = "InmateID"
Me.ComboBox1.DataSource = Me.BindingSource1
Me.InmateNametxt.DataBindings.Add("Text", Me.BindingSource1, "InmateName")
Me.Designation.DataBindings.Add("Text", Me.BindingSource1, "Designation")
Me.ContactNoTxt.DataBindings.Add("Text", Me.BindingSource1, "ContactNo")
Me.OfficeAddresstxt.DataBindings.Add("Text", Me.BindingSource1, "OfficeAddress")
Me.ResidentialAddresstxt.DataBindings.Add("Text", Me.BindingSource1, "ResidentialAddress")
Me.Agetxt.DataBindings.Add("Text", Me.BindingSource1, "Age")
Me.Gender.DataBindings.Add("Text", Me.BindingSource1, "Gender")
Me.ServiceType.DataBindings.Add("Text", Me.BindingSource1, "ServiceType")
Me.InmateIDtxt.DataBindings.Add("Text", Me.BindingSource1, "InmateID")
Me.RoomNotxt.DataBindings.Add("Text", Me.BindingSource1, "RoomNo")
ComboBox1.ResetText()
End Sub
Public Sub loaddata()
Try
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\hp\Desktop\Project\Project.accdb;")
con.Open()
Dim str As String = "Select * from Employee order by InmateID"
da = New OleDbDataAdapter(str, con)
cmd = New OleDbCommandBuilder(da)
ds = New DataSet
da.Fill(ds, "Employee")
dt = ds.Tables("Employee")
DataGridView1.DataSource = dt
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub InsertBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertBtn.Click
Try
cd = New OleDbCommand("Insert Into Employee Values(' " + InmateIDtxt.Text + " ',' " + InmateNametxt.Text + " '," + RoomNotxt.Text + " ," + ContactNoTxt.Text + ", ' " + OfficeAddresstxt.Text + " ',' " + ResidentialAddresstxt.Text + " ', ' " + Gender.SelectedItem.ToString + " ', " + Agetxt.Text + ", ' " + Designation.SelectedItem.ToString + " ', ' " + ServiceType.SelectedItem.ToString + " ')", con)
con.Open()
Dim n As Integer
n = cd.ExecuteNonQuery
If n > 0 Then
MsgBox("Record Inserted")
loaddata()
Else
MsgBox("Insertion Failed")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub UpdateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateBtn.Click
Try
cd = New OleDbCommand("Update Employee Set InmateName=' " + InmateNametxt.Text + " ', RoomNo=" + RoomNotxt.Text + ", ContactNo=" + ContactNoTxt.Text + ", OfficeAddress=' " + OfficeAddresstxt.Text + " ', ResidentialAddress=' " + ResidentialAddresstxt.Text + " ', Gender=' " + Gender.SelectedItem.ToString + " ', Age=" + Agetxt.Text + ", Designation=' " + Designation.SelectedItem.ToString + " ', ServiceType =' " + ServiceType.SelectedItem.ToString + " ' where InmateID= " + InmateIDtxt.Text, con)
con.Open()
Dim n As Integer
n = cd.ExecuteNonQuery
If n > 0 Then
MsgBox("Record Updated")
loaddata()
Else
MsgBox("Updation Failed")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub DeleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBtn.Click
Try
cd = New OleDbCommand("Delete * From Employee Where InmateID=" + InmateIDtxt.Text, con)
con.Open()
Dim n As Integer
n = cd.ExecuteNonQuery()
If (n > 0) Then
MessageBox.Show("Record Deleted")
loaddata()
Else
MessageBox.Show("Deletion Failed")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ClearBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearBtn.Click
InmateIDtxt.Text = " "
RoomNotxt.Text = " "
InmateNametxt.Text = " "
ContactNoTxt.Text = ""
OfficeAddresstxt.Text = " "
ResidentialAddresstxt.Text = " "
Gender.Text = " "
Agetxt.Text = " "
Designation.Text = " "
ServiceType.Text = " "
con.Close()
End Sub
EndClass
 

ss7thirty

Well-known member
Joined
Jun 14, 2005
Messages
455
Location
New Jersey, US
Programming Experience
5-10
Which line is the error occuring on?

Imports System.Data.OleDb
Imports System.Xml
PublicClass Employee
Dim con As OleDbConnection
Dim cmd As OleDbCommandBuilder
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim cd As OleDbCommand
Public Sub Employee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loaddata()
Me.BindingSource1.DataSource = dt
Me.ComboBox1.DisplayMember = "InmateID"
Me.ComboBox1.DataSource = Me.BindingSource1
Me.InmateNametxt.DataBindings.Add("Text", Me.BindingSource1, "InmateName")
Me.Designation.DataBindings.Add("Text", Me.BindingSource1, "Designation")
Me.ContactNoTxt.DataBindings.Add("Text", Me.BindingSource1, "ContactNo")
Me.OfficeAddresstxt.DataBindings.Add("Text", Me.BindingSource1, "OfficeAddress")
Me.ResidentialAddresstxt.DataBindings.Add("Text", Me.BindingSource1, "ResidentialAddress")
Me.Agetxt.DataBindings.Add("Text", Me.BindingSource1, "Age")
Me.Gender.DataBindings.Add("Text", Me.BindingSource1, "Gender")
Me.ServiceType.DataBindings.Add("Text", Me.BindingSource1, "ServiceType")
Me.InmateIDtxt.DataBindings.Add("Text", Me.BindingSource1, "InmateID")
Me.RoomNotxt.DataBindings.Add("Text", Me.BindingSource1, "RoomNo")
ComboBox1.ResetText()
End Sub
Public Sub loaddata()
Try
con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\hp\Desktop\Project\Project.accdb;")
con.Open()
Dim str As String = "Select * from Employee order by InmateID"
da = New OleDbDataAdapter(str, con)
cmd = New OleDbCommandBuilder(da)
ds = New DataSet
da.Fill(ds, "Employee")
dt = ds.Tables("Employee")
DataGridView1.DataSource = dt
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub InsertBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertBtn.Click
Try
cd = New OleDbCommand("Insert Into Employee Values(' " + InmateIDtxt.Text + " ',' " + InmateNametxt.Text + " '," + RoomNotxt.Text + " ," + ContactNoTxt.Text + ", ' " + OfficeAddresstxt.Text + " ',' " + ResidentialAddresstxt.Text + " ', ' " + Gender.SelectedItem.ToString + " ', " + Agetxt.Text + ", ' " + Designation.SelectedItem.ToString + " ', ' " + ServiceType.SelectedItem.ToString + " ')", con)
con.Open()
Dim n As Integer
n = cd.ExecuteNonQuery
If n > 0 Then
MsgBox("Record Inserted")
loaddata()
Else
MsgBox("Insertion Failed")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub UpdateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateBtn.Click
Try
cd = New OleDbCommand("Update Employee Set InmateName=' " + InmateNametxt.Text + " ', RoomNo=" + RoomNotxt.Text + ", ContactNo=" + ContactNoTxt.Text + ", OfficeAddress=' " + OfficeAddresstxt.Text + " ', ResidentialAddress=' " + ResidentialAddresstxt.Text + " ', Gender=' " + Gender.SelectedItem.ToString + " ', Age=" + Agetxt.Text + ", Designation=' " + Designation.SelectedItem.ToString + " ', ServiceType =' " + ServiceType.SelectedItem.ToString + " ' where InmateID= " + InmateIDtxt.Text, con)
con.Open()
Dim n As Integer
n = cd.ExecuteNonQuery
If n > 0 Then
MsgBox("Record Updated")
loaddata()
Else
MsgBox("Updation Failed")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub DeleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBtn.Click
Try
cd = New OleDbCommand("Delete * From Employee Where InmateID=" + InmateIDtxt.Text, con)
con.Open()
Dim n As Integer
n = cd.ExecuteNonQuery()
If (n > 0) Then
MessageBox.Show("Record Deleted")
loaddata()
Else
MessageBox.Show("Deletion Failed")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ClearBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearBtn.Click
InmateIDtxt.Text = " "
RoomNotxt.Text = " "
InmateNametxt.Text = " "
ContactNoTxt.Text = ""
OfficeAddresstxt.Text = " "
ResidentialAddresstxt.Text = " "
Gender.Text = " "
Agetxt.Text = " "
Designation.Text = " "
ServiceType.Text = " "
con.Close()
End Sub
EndClass
 

ss7thirty

Well-known member
Joined
Jun 14, 2005
Messages
455
Location
New Jersey, US
Programming Experience
5-10
My gut tells me line 62 is where your problem is because there you are referencing SelectedItem.ToString() and if nothing is selected you will get the above mentioned exception.
 
Top Bottom