.NET 2.0 Delete using GridView and ObjectDataSource

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Barry L. Camp

    #1

    .NET 2.0 Delete using GridView and ObjectDataSource


    Hi all,

    Wondering if someone can help with a nagging problem I am having using
    a GridView and an ObjectDataSourc e. I have a simple situation where I
    am trying to delete a row from a table, but it doesn't seem to work at
    all. Below is my ASP.NET page, and further below, my VB.NET method that
    I am trying to call:

    <div id="admin-faq" class="page">
    <h2>Site FAQs (Frequently Asked Questions)</h2>
    <asp:Panel ID="ListPanel" runat="server" Width="99%"
    Visible="true">
    <asp:GridView ID="GridView1" runat="server"
    AutoGenerateCol umns="False" CellPadding="4" width="99%"
    DataSourceID="O bjectDataSource 1">
    <EmptyDataTempl ate>
    Apparently, nobody asks any questions around here.
    </EmptyDataTempla te>
    <AlternatingRow Style BackColor="#E0E 0E0" />
    <Columns>
    <asp:CommandFie ld ShowDeleteButto n="True" />
    <asp:BoundFie ld DataField="ID" HeaderText="ID"
    SortExpression= "ID" DataFormatStrin g="{0}" />
    <asp:BoundFie ld DataField="Ques tion"
    HeaderText="Que stion" SortExpression= "Question" />
    <asp:BoundFie ld DataField="Answ er"
    HeaderText="Ans wer" SortExpression= "Answer" />
    <asp:BoundFie ld DataField="Auth or"
    HeaderText="Aut hor" SortExpression= "Author" />
    <asp:CheckBoxFi eld DataField="IsPu blic"
    HeaderText="IsP ublic" SortExpression= "IsPublic" />
    </Columns>
    </asp:GridView>
    </asp:Panel>
    </div>

    <asp:ObjectData Source ID="ObjectDataS ource1" runat="server"
    DataObjectTypeN ame="System.Gui d" SelectMethod="G etFaqs"
    TypeName="FaqMa nager" DeleteMethod="R emoveFaq">
    <DeleteParamete rs>
    <asp:Paramete r Name="id" Type="Object" />
    </DeleteParameter s>
    </asp:ObjectDataS ource>


    Public NotInheritable Class FaqManager

    ' Other methods left out, for clarity.

    Public Shared Sub RemoveFaq(ByVal id As Guid)
    Using connection As New
    SqlConnection(C onfigurationMan ager.Connection Strings("blcamp ").ConnectionSt ring)
    Using command As New SqlCommand("Rem oveFaq", connection)
    command.Command Type = CommandType.Sto redProcedure
    command.Paramet ers.Add(New SqlParameter("@ FaqID", id))
    connection.Open ()
    command.Execute NonQuery()
    End Using
    End Using
    End Sub

    Public Shared Function GetFaqs() As Generic.List(Of Faq)
    Using connection As New
    SqlConnection(C onfigurationMan ager.Connection Strings("blcamp ").ConnectionSt ring)
    Using command As New SqlCommand("Get Faqs", connection)
    command.Command Type = CommandType.Sto redProcedure
    Dim filter As Boolean = Not
    (HttpContext.Cu rrent.User.IsIn Role("Friends") Or
    HttpContext.Cur rent.User.IsInR ole("Administra tors"))
    command.Paramet ers.Add(New SqlParameter("@ IsPublic",
    filter))
    connection.Open ()
    Dim list As New Generic.List(Of Faq)()
    Using reader As SqlDataReader = command.Execute Reader()
    Do While (reader.Read())
    Dim temp As New Faq(CType(reade r("FaqID"),
    Guid), CType(reader("Q uestion"), String), CType(reader("A nswer"),
    String), CType(reader("A uthor"), String), CType(reader("I sPublic"),
    Boolean))
    list.Add(temp)
    Loop
    End Using
    Return list
    End Using
    End Using
    End Function

    End Class


    When I try to run the above code as is, the RemoveFaq method executes,
    but the id parameter receives a value of <nothing>. Notice that
    currently, the GridView has no DataKeyNames attribute set. If I try to
    set it to Id, which is the primary key for my data as well as the
    Delete Parameter, I get the following exception:

    Could not find a property named 'id' on the type specified by the
    DataObjectTypeN ame property in ObjectDataSourc e 'ObjectDataSour ce1'.

    Does anyone out there know what I may be doing wrong here? Any help
    would really be appreciated. Thanks much,

    Barry L. Camp

  • Manu

    #2
    Re: .NET 2.0 Delete using GridView and ObjectDataSourc e

    Barry,

    You have to set the DataKeyName property in the GridView and delete the
    DataObjectTypeN ame in the ObjectDataSourc e (that property is to use
    custom objects as the parameter to the Delete method, and in your case
    you're using a guid so that does not apply).

    Also, delete the Type="Object" from the DeleteParameter .

    Hope it helps,
    Manuel Abadia


    Barry L. Camp wrote:
    Hi all,
    >
    Wondering if someone can help with a nagging problem I am having using
    a GridView and an ObjectDataSourc e. I have a simple situation where I
    am trying to delete a row from a table, but it doesn't seem to work at
    all. Below is my ASP.NET page, and further below, my VB.NET method that
    I am trying to call:
    >
    <div id="admin-faq" class="page">
    <h2>Site FAQs (Frequently Asked Questions)</h2>
    <asp:Panel ID="ListPanel" runat="server" Width="99%"
    Visible="true">
    <asp:GridView ID="GridView1" runat="server"
    AutoGenerateCol umns="False" CellPadding="4" width="99%"
    DataSourceID="O bjectDataSource 1">
    <EmptyDataTempl ate>
    Apparently, nobody asks any questions around here.
    </EmptyDataTempla te>
    <AlternatingRow Style BackColor="#E0E 0E0" />
    <Columns>
    <asp:CommandFie ld ShowDeleteButto n="True" />
    <asp:BoundFie ld DataField="ID" HeaderText="ID"
    SortExpression= "ID" DataFormatStrin g="{0}" />
    <asp:BoundFie ld DataField="Ques tion"
    HeaderText="Que stion" SortExpression= "Question" />
    <asp:BoundFie ld DataField="Answ er"
    HeaderText="Ans wer" SortExpression= "Answer" />
    <asp:BoundFie ld DataField="Auth or"
    HeaderText="Aut hor" SortExpression= "Author" />
    <asp:CheckBoxFi eld DataField="IsPu blic"
    HeaderText="IsP ublic" SortExpression= "IsPublic" />
    </Columns>
    </asp:GridView>
    </asp:Panel>
    </div>
    >
    <asp:ObjectData Source ID="ObjectDataS ource1" runat="server"
    DataObjectTypeN ame="System.Gui d" SelectMethod="G etFaqs"
    TypeName="FaqMa nager" DeleteMethod="R emoveFaq">
    <DeleteParamete rs>
    <asp:Paramete r Name="id" Type="Object" />
    </DeleteParameter s>
    </asp:ObjectDataS ource>
    >
    >
    Public NotInheritable Class FaqManager
    >
    ' Other methods left out, for clarity.
    >
    Public Shared Sub RemoveFaq(ByVal id As Guid)
    Using connection As New
    SqlConnection(C onfigurationMan ager.Connection Strings("blcamp ").ConnectionSt ring)
    Using command As New SqlCommand("Rem oveFaq", connection)
    command.Command Type = CommandType.Sto redProcedure
    command.Paramet ers.Add(New SqlParameter("@ FaqID", id))
    connection.Open ()
    command.Execute NonQuery()
    End Using
    End Using
    End Sub
    >
    Public Shared Function GetFaqs() As Generic.List(Of Faq)
    Using connection As New
    SqlConnection(C onfigurationMan ager.Connection Strings("blcamp ").ConnectionSt ring)
    Using command As New SqlCommand("Get Faqs", connection)
    command.Command Type = CommandType.Sto redProcedure
    Dim filter As Boolean = Not
    (HttpContext.Cu rrent.User.IsIn Role("Friends") Or
    HttpContext.Cur rent.User.IsInR ole("Administra tors"))
    command.Paramet ers.Add(New SqlParameter("@ IsPublic",
    filter))
    connection.Open ()
    Dim list As New Generic.List(Of Faq)()
    Using reader As SqlDataReader = command.Execute Reader()
    Do While (reader.Read())
    Dim temp As New Faq(CType(reade r("FaqID"),
    Guid), CType(reader("Q uestion"), String), CType(reader("A nswer"),
    String), CType(reader("A uthor"), String), CType(reader("I sPublic"),
    Boolean))
    list.Add(temp)
    Loop
    End Using
    Return list
    End Using
    End Using
    End Function
    >
    End Class
    >
    >
    When I try to run the above code as is, the RemoveFaq method executes,
    but the id parameter receives a value of <nothing>. Notice that
    currently, the GridView has no DataKeyNames attribute set. If I try to
    set it to Id, which is the primary key for my data as well as the
    Delete Parameter, I get the following exception:
    >
    Could not find a property named 'id' on the type specified by the
    DataObjectTypeN ame property in ObjectDataSourc e 'ObjectDataSour ce1'.
    >
    Does anyone out there know what I may be doing wrong here? Any help
    would really be appreciated. Thanks much,
    >
    Barry L. Camp

    Comment

    Working...