ben Level: Protégé
 Registered: 12-05-2006 Posts: 6
|
Cannot Add Record to Database
I used this code to add a record into an access database.
In my form design, there are 4 textboxes. I have 4 fields, namely: Name, Block, Floor, Hp. and i have a button (Add_Btn)
On the click of the button, the data in the textboxes are loaded/stored into the database, db1.mdb
I am using the Microsoft JET OLE connection, not SQL
The code below is the one i used.
My problem is the statement starting from "Public Class Form1" onwards doesn't seem to be called when i run the program. Can anyone kindly help me check wads wrong with my code?
If you want the actual code can pass me your email address and i shall send it to you. Thanks a lot.....urgent.
Imports System.Windows.Forms
Imports System.Data.oledb
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Add_Btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'Try
Me.OleDbDataAdapter1.InsertCommand.CommandText = "INSERT INTO db1 (Name, Block, Floor, Hp)" + "VALUES ('" + Me.Name_textbox.Text + "','" + Me.Block_textbox.Text + "' , " + Me.Floor_textbox.Text + ",'" + Me.Hp_textbox.Text + "')"
Me.OleDbConnection1.Open()
Me.OleDbDataAdapter1.InsertCommand.Connection = OleDbConnection1
Me.OleDbDataAdapter1.InsertCommand.ExecuteNonQuery()
Me.OleDbConnection1.Close()
MessageBox.Show("Record Added")
'Me.Name_textbox.Text = ""
'Me.Block_textbox.Text = ""
' Me.Floor_textbox.Text = ""
'Me.Hp_textbox.Text = ""
'Catch exp As System.Data.OleDb.OleDbException
Me.OleDbConnection1.Close()
'MessageBox.Show(exp.ToString)
'End Try
End Sub
End Class
|