DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   VB.NET (http://www.daniweb.com/forums/forum58.html)
-   -   Update database (http://www.daniweb.com/forums/thread208704.html)

tdapower Aug 3rd, 2009 2:49 am
Update database
 
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?:)

Nada_ward Aug 3rd, 2009 3:05 am
Re: Update database
 
use refresh()

babbu Aug 3rd, 2009 6:37 am
Re: Update database
 
can i plz c the code

sknake Aug 3rd, 2009 7:41 am
Re: Update database
 
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]

tdapower Aug 5th, 2009 12:03 am
Re: Update database
 

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

tdapower Aug 5th, 2009 12:05 am
Re: Update database
 
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

babbu Aug 5th, 2009 1:42 am
Re: Update database
 
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)

tdapower Aug 8th, 2009 11:29 pm
Re: Update database
 
I tryied your code, but that doesn't work well

emint Aug 9th, 2009 3:09 pm
Re: Update database
 
everything are seems fine just need to update dataset
 rows = mycmd.ExecuteNonQuery()
          con.Close()
          ds.AcceptChanges()
          da.Update(ds)


should work fine.

Vivek_1986 Aug 10th, 2009 8:28 am
@ tdapower
 
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


All times are GMT -4. The time now is 9:41 pm.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC