I am having trouble connecting my mdb database. I am trying to add some data from form into the database but as i run the application and click on add button, it shows following error:- "System.NullReferenceException: 'Object reference not set to an instance of an object.'" "cmd was nothing". This is my code :-
Public Class addForm
Dim cnn As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Private Sub addForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\krish\source\repos\DDC Library Management System\DDC Library Management System\bin\Debug\data.accdb"
cmd.Connection = cnn
cnn.Open()
cmd.CommandText = "INSERT INTO Books(BookType,BookCode,BookName,Publication,Price,Author,dt)VALUES(@bookType, @bookCode, @bookName, @publication, @price, @author, @dt)"
cmd.Parameters.AddWithValue("@bookType", bookType.SelectedItem)
cmd.Parameters.AddWithValue("@bookCode", bookCode.Text)
cmd.Parameters.AddWithValue("@bookName", bookName.Text)
cmd.Parameters.AddWithValue("@publication", txtPublication.Text)
cmd.Parameters.AddWithValue("@price", txtPrice.Text)
cmd.Parameters.AddWithValue("@author", txtAuthor.Text)
cmd.Parameters.AddWithValue("@dt", dtDate.Value)
cmd.ExecuteNonQuery()
cnn.Close()
MessageBox.Show("Data Added")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub End Class
'addForm' is the name of my current form.
This is the error it is showing as soon as i click on ADD Button