Database DELETE Command error

  • Thread starter Thread starter Rakshithc
  • Start date Start date
R

Rakshithc

Guest
I think there is some coding error in my project so that I failed in many databases. . Here I need one help in delete command... save and add command works well

code is below

Imports System.Data.SqlClient

Public Class Form1
Private MyDatAdp As New SqlDataAdapter
Private MyCmdBld As New SqlCommandBuilder
Private MyDataTbl As New DataTable
Private MyCn As New SqlConnection
Private MyRowPosition As Integer = 0

Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
MyCn.Close()
MyCn.Dispose()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MyCn.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\My Files\Project\Add-Update-Delete\Add-Update-Delete\Database1.mdf;Integrated Security=True"
MyCn.Open()
MyDatAdp = New SqlDataAdapter(“Select* from Contact”, MyCn)
MyCmdBld = New SqlCommandBuilder(MyDatAdp)
MyDatAdp.Fill(MyDataTbl)
Dim MyDataRow As DataRow = MyDataTbl.Rows(0)
Dim strName As String
Dim strState As String
strName = MyDataRow(“ContactName”)
strState = MyDataRow(“State”)
TxtName.Text = strName.ToString
TxtState.Text = strState.ToString
Me.showRecords()
End Sub
Private Sub showRecords()
If MyDataTbl.Rows.Count = 0 Then
txtName.Text = “”
txtState.Text = “”
Exit Sub
End If
txtName.Text = MyDataTbl.Rows(MyRowPosition)(“ContactName”).ToString
txtState.Text = MyDataTbl.Rows(MyRowPosition)(“State”).ToString
End Sub

Private Sub BtnFirst_Click(sender As Object, e As EventArgs) Handles BtnFirst.Click
MyRowPosition = 0
Me.showRecords()
End Sub

Private Sub BtnPrevious_Click(sender As Object, e As EventArgs) Handles BtnPrevious.Click
If MyRowPosition > 0 Then
MyRowPosition = MyRowPosition - 1
Me.showRecords()
End If
End Sub

Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
If MyRowPosition < (MyDataTbl.Rows.Count - 1) Then
MyRowPosition = MyRowPosition + 1
Me.showRecords()
End If
End Sub

Private Sub BtnLast_Click(sender As Object, e As EventArgs) Handles BtnLast.Click
If MyDataTbl.Rows.Count > 0 Then
MyRowPosition = MyDataTbl.Rows.Count - 1
Me.showRecords()
End If
End Sub

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
Dim MyNewRow As DataRow = MyDataTbl.NewRow()
MyDataTbl.Rows.Add(MyNewRow)
MyRowPosition = MyDataTbl.Rows.Count - 1
Me.showRecords()
End Sub

Private Sub BtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnDelete.Click
Dim MyDatAdp = New SqlDataAdapter(“Select* from Contact”, MyCn)

If MyDataTbl.Rows.Count <> 0 Then
MyDataTbl.Rows(MyRowPosition).Delete()
MyRowPosition = 0
MyDatAdp.Update(MyDataTbl)

End If
End Sub

Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnSave.Click
If MyDataTbl.Rows.Count <> 0 Then
MyDataTbl.Rows(MyRowPosition)(“ContactName”) = txtName.Text
MyDataTbl.Rows(MyRowPosition)(“state”) = txtState.Text
MyDatAdp.Update(MyDataTbl)
End If
End Sub
End Class


This is the Error while deleting data


cd80add799c32df86ca183c5d56f1289._.jpg


Continue reading...
 
Back
Top