WolfManBN25
asked on
delete datarow from datatable in datagrid
I have a datagrid: dgprogress, that is binded to a datatable and the datagrid has a delete button which i would like to have a datarow deleted when the delete button is click by the corresponding delete button.
Sub deleteclicked(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'when a user clicks the delete next to an entry it is deleted from the datatable and the database
Dim temprow As DataRow
Dim tempds As DataSet
tempds = Session("dataset_ds")
Dim iid As String = dgprogress.Items(e.Item.It emIndex).C ells(1).Te xt 'Grab the id from the hidden column
' Dim remove As Integer = Convert.ToInt32(dgprogress .Items(e.I tem.ItemIn dex))
' lblerrormessage.Text = e.Item.ItemIndex
lblerror.Text = iid
strSQL = "Delete from BILLING_CAL where id = '" & iid & "'" 'removed from the database
Dim i
For i = 0 To tempds.Tables.Count - 1
If tempds.Tables("table").Row s.Item("id ") Is iid.ToString Then
temprow.Table().Rows().Rem oveAt(i)
End If
Next
ds = tempds
dgprogress.DataSource = ds
dgprogress.DataBind()
ExecuteDBCommand(strSQL) ' executes the delete statement
End Sub
My problem it is not working. What am i doing wrong.
Sub deleteclicked(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'when a user clicks the delete next to an entry it is deleted from the datatable and the database
Dim temprow As DataRow
Dim tempds As DataSet
tempds = Session("dataset_ds")
Dim iid As String = dgprogress.Items(e.Item.It
' Dim remove As Integer = Convert.ToInt32(dgprogress
' lblerrormessage.Text = e.Item.ItemIndex
lblerror.Text = iid
strSQL = "Delete from BILLING_CAL where id = '" & iid & "'" 'removed from the database
Dim i
For i = 0 To tempds.Tables.Count - 1
If tempds.Tables("table").Row
temprow.Table().Rows().Rem
End If
Next
ds = tempds
dgprogress.DataSource = ds
dgprogress.DataBind()
ExecuteDBCommand(strSQL) ' executes the delete statement
End Sub
My problem it is not working. What am i doing wrong.
ASKER
I get an error message:
Server Error in '/WebApplication1' Application.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 176: Dim i
Line 177: For i = 0 To tempds.Tables.Count - 1
Line 178: If tempds.Tables("table").Row s.Item("id ") Is iid Then
Line 179: ' tempds.Tables(0).Rows(0).D elete()
Line 180: 'temprow.Delete()
Source File: c:\inetpub\wwwroot\WebAppl ication1\b illing.asp x.vb Line: 178
Server Error in '/WebApplication1' Application.
--------------------------
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 176: Dim i
Line 177: For i = 0 To tempds.Tables.Count - 1
Line 178: If tempds.Tables("table").Row
Line 179: ' tempds.Tables(0).Rows(0).D
Line 180: 'temprow.Delete()
Source File: c:\inetpub\wwwroot\WebAppl
try with this..
-------------------------- ---------- ---------- ---------- ---------- ---------- --------
For i As Integer = 0 To tempds.Tables(0).Rows.Coun t - 1
If tempds.Tables(0).Rows(i)(" id").ToStr ing() = iid.ToString() Then
tempds.Tables(0).Rows.Remo veAt(i)
End If
Next
tempds.AcceptChanges()
ds = tempds
dgprogress.DataSource = ds
dgprogress.DataBind()
-------------------------- ---------- ---------- ---------- ---------- ---------- --------
--------------------------
For i As Integer = 0 To tempds.Tables(0).Rows.Coun
If tempds.Tables(0).Rows(i)("
tempds.Tables(0).Rows.Remo
End If
Next
tempds.AcceptChanges()
ds = tempds
dgprogress.DataSource = ds
dgprogress.DataBind()
--------------------------
You are checking a dararow with a string. How many tables are there in dataset. I dont understand why u r looping thru all tables in dataset (For i = 0 To tempds.Tables.Count - 1)
i think u need to check something like If tempds.Tables("table").Row s(0).Item( "id") Is iid Then
may b in ur case u need to Rows(e.Item.ItemIndex) depends in ur condition,
u can also use simply ds.Tables(0).Rows(0)(0).To String
i think u need to check something like If tempds.Tables("table").Row
may b in ur case u need to Rows(e.Item.ItemIndex) depends in ur condition,
u can also use simply ds.Tables(0).Rows(0)(0).To
ASKER
my hidden id column in the datagrid/datatable is a string it is a concatination of patient number, and the date/time.
There is one table in the dataset with many rows. What i want to do is delete a entry in the datatable based on the hidden id column. Then update the dataset and rebind to the datagrid.
everytime i run the web app I get an error message:
Server Error in '/WebApplication1' Application.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
There is no row at position 4.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeExce ption: There is no row at position 4.
Source Error:
Line 175:
Line 176: For i As Integer = 0 To tempds.Tables(0).Rows.Coun t - 1
Line 177: If tempds.Tables(0).Rows(i)(" id").ToStr ing() = iid.ToString() Then
Line 178: tempds.Tables(0).Rows.Remo veAt(i)
Line 179: End If
Source File: c:\inetpub\wwwroot\WebAppl ication1\b illing.asp x.vb Line: 177
I entered 5 pieces of sample data.
There is one table in the dataset with many rows. What i want to do is delete a entry in the datatable based on the hidden id column. Then update the dataset and rebind to the datagrid.
everytime i run the web app I get an error message:
Server Error in '/WebApplication1' Application.
--------------------------
There is no row at position 4.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeExce
Source Error:
Line 175:
Line 176: For i As Integer = 0 To tempds.Tables(0).Rows.Coun
Line 177: If tempds.Tables(0).Rows(i)("
Line 178: tempds.Tables(0).Rows.Remo
Line 179: End If
Source File: c:\inetpub\wwwroot\WebAppl
I entered 5 pieces of sample data.
The problem is that the For i To x construct calculates the x at the start of the loop and does not recalculate it as you pass through the loop. So, it starts off (with 5 rows) as 4. Within the loop, the code removes one of the rows: there are now only 4 rows. But the loop still presses on to the fifth row - that is, index 4 - because that is the maximum value for i that it was originally given.
There's all sorts of alternatives. The simplest is probably to reverse the order of the loop
For i As Integer = tempds.Tables(0).Rows.Coun t - 1 To 0 Step - 1
That way, the removal of a later row does not mean that earlier rows are not still there to be visited.
Roger
There's all sorts of alternatives. The simplest is probably to reverse the order of the loop
For i As Integer = tempds.Tables(0).Rows.Coun
That way, the removal of a later row does not mean that earlier rows are not still there to be visited.
Roger
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
tempds.AcceptChanges()
ds = tempds
dgprogress.DataSource = ds
dgprogress.DataBind()