I am having problem deleting my data on click event

Private Sub btnDel_Click

. Here is my code:

Dim inc As Integer
    Dim cs As New SqlConnection("Data Source=Connect\SQLEXPRESS;Initial Catalog=ForumCrawl;Integrated Security=True")
    Dim dbSource As String
    Dim da As SqlDataAdapter '("SELECT * FROM search_result", cs)
    Dim ds As New DataSet
    Dim sql As String

    Dim maxrows As Integer

    Private Sub btn_Rtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Rtn.Click
        Welcome.Show()
        Me.Close()
    End Sub

    Private Sub Database_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cs.Open()

        sql = "SELECT * FROM tbl_content"
        da = New SqlDataAdapter(sql, cs)
        da.Fill(ds, "tbl_content")
        cs.Close()


        maxrows = ds.Tables("table").Rows.Count
        inc = -1
    End Sub

    Private Sub Navigate()
        tb_user_id.Text = ds.Tables("tbl_content").Rows(inc).Item(1)
        tb_url.Text = ds.Tables("tbl_content").Rows(inc).Item(2)
        tb_content.Text = ds.Tables("tbl_content").Rows(inc).Item(3)
        tb_date.Text = ds.Tables("tbl_content").Rows(inc).Item(4)
        tb_forum_name.Text = ds.Tables("tbl_content").Rows(inc).Item(5)
    End Sub



    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If inc <> maxrows - 1 Then
            inc = inc + 1
            Navigate()
        Else
            MsgBox("No more Rows")
        End If

    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        If inc > 0 Then
            inc = inc - 1
            Navigate()
        Else
            MsgBox("FirstRecords")
        End If
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        If inc <> maxrows Then
            inc = maxrows - 1
            Navigate()
        End If
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        If inc <> 0 Then
            inc = 0
            Navigate()
        End If
    End Sub
    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click

        Dim cb As New SqlCommandBuilder(da)
        ds.Tables("search_result").Rows(inc).Delete()
        ds.AcceptChanges()

        maxrows = maxrows - 1

        inc = 0
        Navigate()
        da.Update(ds, "search_result")

        If MessageBox.Show("Do you really want to Delete this Record?", _
            "Delete", MessageBoxButtons.YesNo, _
            MessageBoxIcon.Warning) = DialogResult.No Then
            MsgBox("Operation Cancelled")
            Exit Sub
        End If
    End Sub

This is the error I get:

I tried to debug the code and it is getting stuck here where the row value is 0

Private Sub Navigate()
        tb_user_id.Text = ds.Tables("tbl_content").Rows(inc).Item(1)
        tb_url.Text = ds.Tables("tbl_content").Rows(inc).Item(2)
        tb_content.Text = ds.Tables("tbl_content").Rows(inc).Item(3)
        tb_date.Text = ds.Tables("tbl_content").Rows(inc).Item(4)
        tb_forum_name.Text = ds.Tables("tbl_content").Rows(inc).Item(5)
    End Sub

The error I am getting is

There is no row at position 0

Please advice.

Recommended Answers

All 3 Replies

Member Avatar for stbuchok

What is the value of the following:

ds.Tables("tbl_content").Rows.Count

What is the value of the following:

ds.Tables("tbl_content").Rows.Count

The value is 2 and maxrows = 0

Heres the image: <<click>>

I have changed the table name so ignore the "search_result"

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.