[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Can not delete datagrid row from dataset

Posted on 2011-03-10
6
Medium Priority
?
411 Views
Last Modified: 2012-05-11
I have a datagrid that is bound to a dataset.  I use the datagrids, edit and delete auto buttons.  When I delete the row, code example 1 fires, then makes visible to update button which is to be used to updated the underlying database.  But when I run the update code (the 2nd example) I receive erroor that "Deleted row information can not be accessed through the row.

Please help, the update items seem to work fine, although I thought if I attached each of the sqlcommands to the correct dataapdapter function,  all I had to do was to dataadapter.update(ds.tables(0)) rather than having to iriderate through the database.  Maybe someone could elaborate on that for me.



thanks

Private Sub UpdateTable()
        'Dim myUpdateCMD As New SqlCommand
        Dim cmdEdit As New SqlCommand
        Dim cmdADD As New SqlCommand
        Dim cmdDelete As New SqlCommand
        '' Dim strUpdate As String
        Dim i As Integer = 0
        If dsCategories.HasChanges Then
            For i = 0 To dsCategories.Tables(0).Rows.Count - 1
                Dim myRow As DataRow
                If dsCategories.Tables(0).Rows(i).RowState = DataRowState.Added Then
                    myRow = dsCategories.Tables(0).Rows(i)
                    With cmdADD
                        .Connection = myConn
                        .CommandText = "TAG_InsertCategory"
                        .CommandType = CommandType.StoredProcedure
                        .Parameters.Add(New SqlParameter("@ID", myRow.Item(0)))
                        .Parameters.Add(New SqlParameter("@Long_Name", myRow.Item(1)))
                        .Parameters.Add(New SqlParameter("@Short_Name", myRow.Item(2)))
                        .Parameters.Add(New SqlParameter("@Color", myRow.Item(3)))
                        .Parameters.Add(New SqlParameter("@Active", myRow.Item(4)))
                        .Parameters.Add(New SqlParameter("@LocationID", myRow.Item(5)))
                    End With

                ElseIf dsCategories.Tables(0).Rows(i).RowState = DataRowState.Deleted Then
                    myRow = dsCategories.Tables(0).Rows(i)

                    With cmdDelete
                        .Connection = myConn
                        .CommandText = "TAG_DeleteCategory"
                        .CommandType = CommandType.Text
                        .Parameters.Add(New SqlParameter("@RowID", myRow(0)))
                    End With

                ElseIf dsCategories.Tables(0).Rows(i).RowState = DataRowState.Modified Then
                    myRow = dsCategories.Tables(0).Rows(i)
                    With cmdEdit
                        .Connection = myConn
                        .CommandText = "TAG_UpdateCategories"
                        .CommandType = CommandType.StoredProcedure
                        .Parameters.Add(New SqlParameter("@ID", myRow.Item(0)))
                        .Parameters.Add(New SqlParameter("@Long_Name", myRow.Item(1)))
                        .Parameters.Add(New SqlParameter("@Short_Name", myRow.Item(2)))
                        .Parameters.Add(New SqlParameter("@Color", myRow.Item(3)))
                        .Parameters.Add(New SqlParameter("@Active", myRow.Item(4)))
                        .Parameters.Add(New SqlParameter("@LocationID", myRow.Item(5)))
                    End With
                End If
            Next
        Else
            ' nothing to change
        End If
        Dim daAdapter As New SqlDataAdapter
        Try
            myConn.Open()
            daAdapter.UpdateCommand = cmdEdit
            daAdapter.InsertCommand = cmdADD
            daAdapter.DeleteCommand = cmdDelete
            daAdapter.Update(dsCategories.Tables(0))
            myConn.Close()
        Catch ex As Exception
            Dim myError As String
            myError = ex.Message
        End Try

    End Sub

Open in new window

Private Sub gvCategories_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvCategories.RowDeleting
        dsCategories.Tables(0).Rows(e.RowIndex).BeginEdit()
        dsCategories.Tables(0).Rows(e.RowIndex).Delete()
        dsCategories.Tables(0).Rows(e.RowIndex).EndEdit()
        'dsCategories.Tables(0).Rows(e.RowIndex).AcceptChanges()
        gvCategories.DataSource = dsCategories.Tables(0)
        gvCategories.DataBind()
        If dsCategories.HasChanges Then
            btnUpdate.Visible = True
        Else
            btnUpdate.Visible = False
        End If
    End Sub

Open in new window

0
Comment
Question by:mgmhicks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35098031
Call a refresh of the datagridview AFTER deleting the row in the datatable.
0
 

Author Comment

by:mgmhicks
ID: 35098050
Where under row_deleting or the update sub.  Looking at the row_deleting sub, looks like I removed the row from the dataset, however the problem is updating the sql server to delete that row in the update sub.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35098425
I mis-understood your question.

Now that you know that your routine to delete the data from the table works:
1. you should call the routine to delete the same data from the database before acually deleting it from the table, thereby making use of the data in the datatable BEFORE it is deleted, or:

2. keep some referencing data in a variable so that you can access this data to call your routine to update the database, e.g row number or key data

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Accepted Solution

by:
x77 earned 2000 total points
ID: 35100978
On RowDeleting event, you can cancel the Deleting action.
If yo do´nt  cancel the action (e.Cancel = true), then GridView do the delete action over datarow.

You are deletin the row, then when the Gridview do the action, then a exception occurs.
0
 

Author Comment

by:mgmhicks
ID: 35116205
x77 I have a bound grid, and I have a delete button (auto delete link) and I cant get it deleted from the underlying database.  Should I update the underlying database after each change or wait and do all the changes to the database at once like I have tried to do in the update sub I have.  Its not supposed to be this hard I am missing the boat somewhere.  If I cancel the delete, when I do the UpdateTable sub, how can I tell the underlying database which record to delete.

thanks
0
 

Author Comment

by:mgmhicks
ID: 35122412
Here is how I handle getting rid of the row, within the underlying database

Private Sub gvCategories_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvCategories.RowDeleting
        Dim mValue As Integer = dsCategories.Tables(0).Rows(e.RowIndex).Item(0)

        dsCategories.Tables(0).Rows(e.RowIndex).BeginEdit()
        dsCategories.Tables(0).Rows(e.RowIndex).Delete()
        dsCategories.Tables(0).Rows(e.RowIndex).EndEdit()
        dsCategories.Tables(0).Rows(e.RowIndex).AcceptChanges()
        Dim mystr As String
        mystr = "DELETE FROM TAG_InspectionCategories WHERE ID = " & mValue
        Dim myCMD As New SqlCommand
        Dim myDA As New SqlDataAdapter
        'myDA.DeleteCommand.CommandText = mystr

        Try
            myConn.Open()
            With myCMD
                .Connection = myConn
                .CommandText = mystr
                .CommandType = CommandType.Text

            End With
            myCMD.ExecuteNonQuery()
            myConn.Close()
        Catch ex As Exception
            Dim mtest As String
            mtest = ex.Message

        End Try
        gvCategories.DataSource = dsCategories.Tables(0)
        gvCategories.DataBind()
        If dsCategories.HasChanges Then
            btnUpdate.Visible = True
        Else
            btnUpdate.Visible = False
        End If
    End Sub
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question