I connected a access database by usin codes, But when i add new data the data doesn't display in same time. it need to restart the programme. can I solve this?:)

Welcome to DaniWeb tdapower! Please post the relevant code for your application on this thread so we can see where the problem is. when you post code be sure to use code tags:

[code=vb.net] ...code here

[/code]

Imports System.Data.OleDb
Public Class frmLecAdd
    Dim con As New OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String


    Dim rowNo As Integer



    Private Sub pbUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbUpdate.Click
        


        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim mycmd As New OleDb.OleDbCommand
        Dim rows As Integer


        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = database.mdb;"
        con.Open()

        sql = "SELECT * FROM lec_info"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "myDB")



        mycmd.CommandText = "INSERT INTO lec_info(lecturer_name,staff_no,course_name) VALUES('" & txtLname.Text & "','" & txtStaffNo.Text & "','" & cboCourses.Text & "')"
        mycmd.Connection = con
     
        rows = mycmd.ExecuteNonQuery()
        con.Close()
          ds.AcceptChanges()

    End Sub

This is my code is
when i add new record using this i have to restart my programme to see the new data.
pls help me...

Imports System.Data.OleDb
Public Class frmLecAdd
    Dim con As New OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String


    Dim rowNo As Integer



    Private Sub pbUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbUpdate.Click
        


        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim mycmd As New OleDb.OleDbCommand
        Dim rows As Integer


        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = database.mdb;"
        con.Open()

        sql = "SELECT * FROM lec_info"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "myDB")



        mycmd.CommandText = "INSERT INTO lec_info(lecturer_name,staff_no,course_name) VALUES('" & txtLname.Text & "','" & txtStaffNo.Text & "','" & cboCourses.Text & "')"
        mycmd.Connection = con
     
        rows = mycmd.ExecuteNonQuery()
        con.Close()
          ds.AcceptChanges()

    End Sub

u have to update the data table

da.update(datatable)

u have used data adapter to retrieve data thn why dont u use it to insert as well

Dim drNewRow As DataRow = DataTable.NewRow()
        drNewRow("lecturer_name") = txtLname.Text
        drNewRow("staff_no") = txtStaffNo.Text
        drNewRow("course_name") = cboCourses.Text

DataTable.Rows.Add(drNewRow)
da.Update(DataTable)

everything are seems fine just need to update dataset

rows = mycmd.ExecuteNonQuery()
           con.Close()
          ds.AcceptChanges()
          da.Update(ds)

should work fine.

Tryout this code(It's bit different and i't tested):

Imports System.data
Imports System.Data.OleDb
Public Class frmLectureRecords
    Dim con As OleDbConnection
    Dim cmd As OleDbCommand()
    Dim da As OleDb.OleDbDataAdapter
    Dim ds As New DataSet()
    Dim Sql As String

    Private Sub frmLectureRecords_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\college.mdb;Persist Security Info=False")
        con.Open()
        Sql = "SELECT * FROM lec_info"
        da = New OleDb.OleDbDataAdapter(Sql, con)
        da.Fill(ds, "lec_info")
        con.Close()
        DataGridView1.ClearSelection()
        ' Set the DataGridView properties to bind it to our data...
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "lec_info"
        DataGridView1.Columns(0).HeaderText = "Lecturer Name"
        DataGridView1.Columns(1).HeaderText = "Staff No"
        DataGridView1.Columns(2).HeaderText = "Course"
    End Sub
End Class
This article has been dead for over six months. Start a new discussion instead.