Newbie Question: Deleting from object data source in ASP 2

Discussion in 'ASP .Net' started by damiensawyer@yahoo.com.au, Dec 8, 2005.

  1. Guest

    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
     
    , Dec 8, 2005
    #1
    1. Advertising

  2. Morgan Guest

    Try setting your DataKeyField to your primary key.


    <> wrote in message
    news:...
    > 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
    >
     
    Morgan, Dec 8, 2005
    #2
    1. Advertising

  3. Guest

    Thanks Morgan,

    That worked a treat.


    DS
     
    , Dec 8, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Author Tarun Tyagi
    Replies:
    0
    Views:
    727
    Author Tarun Tyagi
    Dec 29, 2004
  2. Harry Barker
    Replies:
    2
    Views:
    529
    Alf P. Steinbach
    Apr 19, 2006
  3. Allan Ebdrup
    Replies:
    2
    Views:
    485
    Allan Ebdrup
    Oct 6, 2006
  4. Josselin
    Replies:
    3
    Views:
    163
    Peña, Botp
    Aug 17, 2007
  5. crea
    Replies:
    2
    Views:
    422
    Nobody
    Dec 28, 2012
Loading...

Share This Page