here is my code
Try
If Not con.State = ConnectionState.Open Then
con.Open()
End If
Dim cmd As New SqlCommand
cmd.Connection = con
cmd.CommandText = "DELETE FROM PRODUCT WHERE ID_PRODUCT=" & Me.DataGridView1.CurrentRow.Cells(0).Value & "'"
cmd.ExecuteNonQuery()
Finally
con.Close()
End Try
thanks in advance
mohamad gunturPosted Oct 3, 2012, 5:41 AM
but i got the solution without using "Count", here is my code. and it works out :)
Satyapriya NayakPosted Oct 3, 2012, 1:13 AM
Try this....
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim ConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("dsn")
Dim con As OleDbConnection = New OleDbConnection(ConnectionString)
Dim com As OleDbCommand
Dim oledbda As OleDbDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim dr As DataRow
Dim dc As DataColumn
Dim str As String
Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
com = New OleDbCommand()
If DataGridView1.Rows.Count > 1 AndAlso DataGridView1.SelectedRows(0).Index <> DataGridView1.Rows.Count - 1 Then
com.CommandText = "DELETE FROM employee WHERE ID=" & DataGridView1.SelectedRows(0).Cells(0).Value.ToString() & ""
con.Open()
com.Connection = con
com.ExecuteNonQuery()
con.Close()
DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
MessageBox.Show("Record Successfully Deleted")
End If
bindgrid()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bindgrid()
End Sub
Sub bindgrid()
Try
con.Open()
str = "select * from employee"
com = New OleDbCommand(str, con)
oledbda = New OleDbDataAdapter(com)
ds = New DataSet
oledbda.Fill(ds, "employee")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "employee"
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Thanks
If this post helps you mark it as answer