Results 1 to 11 of 11

Thread: [RESOLVED] Delete from datagrid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    210

    Resolved [RESOLVED] Delete from datagrid

    I am currently trying to delete a row from the datagrid, so far so good until I tried to "acceptchanges" and keep on getting this error message:
    "Update requires the DeleteCommand to have a Connection object. The Connection property of the DeleteCommand has not been initialized"

    This is my code:
    Try
    Dim dt_BussLine As DataTable = Me.dg_BusLine.DataSource
    dt_BussLine.Rows(dg_BusLine.CurrentRowIndex).Delete()
    dadaptBusLine.Update(DsBusLine1)
    Me.DsBusLine1.AcceptChanges()
    Catch eexception As Exception
    MsgBox(eexception.Message)
    End Try

    I don't understand what it means since I am currently connected to the table. Please Help!

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Delete from datagrid

    you didn't pass a connection to your delete command.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    210

    Re: Delete from datagrid

    sorry for sounding stupid since i'm new at this, but how do i do that? i though passing a connection to the form itself is enough.

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Delete from datagrid

    could you post your delete command

    in passing a connection to your command
    VB Code:
    1. 'cn is your connection
    2.  Dim da As New SqlDataAdapter("delete from table where id='" & DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0) & "'", [b]cn[/b])

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    210

    Re: Delete from datagrid

    This my whole Delete command (I have a feeling I missed something)

    Private Sub btn_Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Delete.Click
    Dim strAns As String

    strMode = "Delete"
    strAns = MsgBox("Are you sure you want to delete the selected row?", MsgBoxStyle.YesNo)
    If strAns = "6" Then
    Try
    Dim dt_BussLine As DataTable = Me.dg_BusLine.DataSource
    dt_BussLine.Rows(dg_BusLine.CurrentRowIndex).Delete()
    dadaptBusLine.Update(DsBusLine1)
    Me.DsBusLine1.AcceptChanges()
    Catch eexception As Exception
    MsgBox(eexception.Message)
    End Try
    End If
    End Sub

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Delete from datagrid

    try this one out
    VB Code:
    1. Dim dt_BussLine As DataTable = Me.dg_BusLine.DataSource
    2. dt_BussLine.Rows(dg_BusLine.CurrentRowIndex).Delete()
    3. dim cm as new sqlcommandbuilder(dadaptBusLine)
    4. dadaptBusLine.Update(DsBusLine1)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    210

    Re: Delete from datagrid

    same error message I'm afraid :

    "Update requires the DeleteCommand to have a Connection object. The Connection property of the DeleteCommand has not been initialized"

  8. #8
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Delete from datagrid

    if that's the case then there is something wrong w/ your dataadapter and your connection.

    does the dadaptBusLine is the one you use in filling your datatable and binding it to datagrid?

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    210

    Re: Delete from datagrid

    uh-oh...databind?

    is this code databinding it to the datagrid?

    Private Sub frm_BusLine_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim frm_Parent As frm_SRMAINSCRN

    dim dadaptBusLine As New SqlClient.SqlDataAdapter("select buss_code, buss_desc from cde_buss_line", conBusLine)

    Me.Text = "Business LIne"
    Me.txt_BusLine.ReadOnly = True
    Me.txt_BusDesc.ReadOnly = True
    Me.btn_Save.Enabled = False
    Me.btn_Cancel.Enabled = False
    strMode = "Initial"

    dadaptBusLine.Fill(DsBusLine1, "CDE_Buss_Line")

    Dim dgts_BusLine As New DataGridTableStyle
    dgts_BusLine.MappingName = "CDE_BUSS_LINE"
    Dim acol1 As New DataGridTextBoxColumn
    Dim acol2 As New DataGridTextBoxColumn
    acol1.MappingName = "Buss_Code"
    acol1.HeaderText = "Business Code"
    acol1.Width = 113
    dgts_BusLine.GridColumnStyles.Add(acol1)
    acol2.MappingName = "Buss_Desc"
    acol2.HeaderText = "Description"
    acol2.Width = 200
    dgts_BusLine.GridColumnStyles.Add(acol2)
    Me.dg_BusLine.TableStyles.Add(dgts_BusLine)
    RowNum = Me.dg_BusLine.CurrentRowIndex
    Me.txt_BusLine.Text = dg_BusLine(RowNum, 0)
    Me.txt_BusDesc.Text = dg_BusLine(RowNum, 1)

    End Sub


    I guess to make things easier for both of us. Maybe you can give me steps (not necessarily code though codes would be great) on how to delete a row from the datagrid and then apply that deletion to the table itself.

  10. #10
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Delete from datagrid

    ok this one.
    it works fine in my machine
    VB Code:
    1. 'cn is a connection
    2. Dim ds As New DataSet()
    3.     Dim da As New SqlDataAdapter("select * from test", cn)
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         ds.Tables.Add("dt")
    6.     End Sub
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         da.Fill(ds.Tables("dt"))
    10.         DataGrid1.DataSource = ds.Tables("dt")
    11.     End Sub
    12.  
    13.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    14.         Try
    15.             Dim d As DataRow = ds.Tables("dt").Rows(DataGrid1.CurrentRowIndex)
    16.             d.Delete()
    17.             Dim cmb As New SqlCommandBuilder(da)
    18.             da.Update(ds.Tables("dt"))
    19.             MessageBox.Show("Data has been updated")
    20.         Catch ex As Exception
    21.             MessageBox.Show("You must select first a row")
    22.         End Try
    23.     End Sub

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    210

    Re: Delete from datagrid

    sorry didn't work. but it worked when i added :

    dadaptBusLine.DeleteCommand = New SqlClient.SqlCommand("delete from cde_buss_line where buss_code ='" & Me.txt_BusLine.Text & "'", conBusLine)

    what do you think? i'm afraid that it might have some hidden repurcussions that i don't know about.

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