Results 1 to 6 of 6

Thread: [RESOLVED] Dataset Problem - Deleting Row

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Resolved [RESOLVED] Dataset Problem - Deleting Row

    I am having problems deleting a row from my database. Here is my code:

    Code:
    Public Sub DeleteList(ByVal Text As String, ByVal Form As Form)
    
            If Text = "D" Then
    
                'Clear Dataset'
    
                Database.DataSet.Clear()
    
    
                'Store sql query in string variable for use with database connection sub'
    
                Dim sql As String = "SELECT * FROM " & DeleteListForm.WindscreenCompany
    
    
                'Call database connection sub and pass along above sql query'
    
                Call Database.DatabaseConnection(sql)
    
    
                'Call rowcount sub from database module to get tables rowcount'
    
                Dim RowCount As Integer = Database.RowCount
    
    
                'Loop through dataset table to match selected list on delete list form and when found delete record from dataset'
    
                Dim dbCommandBuilder As New OleDb.OleDbCommandBuilder(Database.DataAdapter)
    
    
                If DeleteListForm.IsAutowindscreens = True Then
    
                    For i As Integer = 0 To RowCount - 1
    
                        If Database.DataSet.Tables("Windscreens").Rows(i).Item(0) = DeleteListForm.ListDate And Database.DataSet.Tables("Windscreens").Rows(i).Item(1) = DeleteListForm.Broker Then
    
                            Database.DataSet.Tables("Windscreens").Rows(i).Delete()
    
    
                            'Update database'
    
                            Database.DataAdapter.Update(Database.DataSet, "Windscreens")
    
                        End If
    
                    Next i
    
                ElseIf DeleteListForm.IsAutowindscreens = False Then
    
                    For i As Integer = 0 To RowCount - 1
    
                        If Database.DataSet.Tables("Windscreens").Rows(i).Item(0) = DeleteListForm.ListDate Then
    
                            Database.DataSet.Tables("Windscreens").Rows(i).Delete()
    
    
                            'Update database'
    
                            Database.DataAdapter.Update(Database.DataSet, "Windscreens")
    
                        End If
    
                    Next i
    
                End If
    
    
                'Display messagebox confirming that list has been deleted'
    
                MessageBox.Show("List has been deleted", "Delete List", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Here is the database module that i am calling in the code:

    Code:
    Module Database
    
        'Declare varibales for database connection, connection string builder, dataset, database adapter and sql command'
    
        Public dbConnection As New OleDb.OleDbConnection
        Public dbConnectionStringBuilder As New OleDb.OleDbConnectionStringBuilder
        Public DataSet As New DataSet
        Public DataAdapter As OleDb.OleDbDataAdapter
        Public sql As String
    
        Public Sub DatabaseConnection(ByVal sql As String)
    
            'Provide data source to database connection builders connection string'
    
            dbConnectionStringBuilder.ConnectionString = "Data Source = " & LoginForm.Filename
    
    
            'Add database password and provider to connectionbuilder and then load into dbconnectionstring'
    
            dbConnectionStringBuilder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
            dbConnectionStringBuilder.Add("Jet OLEDB:Database Password", "A5B7C5D8E4")
            dbConnection.ConnectionString = dbConnectionStringBuilder.ConnectionString
    
    
            'Open database connection, use sql query to obtain required data and fill dataset'
    
            dbConnection.Open()
    
            DataAdapter = New OleDb.OleDbDataAdapter(sql, dbConnection)
    
            dbConnection.Close()
    
            DataAdapter.Fill(DataSet, "Windscreens")
    
        End Sub
    
        Public Function RowCount() As Integer
    
            'Get rowcount of data set and pass to RowCount functions returned value'
    
            RowCount = DataSet.Tables("Windscreens").Rows.Count
    
        End Function
    
    End Module
    The problem is i am getting my message box displayed confirming that the record has been deleted but when i check the database it is still there so for some reason it is not updating properly. Any idea why this may be because this usually works all the time for me?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Dataset Problem - Deleting Row

    Put a breakpoint on your delete statements... make sure you are actually executing them....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Dataset Problem - Deleting Row

    Ye i have tried this, for some reason they are not being executed but ive checked my variables and they do match the data in the database so it should be executing. Think im gna have to recode it and try again, im really stumped on this one.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Dataset Problem - Deleting Row

    Then something isn't rightwith the logic.

    I'd start by putting a breakpoint on this line:
    If Text = "D" Then

    And stepping through each line, chekcing values and variables, and making sure that you arte getting what you expect, when you expect it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Dataset Problem - Deleting Row

    Ok problem now solved it wasn't executing correctly because my variables were in the wrong palce and were blank when my module was being called. Then it started throwing an exception which was because i didnt have a primary key on my table for some reason. Is it always a good idea to have a primary key in your table?

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Dataset Problem - Deleting Row

    Generally speaking, yes... that's what tells the database how to uniquely identify each row in the table.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width