hi there, i just want to ask for help about my codes in adding up data in my database using oledb connection. the codes is working during execution because the added item can be searched during execution only but once i close the application the data is not saved in the database.
Any help will be greatly appreciated. Thanks in advance.
Dim conn As New OleDb.OleDbConnection
Dim comm As New OleDb.OleDbCommand
Dim adap As New OleDb.OleDbDataAdapter
Public Function viewDetails(ByVal sqlRetrieve As String, ByVal sourceTable As String) As DataSet
Dim result As New DataSet
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb;Persist Security Info=True"
comm.CommandText = sqlRetrieve
conn.Open()
comm.Connection = conn
adap.SelectCommand = comm
adap.Fill(result, sourceTable)
conn.Close()
Return result
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim result As New DataSet
result = viewDetails("SELECT * FROM tblTest WHERE fname = '" & TextBox5.Text & "'", "tblTest")
TextBox1.Text = result.Tables(0).Rows(0).Item(0)
TextBox2.Text = result.Tables(0).Rows(0).Item(1)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim result As New DataSet
result = viewDetails("SELECT * FROM tblTest", "tblTest")
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb;Persist Security Info=True"
conn.Open()
Dim insertEntry As New OleDb.OleDbCommand("INSERT INTO tblTest VALUES(@fname, @lname)", conn)
insertEntry.Parameters.Add("@fname", OleDb.OleDbType.VarChar, 20, "fname")
insertEntry.Parameters.Add("@lname", OleDb.OleDbType.VarChar, 20, "lname")
Dim dt As DataTable = result.Tables("tblTest")
Dim newRow As DataRow = dt.NewRow()
newRow("fname") = TextBox3.Text
newRow("lname") = TextBox4.Text
dt.Rows.Add(newRow)
adap.InsertCommand = insertEntry
adap.Update(result, "tblTest")
result.AcceptChanges()
MsgBox("done")
conn.Close()
End Sub