Newbie Question: Deleting from object data source in ASP 2

D

damiensawyer

Hello all,

I am playing around with the new object data source control. I have
successfully managed to select, insert and update records via the
object, however am having trouble deleting. It seems that the primary
key (au_id) is getting passed through to the object as "nothing". Can
anyone see what I'm doing wrong?

Thanks very much in advance,


Damien Sawyer



<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetAuthors"
TypeName="PubsClasses.AuthorClass" UpdateMethod="UpdateAuthor"
DeleteMethod="DeleteAuthor">

<UpdateParameters>
<asp:parameter Name="au_id" Type="String" />
<asp:parameter Name="au_lname" Type="String" />
<asp:parameter Name="au_fname" Type="String" />
<asp:parameter Name="au_phone" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:parameter Name="au_id" Type="String" />
</DeleteParameters>
</asp:ObjectDataSource>

</div>
<br />
<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" ShowSelectButton="True" />
</Columns>
</asp:GridView>
</form>
</body>
</html>


Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Data
Namespace PubsClasses
Public Class AuthorClass
Private dsAuthors As DataSet = _
New System.Data.DataSet("ds1")
Private filePath As String = _
HttpContext.Current.Server.MapPath _
("~/App_Data/authors.xml")
Public Sub New()
dsAuthors.ReadXml(filePath, Data.XmlReadMode.ReadSchema)
End Sub

' ****************** PROBLEM IN THIS SUB *********************
Public Sub DeleteAuthor(ByVal au_id As String)
' Problem is here - au_id always passed as "nothing"
Dim Delrow As DataRow =
dsAuthors.Tables(0).Rows.Find(au_id)
dsAuthors.Tables(0).Rows.Remove(Delrow)
dsAuthors.WriteXml(filePath, Data.XmlWriteMode.WriteSchema)
End Sub

Public Function GetAuthors() As DataSet
Return dsAuthors
End Function

Public Sub InsertAuthor(ByVal au_id As String, _
ByVal au_lname As String, _
ByVal au_fname As String, ByVal au_phone As String)
Dim workRow As DataRow = dsAuthors.Tables(0).NewRow
workRow.BeginEdit()
workRow(0) = au_id
workRow(1) = au_lname
workRow(2) = au_fname
workRow(3) = au_phone
workRow.EndEdit()
dsAuthors.Tables(0).Rows.Add(workRow)
dsAuthors.WriteXml(filePath, Data.XmlWriteMode.WriteSchema)
End Sub



Public Sub UpdateAuthor(ByVal au_id As String, _
ByVal au_lname As String, _
ByVal au_fname As String, ByVal au_phone As String)

Dim workRow As DataRow = dsAuthors.Tables(0).NewRow
workRow.BeginEdit()
workRow(0) = au_id
workRow(1) = au_lname
workRow(2) = au_fname
workRow(3) = au_phone
workRow.EndEdit()

Dim Delrow As DataRow =
dsAuthors.Tables(0).Rows.Find(au_id)
dsAuthors.Tables(0).Rows.Remove(Delrow)

' Add back the new row
dsAuthors.Tables(0).Rows.Add(workRow)
dsAuthors.WriteXml(filePath, Data.XmlWriteMode.WriteSchema)
End Sub



End Class

End Namespace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top