473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Record not being deleted in dbase, even tho the display on datagrid is deleted..

IN my code behind .vb page for a delete records script (this also does
a deletion confirmation with a javascript popup, this gets called on
my front .aspx page with the datagrid), I'm not sure if he
row.delete() in my Delete_Row sub is correct because when deleting a
record from the dbase, it doesnot get deleted from the actual
database, even tho on the datagrid it does show that the record is
deleted:
'''''''''''''
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Public Class ConfirmDelDG
Inherits System.Web.UI.Page

Protected WithEvents dtgProducts As
System.Web.UI.WebControls.DataGrid
Private strConnection As String =
"SERVER=xxxx.xxx.xxx.xx;UID=xxxx;PWD=xxxx;DATABASE =players;"
Private strSql As String = "SELECT * FROM pictures Order by pid
desc"
Private objConn As SqlConnection

Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindTheGrid()
End If
End Sub

Private Sub BindTheGrid()
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "pictures")
Disconnect()

dtgProducts.DataSource = ds.Tables("pictures")
dtgProducts.DataBind()
End Sub

Private Sub Connect()
If objConn Is Nothing Then
objConn = New SqlConnection(strConnection)
End If

If objConn.State = ConnectionState.Closed Then
objConn.Open()
End If
End Sub

Private Sub Disconnect()
objConn.Dispose()
End Sub

Private Sub dtgProducts_ItemDataBound (ByVal sender As
System.Object, _
ByVal e As DataGridItemEventArgs) Handles
dtgProducts.ItemDataBound

Dim btn As Button
If e.Item.ItemType = ListItemType.Item or e.Item.ItemType =
ListItemType.AlternatingItem Then
btn = CType(e.Item.Cells(0).FindControl("btnDelete"), Button)
btn.Attributes.Add("onclick", "return confirm_delete();")
End If

End Sub

Public Sub Delete_Row(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)

' Retrieve the ID of the product to be deleted
Dim pid As system.Int32 =
System.Convert.ToInt32(E.Item.Cells(0).Text)

dtgProducts.EditItemIndex = -1

' Create and load a DataSet
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "pictures")
Disconnect()

' Mark the product as Deleted in the DataSet
Dim tbl As DataTable = ds.Tables("pictures")
tbl.PrimaryKey = New DataColumn() _
{ _
tbl.Columns("pid") _
}
Dim row As DataRow = tbl.Rows.Find(pid)
row.Delete()
' Display remaining rows in the DataGrid
dtgProducts.DataSource = ds.Tables("pictures")
dtgProducts.DataBind()

End Sub

End Class
''''''''''''

??????
chumley
Jul 21 '05 #1
3 2180
Hey Chumley,

It looks like you're not sending any word of the deletion back to the
database. To the contrary, you're actually re-pulling data from the
database then affecting the delete before letting it refresh the datagrid.

One way to do it would be to pull the data from the database in the
Delete_Row function as you currently do.
- Define a command object that would perform a delete to the effect of
"DELETE * FROM MyTable WHERE PID = @PID"
- Set its parameters
- Set the command object as your data adapter's DeleteCommand.
- Call your data adapter's Update() function.

Just as Fill uses the SelectCommand to pull data through the data adapter to
your data set. The Update will apply the queries referenced in its
InsertCommand, DeleteCommand and UpdateCommand back to the database.

HTH,

John
"Chumley the Walrus" <sp*******@yahoo.com> wrote in message
news:1e**************************@posting.google.c om...
IN my code behind .vb page for a delete records script (this also does
a deletion confirmation with a javascript popup, this gets called on
my front .aspx page with the datagrid), I'm not sure if he
row.delete() in my Delete_Row sub is correct because when deleting a
record from the dbase, it doesnot get deleted from the actual
database, even tho on the datagrid it does show that the record is
deleted:
'''''''''''''
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Public Class ConfirmDelDG
Inherits System.Web.UI.Page

