Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic ( VB ) (http://www.go4expert.com/forums/visual-basic/)
-   -   Add And Update Records In Vb.net 2005 Without Ussing Command (http://www.go4expert.com/forums/add-update-records-vbnet-2005-ussing-t8601/)

jatin_borah 2Feb2008 00:02

Add And Update Records In Vb.net 2005 Without Ussing Command
 
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

    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

  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

    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

  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

    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...

shabbir 3Feb2008 11:03

Re: Add And Update Records In Vb.net 2005 Without Ussing Command
 
Moved to VB forum

KiLLER 2Mar2008 20:56

Re: Add And Update Records In Vb.net 2005 Without Ussing Command
 
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. :)

pardeep3dec 14Oct2008 16:13

Re: Add And Update Records In Vb.net 2005 Without Ussing Command
 
'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



All times are GMT +5.5. The time now is 07:25.