Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Delete Button

Status
Not open for further replies.

anubis7000

Programmer
Apr 27, 2006
19
US
Hi,

I am trying to create a delete button on my ASP.net webpage. I am using a ms access db as my backend.

Here is my code:

Protected Sub DELETE_BTN_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DELETE_BTN.Click

'PET_GRID is the name of my datagrid
Dim foundrec As Boolean
foundrec = False

Dim pet_link As ADODB.Connection
pet_link = New ADODB.Connection

Dim pet_record As ADODB.Recordset
pet_record = New ADODB.Recordset

Dim pet_path As String
pet_path = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator.DAMAGEINC\My Documents\Visual Studio 2005\WebSites\WebSite1\App_Data\pets.mdb;"

pet_link.Open(pet_path)

pet_record.Open("SELECT * FROM PET", pet_link, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
'creates a link to the backend access database

pet_record.MoveFirst()

Do While Not pet_record.EOF
If pet_record.Fields("PET_ID").Value = PET_GRID.SelectedValue Then
'PET_GRID.SelectedValue returns the value of "PET_ID" of the currently selected record in the grid
pet_record.Delete(ADODB.AffectEnum.adAffectCurrent)
pet_record.Update()
MsgBox("Name Successfully deleted", vbInformation)
foundrec = True
End If

pet_record.MoveNext()

Loop
'goes to the first record in the backend database and tries to find the record that has the same PK as the one that
'is currently selected in the grid. Once found, it deletes that record


If foundrec = False Then
MsgBox("No matching record found", MsgBoxStyle.Exclamation)
End If

pet_record.Close()
PET_GRID.DataBind()

End Sub

This code works, however, if I do not have a message box at the end that says "Name Successfully deleted", the webpage will not update, and will still display the record, even though it no longer exists.

Any suggestions? Also, the method I am using to connect/open the backend database seems inefficient, as I have seen other code that uses temporary datasets, though I am not sure how to implement it myself.

Thanks in advance.
 
There's a lot of problems with this code. I'll try to list them:

1) Microsoft Access generally isn't very good for web applications. Try using SQL Server instead.
2) You are using ADODB rather than ADO.NET. Try searching google for an example of how you can use ADO.NET.
3) You are looping through all records until you find the one you want rather than using a simple WHERE clause. I suggest you read up on SQL before proceeding.
4) You are using a windows forms messagebox which won't work in a web application environment.

These problems are only from the one event you've posted but I imagine they exist in the page as a whole.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top