Protected WithEvents dtgProducts As
System.Web.UI.WebControls.DataGrid
Private strConnection As String =
"SERVER=xxxx.xxx.xxx.xx;UID=xxxx;PWD=xxxx;DATABASE =players;"
Private strSql As String = "SELECT * FROM pictures Order by pid
desc"
Private objConn As SqlConnection

Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindTheGrid()
End If
End Sub

Private Sub BindTheGrid()
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "pictures")
Disconnect()

dtgProducts.DataSource = ds.Tables("pictures")
dtgProducts.DataBind()
End Sub

Private Sub Connect()
If objConn Is Nothing Then
objConn = New SqlConnection(strConnection)
End If

If objConn.State = ConnectionState.Closed Then
objConn.Open()
End If
End Sub

Private Sub Disconnect()
objConn.Dispose()
End Sub

Private Sub dtgProducts_ItemDataBound (ByVal sender As
System.Object, _
ByVal e As DataGridItemEventArgs) Handles
dtgProducts.ItemDataBound

Dim btn As Button
If e.Item.ItemType = ListItemType.Item or e.Item.ItemType =
ListItemType.AlternatingItem Then
btn = CType(e.Item.Cells(0).FindControl("btnDelete"), Button)
btn.Attributes.Add("onclick", "return confirm_delete();")
End If

End Sub

Public Sub Delete_Row(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)

' Retrieve the ID of the product to be deleted
Dim pid As system.Int32 =
System.Convert.ToInt32(E.Item.Cells(0).Text)

dtgProducts.EditItemIndex = -1

' Create and load a DataSet
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "pictures")
Disconnect()

' Mark the product as Deleted in the DataSet
Dim tbl As DataTable = ds.Tables("pictures")
tbl.PrimaryKey = New DataColumn() _
{ _
tbl.Columns("pid") _
}
Dim row As DataRow = tbl.Rows.Find(pid)
row.Delete()
' Display remaining rows in the DataGrid
dtgProducts.DataSource = ds.Tables("pictures")
dtgProducts.DataBind()

End Sub

End Class
''''''''''''

??????
chumley

Jul 21 '05 #2
Related comment/question -- In a situation of mine, the data adapter's
update() method didn't do a thing even though everybody I talked to and
everything I read said it would work just fine. Finally I gave up and I had
to resort to using an executenonquery to finally delete the row.

Any thoughts as to why this was the case?

Thanks,
TomG

----------------------------------

"John Spiegel" <js******@YETANOTHERSPAMHATERc-comld.com> wrote in message
news:eP****************@TK2MSFTNGP11.phx.gbl...
Hey Chumley,

It looks like you're not sending any word of the deletion back to the
database. To the contrary, you're actually re-pulling data from the
database then affecting the delete before letting it refresh the datagrid.

One way to do it would be to pull the data from the database in the
Delete_Row function as you currently do.
- Define a command object that would perform a delete to the effect of
"DELETE * FROM MyTable WHERE PID = @PID"
- Set its parameters
- Set the command object as your data adapter's DeleteCommand.
- Call your data adapter's Update() function.

Just as Fill uses the SelectCommand to pull data through the data adapter to your data set. The Update will apply the queries referenced in its
InsertCommand, DeleteCommand and UpdateCommand back to the database.

HTH,

John
"Chumley the Walrus" <sp*******@yahoo.com> wrote in message
news:1e**************************@posting.google.c om...
IN my code behind .vb page for a delete records script (this also does
a deletion confirmation with a javascript popup, this gets called on
my front .aspx page with the datagrid), I'm not sure if he
row.delete() in my Delete_Row sub is correct because when deleting a
record from the dbase, it doesnot get deleted from the actual
database, even tho on the datagrid it does show that the record is
deleted:
'''''''''''''
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Public Class ConfirmDelDG
Inherits System.Web.UI.Page

