borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Make control in VB.Net became Xp style)Next Topic (ListBox Double Click) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Cannot Add Record to Database
Poster Message
ben
Level: Protégé

Registered: 12-05-2006
Posts: 6

icon 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

25-05-2006 at 04:46 PM
View Profile Send Email to User Show All Posts | Quote Reply
tanmoy
Level: Whizz Kid

Registered: 17-03-2006
Posts: 18
icon Re: Cannot Add Record to Database

Dim cm As New OleDb.OleDbCommand()
                   Dim s As String
             s = "insert into backup1 values('o005','b005',500,'2/5/2005')"
             cm = New OleDbCommand(s, con)
            cm.ExecuteNonQuery()
        Next
backup1 is table name
con is conection string
i think ur prob will be solved.
remember one thing u have to give the data as per database field type.
      

[Edited by tanmoy on 26-05-2006 at 07:18 AM GMT]

____________________________
ambarish sadhu

26-05-2006 at 07:17 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Cannot Add Record to Database
Previous Topic (Make control in VB.Net became Xp style)Next Topic (ListBox Double Click) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder