470,641 Members | 2,555 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

.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 ObjectDataSource. 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"
AutoGenerateColumns="False" CellPadding="4" width="99%"
DataSourceID="ObjectDataSource1">
<EmptyDataTemplate>
Apparently, nobody asks any questions around here.
</EmptyDataTemplate>
<AlternatingRowStyle BackColor="#E0E0E0" />
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" DataFormatString="{0}" />
<asp:BoundField DataField="Question"
HeaderText="Question" SortExpression="Question" />
<asp:BoundField DataField="Answer"
HeaderText="Answer" SortExpression="Answer" />
<asp:BoundField DataField="Author"
HeaderText="Author" SortExpression="Author" />
<asp:CheckBoxField DataField="IsPublic"
HeaderText="IsPublic" SortExpression="IsPublic" />
</Columns>
</asp:GridView>
</asp:Panel>
</div>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="System.Guid" SelectMethod="GetFaqs"
TypeName="FaqManager" DeleteMethod="RemoveFaq">
<DeleteParameters>
<asp:Parameter Name="id" Type="Object" />
</DeleteParameters>
</asp:ObjectDataSource>
Public NotInheritable Class FaqManager

' Other methods left out, for clarity.

Public Shared Sub RemoveFaq(ByVal id As Guid)
Using connection As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("blcamp").ConnectionString)
Using command As New SqlCommand("RemoveFaq", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add(New SqlParameter("@FaqID", id))
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
End Sub

Public Shared Function GetFaqs() As Generic.List(Of Faq)
Using connection As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("blcamp").ConnectionString)
Using command As New SqlCommand("GetFaqs", connection)
command.CommandType = CommandType.StoredProcedure
Dim filter As Boolean = Not
(HttpContext.Current.User.IsInRole("Friends") Or
HttpContext.Current.User.IsInRole("Administrators" ))
command.Parameters.Add(New SqlParameter("@IsPublic",
filter))
connection.Open()
Dim list As New Generic.List(Of Faq)()
Using reader As SqlDataReader = command.ExecuteReader()
Do While (reader.Read())
Dim temp As New Faq(CType(reader("FaqID"),
Guid), CType(reader("Question"), String), CType(reader("Answer"),
String), CType(reader("Author"), String), CType(reader("IsPublic"),
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
DataObjectTypeName property in ObjectDataSource 'ObjectDataSource1'.

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

Barry L. Camp

Jan 7 '07 #1
1 8358
Barry,

You have to set the DataKeyName property in the GridView and delete the
DataObjectTypeName in the ObjectDataSource (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
http://www.manuelabadia.com

Barry L. Camp wrote:
Hi all,

Wondering if someone can help with a nagging problem I am having using
a GridView and an ObjectDataSource. 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"
AutoGenerateColumns="False" CellPadding="4" width="99%"
DataSourceID="ObjectDataSource1">
<EmptyDataTemplate>
Apparently, nobody asks any questions around here.
</EmptyDataTemplate>
<AlternatingRowStyle BackColor="#E0E0E0" />
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" DataFormatString="{0}" />
<asp:BoundField DataField="Question"
HeaderText="Question" SortExpression="Question" />
<asp:BoundField DataField="Answer"
HeaderText="Answer" SortExpression="Answer" />
<asp:BoundField DataField="Author"
HeaderText="Author" SortExpression="Author" />
<asp:CheckBoxField DataField="IsPublic"
HeaderText="IsPublic" SortExpression="IsPublic" />
</Columns>
</asp:GridView>
</asp:Panel>
</div>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="System.Guid" SelectMethod="GetFaqs"
TypeName="FaqManager" DeleteMethod="RemoveFaq">
<DeleteParameters>
<asp:Parameter Name="id" Type="Object" />
</DeleteParameters>
</asp:ObjectDataSource>
Public NotInheritable Class FaqManager

' Other methods left out, for clarity.

Public Shared Sub RemoveFaq(ByVal id As Guid)
Using connection As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("blcamp").ConnectionString)
Using command As New SqlCommand("RemoveFaq", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add(New SqlParameter("@FaqID", id))
connection.Open()
command.ExecuteNonQuery()
End Using
End Using
End Sub

Public Shared Function GetFaqs() As Generic.List(Of Faq)
Using connection As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("blcamp").ConnectionString)
Using command As New SqlCommand("GetFaqs", connection)
command.CommandType = CommandType.StoredProcedure
Dim filter As Boolean = Not
(HttpContext.Current.User.IsInRole("Friends") Or
HttpContext.Current.User.IsInRole("Administrators" ))
command.Parameters.Add(New SqlParameter("@IsPublic",
filter))
connection.Open()
Dim list As New Generic.List(Of Faq)()
Using reader As SqlDataReader = command.ExecuteReader()
Do While (reader.Read())
Dim temp As New Faq(CType(reader("FaqID"),
Guid), CType(reader("Question"), String), CType(reader("Answer"),
String), CType(reader("Author"), String), CType(reader("IsPublic"),
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
DataObjectTypeName property in ObjectDataSource 'ObjectDataSource1'.

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

Barry L. Camp
Jan 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kjetil Klaussen | last post: by
1 post views Thread by Jrgen Bayer | last post: by
3 posts views Thread by tarscher | last post: by
8 posts views Thread by Greg Lyles | last post: by
3 posts views Thread by pargat.singh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.