Protected WithEvents dtgProducts As
System.Web.UI.WebControls.DataGrid
Private strConnection As String =
"SERVER=xxxx.xxx.xxx.xx;UID=xxxx;PWD=xxxx;DATABASE =players;"
Private strSql As String = "SELECT * FROM pictures Order by pid
desc"
Private objConn As SqlConnection

Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindTheGrid()
End If
End Sub

Private Sub BindTheGrid()
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "pictures")
Disconnect()

dtgProducts.DataSource = ds.Tables("pictures")
dtgProducts.DataBind()
End Sub

Private Sub Connect()
If objConn Is Nothing Then
objConn = New SqlConnection(strConnection)
End If

If objConn.State = ConnectionState.Closed Then
objConn.Open()
End If
End Sub

Private Sub Disconnect()
objConn.Dispose()
End Sub

Private Sub dtgProducts_ItemDataBound (ByVal sender As
System.Object, _
ByVal e As DataGridItemEventArgs) Handles
dtgProducts.ItemDataBound

Dim btn As Button
If e.Item.ItemType = ListItemType.Item or e.Item.ItemType =
ListItemType.AlternatingItem Then
btn = CType(e.Item.Cells(0).FindControl("btnDelete"), Button)
btn.Attributes.Add("onclick", "return confirm_delete();")
End If

End Sub

Public Sub Delete_Row(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)

' Retrieve the ID of the product to be deleted
Dim pid As system.Int32 =
System.Convert.ToInt32(E.Item.Cells(0).Text)

dtgProducts.EditItemIndex = -1

' Create and load a DataSet
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "pictures")
Disconnect()

' Mark the product as Deleted in the DataSet
Dim tbl As DataTable = ds.Tables("pictures")
tbl.PrimaryKey = New DataColumn() _
{ _
tbl.Columns("pid") _
}
Dim row As DataRow = tbl.Rows.Find(pid)
row.Delete()
' Display remaining rows in the DataGrid
dtgProducts.DataSource = ds.Tables("pictures")
dtgProducts.DataBind()

End Sub

End Class
''''''''''''

??????
chumley


Jul 21 '05 #3
Related comment/question -- In a situation of mine, the data adapter's
update() method didn't do a thing even though everybody I talked to and
everything I read said it would work just fine. Finally I gave up and I had
to resort to using an executenonquery to finally delete the row.

Any thoughts as to why this was the case?

Thanks,
TomG

----------------------------------

"John Spiegel" <js******@YETANOTHERSPAMHATERc-comld.com> wrote in message
news:eP****************@TK2MSFTNGP11.phx.gbl...
Hey Chumley,

It looks like you're not sending any word of the deletion back to the
database. To the contrary, you're actually re-pulling data from the
database then affecting the delete before letting it refresh the datagrid.

One way to do it would be to pull the data from the database in the
Delete_Row function as you currently do.
- Define a command object that would perform a delete to the effect of
"DELETE * FROM MyTable WHERE PID = @PID"
- Set its parameters
- Set the command object as your data adapter's DeleteCommand.
- Call your data adapter's Update() function.

Just as Fill uses the SelectCommand to pull data through the data adapter to your data set. The Update will apply the queries referenced in its
InsertCommand, DeleteCommand and UpdateCommand back to the database.

HTH,

John
"Chumley the Walrus" <sp*******@yahoo.com> wrote in message
news:1e**************************@posting.google.c om...
IN my code behind .vb page for a delete records script (this also does
a deletion confirmation with a javascript popup, this gets called on
my front .aspx page with the datagrid), I'm not sure if he
row.delete() in my Delete_Row sub is correct because when deleting a
record from the dbase, it doesnot get deleted from the actual
database, even tho on the datagrid it does show that the record is
deleted:
'''''''''''''
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Public Class ConfirmDelDG
Inherits System.Web.UI.Page

