472,805 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

.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 8481
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kjetil Klaussen | last post by:
Hi, I’m having some troubles trying to bind my dataset to a GridView control through an ObjectDataSource control. The binding works fine for regular columns in my dataset, but I can’t seem...
0
by: David Hubbard | last post by:
I am using a GridView to display a set of objects that have a parent-child relationship. Each object, MyBO, has an ID property that is used to get the children of that object. class MyBO { ...
1
by: Jürgen Bayer | last post by:
Hi, I just tried out the ObjectDataSource of ASP.NET 2.0. A simple application works with a GridView bound to an ObjectDataSource. The ObjectDataSource is set to a (factory) class...
3
by: tarscher | last post by:
Hi all, I have a grid that contains 7 columns from 3 tables (3 unique keys, 4 normal fields). I show this 7 columns on the gridview. I now want to add edit and delete functionality. This should...
0
by: Steve | last post by:
I have a gridview which uses an objectdatasource for its select and delete. The delete command uses the function below. The delete itself works but the extra logic which requires parameters...
8
by: Greg Lyles | last post by:
Hi all, I'm trying to develop an ASP.NET 2.0 website and am running into some real problems with what I thought would be a relatively simple thing to do. In a nutshell, I'm stuck on trying to...
3
by: pargat.singh | last post by:
I want to add delete button to grid view and pass all the clicked row parameters. Can someone please tell me how can i do this? Below are my codes. Thanks in advance. <asp:GridView...
1
by: rote | last post by:
Hi Guys, I have a simple Edit,Update Gridview and i'm using ObjectDatasource with dataset generated and TableAdapters I can do an update no problem.But can't get my delete to work.When i look at...
0
by: Rote Rote | last post by:
Hi Guys, I have a simple Edit,Update Gridview and i'm using ObjectDatasource with dataset generated and TableAdapters I can do an update no problem. But can't get my delete to work. When i look at...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.