none
Cant Delete record from Database RRS feed

  • Question

  • I am trying to delete selected row, i can delete from datagridview but cant delete from database.

    my coding is bellow:

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click


            Dim conn As New SqlServerCe.SqlCeConnection("Data Source=|DataDirectory|\CalDatabase.sdf")
            Dim sqlAdapter As New SqlServerCe.SqlCeDataAdapter("SELECT * FROM tbl_Invoice", conn)

            sqlAdapter.Update(Ds_Invoice.tbl_Invoice)

            For Each row As DataGridViewRow In DataGridView1.Rows
                If row.Selected = True Then
                    Dim cmd As New SqlServerCe.SqlCeCommand
                    cmd = New SqlServerCe.SqlCeCommand("delete_row", conn)
                    cmd.CommandText = "delete from tbl_Invoice Where " & "Month= " & row.DataBoundItem(1).ToString
                    conn.Open()
                    cmd.ExecuteNonQuery()
                    conn.Close()
                    Ds_Invoice.Tables(0).Rows.RemoveAt(row.Index)
                    DataGridView1.DataSource = Ds_Invoice.Tables(0)
                End If
            Next

        End Sub

    Plz help me.

    Friday, May 24, 2013 6:28 PM