Click to See Complete Forum and Search --> : Help: Access-VB.Net Project


BostonApplet02
November 18th, 2004, 06:31 PM
How do you add a record to an access database from a vb.net program?? Where do you put the code....ex. a button to add a record using an input box??

aslamshaikh04
February 1st, 2007, 05:38 PM
How do you add a record to an access database from a vb.net program?? Where do you put the code....ex. a button to add a record using an input box??

am having the same problem, it just doesnt seem to add the data into the table. am not sure if it has sumthing to do with autoincrement idsEmoyeeId?Can someone please help us.

Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
If inc <> -1 Then
Try
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow

dsNewRow = ds.Tables("tblEmployee").NewRow()

'dsNewRow.Item("idsEmployeeId") = txtEmployeeId.Text
dsNewRow.Item("txtFirstName") = txtFirstName.Text
dsNewRow.Item("txtLastName") = txtLastName.Text
dsNewRow.Item("txtJobTitle") = txtJobTitle.Text
dsNewRow.Item("Password") = txtPassword.Text

ds.Tables("tblEmployee").Rows.Add(dsNewRow)

da.Update(ds, "tblEmployee")



MsgBox("New Record added to the Database")
Catch ex As Exception
MsgBox(ex.Message)
End Try
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True

End If
End Sub
End Class

aniskhan
February 1st, 2007, 09:08 PM
[VB2005] Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\attendence.mdb")

Dim sSql As String = "INSERT INTO StudentClone(name, sessno) VALUES (@name, @sessno)"

Using cmd As New OleDb.OleDbCommand(sSql, conn)
cmd.Parameters.AddWithValue("@name", TextBox1.Text)
cmd.Parameters.AddWithValue("@sessno", ComboBox1.Text)

conn.Open()
cmd.ExecuteNonQuery()
conn.Close()

End Using[VB.net 2003]
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\attendence.mdb")

Dim sSql As String = "INSERT INTO StudentClone(name, sessno) VALUES (@name, @sessno)"

Dim cmd As New OleDb.OleDbCommand(sSql, conn)

cmd.Parameters.Add(New OleDb.OleDbParameter("@name", TextBox1.Text))
cmd.Parameters.Add(New OleDb.OleDbParameter("@sessno", ComboBox1.Text))

conn.Open()
cmd.ExecuteNonQuery()
conn.Close()