Code is here. pl.. let me know any mistake that I have done.
Is there any other easy method to update table in Access Database?
Thanks
hmssiri
Imports System.Data.OleDb
Public Class Form4
Inherits System.Windows.Forms.Form
Dim myadapter As New OleDbDataAdapter
Dim mydataset As New DataSet
Dim olecon As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\eUnion\eUnion.mdb")
Public mybmb As BindingManagerBase
Dim myrow As DataRow
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
olecon.Open()
myadapter.SelectCommand = New OleDbCommand("select * from province", olecon)
myadapter.Fill(mydataset, "province")
mybmb = BindingContext(mydataset, "province")
Me.BindingContext(mydataset, "province").Position = 0
Showrecords()
End Sub
Sub Showrecords()
Dim oRaw As DataRow
Dim OTable As DataTable
OTable = mydataset.Tables("province")
oRaw = OTable.Rows(mybmb.Position)
TextBox1.Text = oRaw("province")
TextBox2.Text = oRaw("PDiscript")
End Sub
Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
If mydataset.HasChanges(DataRowState.Added) Then
Dim myAddDataset As New DataSet()
AddPrTable(myAddDataset)
MessageBox.Show("Added to Database Successfull")
Else
MessageBox.Show("No Record Added...")
End If
End Sub
Sub AddPrTable(ByVal myAddDataset As DataSet)
Try
Dim myrow As DataRow
Dim intRowsAffected As Integer
For Each myrow In myAddDataset.Tables("province").Rows
Dim cmdCommand As New OleDb.OleDbCommand()
insertAddParam(cmdCommand, myrow)
intRowsAffected = ExecuteStoredProcedure("spAddRecord", cmdCommand)
Next
mydataset.AcceptChanges()
Catch oEx As Exception
MessageBox.Show("The following Error occured:" & oEx.Message)
End Try
End Sub
Sub insertAddParam(ByRef cmdcommand As OleDb.OleDbCommand, ByVal myrow As DataRow)
Dim oleparm As New OleDb.OleDbParameter()
oleparm = cmdcommand.Parameters.Add("@province", OleDbType.Char, 2)
oleparm.Value = myrow("province")
oleparm = cmdcommand.Parameters.Add("@PDiscript", OleDbType.Char, 25)
oleparm.Value = myrow("PDiscript")
End Sub
Function ExecuteStoredProcedure(ByVal Strspname As String, ByVal cmdcommand As OleDbCommand) As Integer
Dim intRowsAffected As Integer
olecon.Open()
Dim cmdparms As OleDb.OleDbCommand = cmdcommand
cmdparms.Connection = olecon
cmdparms.CommandType = CommandType.StoredProcedure
cmdparms.CommandText = Strspname
intRowsAffected = cmdparms.ExecuteNonQuery
olecon.Close()
End Function
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
'Dim myrow As DataRow
myrow = mydataset.Tables("province").NewRow
myrow = ("province") = TextBox1.Text
myrow = ("pDiscript") = TextBox2.Text
mydataset.Tables("province").Rows.Add(myrow)
mybmb.Position = mybmb.Count - 1
Showrecords()
End Sub
End Class

New Topic/Question
Reply




MultiQuote







|