windymiller
Member
- Joined
- Feb 10, 2009
- Messages
- 5
- Programming Experience
- Beginner
I have been trying for ages to get VB to give the option to Update the database on Form Close. The database is a single table Access Database.The Binding Navigator works fine, the text boxes and labels are updated as you navigate through the records.Hopefully someone can show me where my Update code is going wrong
Imports System.Object
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.DataSet
Imports System.ComponentModel
Imports System.Collections.Generic
Public Class Form1
Dim FileDuplcateBindingNavigator As New BindingNavigator(True)
Dim FileDuplicatesBindingSource As New BindingSource()
Dim dwg As String
Dim fswMessage As String
Dim fswPath As String
Dim RowIndex As Integer = 0
Dim response As MsgBoxResult
Dim ds As New DataSet
Dim dt As New DataTable
Dim oledbDataAdapter1 As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(oledbDataAdapter1)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Book visual basic 2005 page 1056
ds = New DataSet("FileDuplicate")
dt = New DataTable("FileDuplicate")
Dim ConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Peter\Databases\Database1.mdb"
Dim Connection1 As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConnectionString)
Dim command1 As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM FileDuplicate")
command1.CommandType = CommandType.Text
Connection1.Open()
command1.Connection = Connection1
oledbDataAdapter1.SelectCommand = command1
oledbDataAdapter1.TableMappings.Add("Table", "FileDuplicate")
oledbDataAdapter1.Fill(ds)
oledbDataAdapter1.Fill(dt)
Call Populate()
'Binding navigator class printed from help
Me.FileDuplcateBindingNavigator.BindingSource = Me.FileDuplicatesBindingSource
Me.FileDuplcateBindingNavigator.Dock = DockStyle.Top
Me.Controls.Add(Me.FileDuplcateBindingNavigator)
Me.FileDuplicatesBindingSource.DataSource = ds.Tables("FileDuplicate")
Try
Me.IDTextBox.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "ID", True))
Me.Drawing_NumberTextBox.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Drawing Number", True))
Me.Created_DateLabel1.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Created Date", True))
Me.Last_AccessedLabel2.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Last Accessed", True))
Me.LocationTextBox.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Location", True))
Me.txtDescription.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Description", True))
Me.cboType.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Type", True))
Catch ex As Exception
MsgBox("Update Failed" & ex.Message.ToString)
End Try
'Book visual basic 2005 page 1057
If Connection1.State = ConnectionState.Open Then
lblstatus.Text = "Database connection is Open"
Else
lblstatus.Text = "Database connection failed"
End If
End Sub
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
If Me.ds.HasChanges Then
response = MsgBox("Changes have been made do you wish to save changes", vbYesNo, )
If response = MsgBoxResult.Yes Then
'On closing form can update database if needed
Try
Me.Validate()
Me.FileDuplicatesBindingSource.EndEdit()
builder.GetUpdateCommand()
Me.oledbDataAdapter1.Update(Me.ds)
Me.oledbDataAdapter1.Update(Me.ds.Tables("FileDuplicate"))
MsgBox("UpdateSuccesful")
Close()
Catch ex As Exception
MsgBox("Update Failed" & ex.Message)
End Try
End If
End If
Close()
End Sub
Public Sub populate()
Try
IDTextBox.Text = CStr(dt.Rows(RowIndex)("ID"))
Drawing_NumberTextBox.Text = CStr(dt.Rows(RowIndex)("drawing Number"))
Created_DateLabel1.Text = CStr(dt.Rows(RowIndex)("created date"))
Last_AccessedLabel2.Text = CStr(dt.Rows(RowIndex)("Last Accessed"))
LocationTextBox.Text = CStr(dt.Rows(RowIndex)("location"))
txtDescription.Text = CStr(dt.Rows(RowIndex)("Description"))
cboType.Text = CStr(dt.Rows(RowIndex)("Type"))
Catch ex As Exception
MsgBox("Update Failed" & ex.Message.ToString)
End Try
End Sub
End Class
Imports System.Object
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.DataSet
Imports System.ComponentModel
Imports System.Collections.Generic
Public Class Form1
Dim FileDuplcateBindingNavigator As New BindingNavigator(True)
Dim FileDuplicatesBindingSource As New BindingSource()
Dim dwg As String
Dim fswMessage As String
Dim fswPath As String
Dim RowIndex As Integer = 0
Dim response As MsgBoxResult
Dim ds As New DataSet
Dim dt As New DataTable
Dim oledbDataAdapter1 As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(oledbDataAdapter1)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Book visual basic 2005 page 1056
ds = New DataSet("FileDuplicate")
dt = New DataTable("FileDuplicate")
Dim ConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Peter\Databases\Database1.mdb"
Dim Connection1 As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConnectionString)
Dim command1 As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM FileDuplicate")
command1.CommandType = CommandType.Text
Connection1.Open()
command1.Connection = Connection1
oledbDataAdapter1.SelectCommand = command1
oledbDataAdapter1.TableMappings.Add("Table", "FileDuplicate")
oledbDataAdapter1.Fill(ds)
oledbDataAdapter1.Fill(dt)
Call Populate()
'Binding navigator class printed from help
Me.FileDuplcateBindingNavigator.BindingSource = Me.FileDuplicatesBindingSource
Me.FileDuplcateBindingNavigator.Dock = DockStyle.Top
Me.Controls.Add(Me.FileDuplcateBindingNavigator)
Me.FileDuplicatesBindingSource.DataSource = ds.Tables("FileDuplicate")
Try
Me.IDTextBox.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "ID", True))
Me.Drawing_NumberTextBox.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Drawing Number", True))
Me.Created_DateLabel1.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Created Date", True))
Me.Last_AccessedLabel2.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Last Accessed", True))
Me.LocationTextBox.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Location", True))
Me.txtDescription.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Description", True))
Me.cboType.DataBindings.Add(New Binding("Text", Me.FileDuplicatesBindingSource, "Type", True))
Catch ex As Exception
MsgBox("Update Failed" & ex.Message.ToString)
End Try
'Book visual basic 2005 page 1057
If Connection1.State = ConnectionState.Open Then
lblstatus.Text = "Database connection is Open"
Else
lblstatus.Text = "Database connection failed"
End If
End Sub
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
If Me.ds.HasChanges Then
response = MsgBox("Changes have been made do you wish to save changes", vbYesNo, )
If response = MsgBoxResult.Yes Then
'On closing form can update database if needed
Try
Me.Validate()
Me.FileDuplicatesBindingSource.EndEdit()
builder.GetUpdateCommand()
Me.oledbDataAdapter1.Update(Me.ds)
Me.oledbDataAdapter1.Update(Me.ds.Tables("FileDuplicate"))
MsgBox("UpdateSuccesful")
Close()
Catch ex As Exception
MsgBox("Update Failed" & ex.Message)
End Try
End If
End If
Close()
End Sub
Public Sub populate()
Try
IDTextBox.Text = CStr(dt.Rows(RowIndex)("ID"))
Drawing_NumberTextBox.Text = CStr(dt.Rows(RowIndex)("drawing Number"))
Created_DateLabel1.Text = CStr(dt.Rows(RowIndex)("created date"))
Last_AccessedLabel2.Text = CStr(dt.Rows(RowIndex)("Last Accessed"))
LocationTextBox.Text = CStr(dt.Rows(RowIndex)("location"))
txtDescription.Text = CStr(dt.Rows(RowIndex)("Description"))
cboType.Text = CStr(dt.Rows(RowIndex)("Type"))
Catch ex As Exception
MsgBox("Update Failed" & ex.Message.ToString)
End Try
End Sub
End Class