For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > November 2006 > Problem with deleting rows from a table









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with deleting rows from a table
Amiram Korach

2006-11-30, 7:04 pm

I want to get data from a web service and put the data in a html table. For
some reasons, I can't use data binding, so I have to fill the rows manually.
In each row I put a button that connected to an event handler that deletes
the row from the data source by the web service API and refreshes the table.
(The buttons holds the row key in the command argument).
In the Load event I call the method that fill the table. In the event
handler that deletes the row, I need to refresh the table, because it happens
after the load event.
I can refresh the table in two ways. First, clear the table and fill it
again. Second, find the row of the button that was clicked and delete it.
With the first method, first time you delete a row it works, but second
attempt goes to the server, gets into the load event and not into the delete
event. Third attempt works and fourth does not, and so on.
With the second method, first attempt works, but second attempt deletes the
row below the row you want to delete.
I tried to fill the table in PreRender event instead of the load event, but
then you don't get into the delete event at all.

Here is some of a sample code (with Sql Server, not with a web service):

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

FillTable()

End Sub

Public Sub FillTable()

table1.Rows.Clear()

Dim ds As New DataSet
Dim da As New SqlDataAdapter("Select * FROM MyTable", ConStr)
da.Fill(ds)

For Each tmprow As DataRow In ds.Tables(0).Rows

Dim cell1 As New HtmlTableCell
Dim cell2 As New HtmlTableCell
Dim cell3 As New HtmlTableCell
Dim row As New HtmlTableRow

cell1.InnerText = tmprow("id")
row.Cells.Add(cell1)

cell2.InnerText = tmprow("des")
row.Cells.Add(cell2)

Dim tmpDeleteButton As New Button()
tmpDeleteButton.CommandArgument = tmprow("id")
tmpDeleteButton.Text = "delete"
AddHandler tmpDeleteButton.Click, AddressOf DeleteRow
cell3.Controls.Add(tmpDeleteButton)
row.Cells.Add(cell3)

table1.Rows.Add(row)

Next

End Sub

Protected Sub DeleteRow(ByVal sender As Object, ByVal e As EventArgs)

Dim a As Integer = DirectCast(sender, Button).CommandArgument
Dim sqlCon As New SqlConnection("Data Source=machshir;Initial
Catalog=devmazak;Integrated Security=True")
sqlCon.Open()
Dim cmd As New SqlCommand("Delete FROM MyTable WHERE id = " +
a.ToString, sqlCon)
cmd.ExecuteNonQuery()

' First Method
'FillTable()

' Second Method
'table1.Rows.Remove(DirectCast(DirectCast(sender,
Button).Parent.Parent, HtmlTableRow))

End Sub

Any help would be appriciated
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com