Visual Basic .NET Forums    

Go Back   Visual Basic .NET Forums > VB.NET > Data Access

VB.NET Forums Newsletter Signup:
Email address:


Data Access VB.NET development for data access and back-end related areas

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-10-2008, 5:01 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Oct 2008
Age: 29
Posts: 2
Reputation: 0
Coisox is on a distinguished programming path ahead
Default Add, Update, Delete, Prev, Next - program crash

I try a simple MS Access database apps.



Each button (or function) works perfectly by themselves. Which means, if I click the ADD button and exit the application, then open the database through MS Access, the new record is perfectly added.

But if I click Add, then immediately click the Delete button, the application crash. If I click the Add, then exit application, then run the application again, then click the Delete button, works perfectly.

Here's my code:
Or you can download the project here.

Code:
Public Class Form1
    Public cnADONetConnection As New OleDb.OleDbConnection()
    Public dataAdapter As OleDb.OleDbDataAdapter
    Public commandBuilder As OleDb.OleDbCommandBuilder
    Public dataTable As New DataTable

    Dim m_rowPosition As Integer = 0

    Private Sub ShowCurrentRecord()
        If dataTable.Rows.Count = 0 Then
            TextBox1.Text = ""
            Exit Sub
        End If

        TextBox1.Text = dataTable.Rows(m_rowPosition)("nama").ToString()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cnADONetConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb"
        cnADONetConnection.Open()
        dataAdapter = New OleDb.OleDbDataAdapter("Select * From Table1", cnADONetConnection)
        commandBuilder = New OleDb.OleDbCommandBuilder(dataAdapter)
        dataAdapter.Fill(dataTable)

        ShowCurrentRecord()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If m_rowPosition > 0 Then
            m_rowPosition = m_rowPosition - 1
            Me.ShowCurrentRecord()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If m_rowPosition < (dataTable.Rows.Count - 1) Then
            m_rowPosition = m_rowPosition + 1
            Me.ShowCurrentRecord()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If dataTable.Rows.Count <> 0 Then
            dataTable.Rows(m_rowPosition)("nama") = TextBox1.Text
            dataAdapter.Update(dataTable)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim drNewRow As DataRow = dataTable.NewRow()
        drNewRow("nama") = TextBox1.Text
        dataTable.Rows.Add(drNewRow)
        dataAdapter.Update(dataTable)
        m_rowPosition = dataTable.Rows.Count - 1
        Me.ShowCurrentRecord()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If dataTable.Rows.Count <> 0 Then
            dataTable.Rows(m_rowPosition).Delete()
            dataAdapter.Update(dataTable)
            m_rowPosition = 0
            Me.ShowCurrentRecord()
        End If
    End Sub
End Class
Reply With Quote
  #2 (permalink)  
Old 10-13-2008, 9:23 AM
jmcilhinney's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2004
Location: Sydney, Australia
Age: 39
Posts: 4,356
Reputation: 254
jmcilhinney I'm the one to askjmcilhinney I'm the one to askjmcilhinney I'm the one to askjmcilhinney I'm the one to askjmcilhinney I'm the one to askjmcilhinney I'm the one to ask
Default

If the application crashes then you'd be given an error message. Please give it to us.
__________________
Essential: Multiple Forms
101 Samples: 2002 | 2003 | 2005Free Components: WFC | XPCC | ElementsEx | VBPP | ADO.NET/MySQL | VisualStyles | NPlot | SDFTutorials: Home & Learn | Start VB.NET | Learn VB.NETFavourites: MSDN | WinForms.NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz
Reply With Quote
  #3 (permalink)  
Old 10-13-2008, 10:06 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Oct 2008
Age: 35
Posts: 8
Reputation: 0
avile22 is on a distinguished programming path ahead
Default

Its the famous concurrency violation. This drove me nuts awhile back and I'll be back from work by noon to dig up my old code.

I believe its something to do with the Primary Key in MS Access
Reply With Quote
  #4 (permalink)  
Old 10-13-2008, 2:40 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Oct 2008
Age: 29
Posts: 2
Reputation: 0
Coisox is on a distinguished programming path ahead
Default

Here's the error message.



Currently, what I do to solve this problem is:

dataAdapter.Update(dataTable)
dataTable.Clear()
redo the SQL query
redo commandBuilder
dataAdapter.Fill(dataTable)
Reply With Quote
  #5 (permalink)  
Old 10-20-2008, 12:13 PM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 64
Posts: 5,132
Reputation: 529
cjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalist
Default

For info on how you ought to be doing your data access, read the DW2 link in my signature (and keep an eye out for pages that are .net 3.5 enhanced)

Start with the section "Creating a Simple Data App"
__________________
"it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -4. The time now is 9:29 PM.




Click to advertise here

Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
For advertising opportunities click here.