Add And Update Records In Vb.net 2005 Without Ussing Command

Discussion in 'Visual Basic ( VB )' started by jatin_borah, Feb 1, 2008.

  1. jatin_borah

    jatin_borah New Member

    Joined:
    Feb 1, 2008
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    hello im jatin ur new frnd and i want to gain some knowledge of programming in VB.Net and oracle certification course. I have completed my PGDCA, A Level, and noe im doing CCNA fron HCL.
    Now i want to solve one problem in VB.net if anyone knows the solution of this then plz reply...
    Q. Iam creatin one project in VB.Net 2005 with ms Access as Database.
    the codes are:

    ==========================================================

    Code:
    Imports System.Data.OleDb
    Public Class Form1
        Dim da As OleDbDataAdapter
        Dim ds As New DataSet
        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =G:\jatinz project\std.mdb;")
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =D:\database pro\cmc.mdb;")
            da = New OleDbDataAdapter("Select * from student", con)
            'Dim ds As New DataSet
            con.Open()
            da.Fill(ds, "student")
            txtname.DataBindings.Add("text", ds, "student.name")
            txtroll.DataBindings.Add("text", ds, "student.roll")
            txtcourse.DataBindings.Add("text", ds, "student.course")
        End Sub
    
        Private Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click
            BindingContext(ds, "student").Position = BindingContext(ds, "student").Position - 1
        End Sub
    
        [B]Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            BindingContext(ds, "student").AddNew()
            MessageBox.Show("Successfully Inserted")
        End Sub[/B]
       [B] Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
            BindingContext(ds, "student").RemoveAt(BindingContext(ds, "student").Position)
        End Sub[/B]
        Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
            BindingContext(ds, "student").Position = BindingContext(ds, "student").Position + 1
        End Sub
    
       [B] Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
            Me.BindingContext(ds, "student").EndCurrentEdit()
            da.Update(ds)
        End Sub[/B]
        Private Sub btnfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfirst.Click
            BindingContext(ds, "student").Position = 0
        End Sub
    
        Private Sub btnlast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlast.Click
            BindingContext(ds, "student").Position = BindingContext(ds, "student").Count - 1
        End Sub
    
        Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
            txtname.Clear()
            txtroll.Clear()
            txtcourse.Clear()
        End Sub
    End Class
    but the code of btnadd, btndelete,btnupdate is not work. How can i input the record to the table by using this command.
    (without using or creating command)
    plz reply as soon as possible...
     
    Last edited by a moderator: Feb 3, 2008
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    Moved to VB forum
     
  3. KiLLER

    KiLLER New Member

    Joined:
    Mar 2, 2008
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    What you're doing there is, you're adding something into database but not updating it. It's like you wrote the code for program and then you didn't save it!!

    So you need to update your database table. So every time you add or delete something from table update it by typing
    Code:
    da.Update(ds)
    It should work now. :)
     
  4. pardeep3dec

    pardeep3dec New Member

    Joined:
    Aug 25, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Developer
    Location:
    Delhi
    'add three command button named cmdupdate,cmdadd and cmddelete
    'and two text box named txt1 and txt2
    'and MS Access file with table name tblContacts with two field name and department



    Code:
    Imports System.Data
    
    Public Class Form1
        Dim ds As New DataSet
        Dim da As New OleDb.OleDbDataAdapter
        Dim sql As String 
    
    Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            ds.Tables("AddressBook").Rows(Val(IDTextBox.Text)).Item(1) = txt1.Text
            ds.Tables("AddressBook").Rows(Val(IDTextBox.Text)).Item(2) = txt2.Text
            da.Update(ds, "AddressBook")
            MsgBox("hi", MsgBoxStyle.MsgBoxHelp, "hello")
    
    
        End Sub
    
        Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow
            dsNewRow = ds.Tables("AddressBook").NewRow()
            dsNewRow.Item("FirstName") = txt1.Text
            dsNewRow.Item("Surname") = txt2.Text
            ds.Tables("AddressBook").Rows.Add(dsNewRow)
            da.Update(ds, "AddressBook")
            MsgBox("New Record added to the Database")
        End Sub
    
        Private Sub cmddelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddelete.Click
            Dim cb As New OleDb.OleDbCommandBuilder(da)
    
            If (MessageBox.Show("r u sure u want to delete", "Warning", MessageBoxButtons.OKCancel) = vbOK) Then
                ds.Tables("AddressBook").Rows(Val(IDTextBox.Text)).Delete()
            Else
    
    
            End If
            da.Update(ds, "AddressBook")
        End Sub
    
    End Class
     
    Last edited by a moderator: Oct 14, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice