473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 8525
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.