Protected WithEvents dtgProducts As
System.Web.UI.WebControls.DataGrid
Private strConnection As String =
"SERVER=xxxx.xxx.xxx.xx;UID=xxxx;PWD=xxxx;DATABASE =players;"
Private strSql As String = "SELECT * FROM pictures Order by pid
desc"
Private objConn As SqlConnection

Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindTheGrid()
End If
End Sub

Private Sub BindTheGrid()
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "pictures")
Disconnect()

dtgProducts.DataSource = ds.Tables("pictures")
dtgProducts.DataBind()
End Sub

Private Sub Connect()
If objConn Is Nothing Then
objConn = New SqlConnection(strConnection)
End If

If objConn.State = ConnectionState.Closed Then
objConn.Open()
End If
End Sub

Private Sub Disconnect()
objConn.Dispose()
End Sub

Private Sub dtgProducts_ItemDataBound (ByVal sender As
System.Object, _
ByVal e As DataGridItemEventArgs) Handles
dtgProducts.ItemDataBound

Dim btn As Button
If e.Item.ItemType = ListItemType.Item or e.Item.ItemType =
ListItemType.AlternatingItem Then
btn = CType(e.Item.Cells(0).FindControl("btnDelete"), Button)
btn.Attributes.Add("onclick", "return confirm_delete();")
End If

End Sub

Public Sub Delete_Row(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)

' Retrieve the ID of the product to be deleted
Dim pid As system.Int32 =
System.Convert.ToInt32(E.Item.Cells(0).Text)

dtgProducts.EditItemIndex = -1

' Create and load a DataSet
Connect()
Dim adapter As New SqlDataAdapter(strSql, objConn)
Dim ds As New DataSet()
adapter.Fill(ds, "pictures")
Disconnect()

' Mark the product as Deleted in the DataSet
Dim tbl As DataTable = ds.Tables("pictures")
tbl.PrimaryKey = New DataColumn() _
{ _
tbl.Columns("pid") _
}
Dim row As DataRow = tbl.Rows.Find(pid)
row.Delete()
' Display remaining rows in the DataGrid
dtgProducts.DataSource = ds.Tables("pictures")
dtgProducts.DataBind()

End Sub

End Class
''''''''''''

??????
chumley


Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Symphony | last post by:
Hi,all: our web application using vb.net and xp. we have a web form using a datagrid, when load that page, it will list all our cutomer info. to the datagrid. The first column of the datagrid is...
4
by: Susan Bricker | last post by:
I have a command button on a form that is supposed to Delete the record being displayed. The record is displayed one to a form. The form is not a Pop-Up nor is it Modal. Tracing the btnDelete...
0
by: Sharon | last post by:
I have a problem using with dbf file here. I have tried the code below using VB.NET with no errors. However, I use the exact same code in ASP NET, it prompt me error when I tried to insert a...
2
by: Chumley the Walrus | last post by:
IN my code behind .vb page for a delete records script (this also does a deletion confirmation with a javascript popup, this gets called on my front .aspx page with the datagrid), I'm not sure if...
3
by: Ryan Liu | last post by:
Can someone give a sample to prevent a row from being deleted in a datatable? I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not work. I need verify if there other data...
0
by: Iain | last post by:
Hi All I have a datagrid which takes it's data from a table on a remote i-series. This datagrid is loaded on Page Load and the user is able to select a record to edit by pressing the...
3
by: Ctal | last post by:
I have an app that populates several data tables on load. Each of these are bound to a datagrid. Above each datagrid I have several text boxes that display the data for the active row. There are...
9
by: =?Utf-8?B?UHJhdmlu?= | last post by:
We are using .net Framework 1.1 We are having one page on which we are using this Grid component. From this page we open a popup for adding new record as well as for editing an existing record...
8
by: =?Utf-8?B?bWlrZWc=?= | last post by:
Hi, I am building a small Help Desk application for my company and need to be able to edit "open" help desk issues. I use a simple datagrid to display each issue (6 per page) , with an Edit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.