4 Replies - 1787 Views - Last Post: 02 December 2011 - 09:51 AM Rate Topic: -----

#1 caello  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 11

how to delete rows with database in access 2003

Posted 02 December 2011 - 12:26 AM

How to delete datagridview with database in access 2003 using button & keyboard delete
my code (VB.Net 2005)
Private Sub delete()
        Dim ds As New DataSet
        Dim da As New OleDbDataAdapter
        Dim cmd As New OleDbCommandBuilder
 

        con.Open()
        da = New OleDbDataAdapter("SELECT * FROm Part", con)
        cmd = New OleDbCommandBuilder(da)
        da.DeleteCommand = cmd.GetDeleteCommand()
        da.UpdateCommand = cmd.GetUpdateCommand()
        da.InsertCommand = cmd.GetInsertCommand()
        da.Fill(ds, "Part")
        DataGridView1.DataSource = ds.Tables("Part")
 
        Try
            Dim i As Integer = DataGridView1.SelectedRows(0).Index
            DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
            ds.Tables(0).Rows(i).Delete()
            da.Update(ds, "Part")
            MessageBox.Show("Success!")
 
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 
        con.Close()
    End Sub

when i click and delete by keyboard nothing happen n Error "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

Is This A Good Question/Topic? 0
  • +

Replies To: how to delete rows with database in access 2003

#2 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: how to delete rows with database in access 2003

Posted 02 December 2011 - 01:06 AM

You could learn it here http://www.mikesdotn...-with-MS-Access
Was This Post Helpful? 0
  • +
  • -

#3 caello  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 11

Re: how to delete rows with database in access 2003

Posted 02 December 2011 - 02:22 AM

thanks but not work in my code..
may be i newbie n i don't know using in code
can you explain code to me?..
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: how to delete rows with database in access 2003

Posted 02 December 2011 - 02:30 AM

You should probably learn the OOP basics before you try to code semi-complex programs, it helps.

Quote

thanks but not work in my code..
may be i newbie n i don't know using in code
can you explain code to me?..

Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: how to delete rows with database in access 2003

Posted 02 December 2011 - 09:51 AM

Lets break down what you did. You have your select statement, that select statement is going to the adapter, which then fills the dataset with that information i.e. taking a picture of your database. Now you need to pass a new statement preferably in another sub routine, where you delete that selected row from the database, to which you then update your dataset again. What you are attempting to do now, is update the gridview, which in essence does the very same thing the gridview can do by default, no code needed. If you are a newbie, you need to find another project, because you need to understand various other things. The rest is up to you, good luck sir.

Summary:
-establish connection
-pass delete statement
-get the information from the adapter
-update the dataset with the adapter
-update the gridview datasource

I would suggest getting the information for the dataset in another routine, so delete here, then go to another one where you can update the gridview.

This post has been edited by trevster344: 02 December 2011 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1