B
Barry L. Camp
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>
<aspanel 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>
</aspanel>
</div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="System.Guid" SelectMethod="GetFaqs"
TypeName="FaqManager" DeleteMethod="RemoveFaq">
<DeleteParameters>
<asparameter 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.ConnectionStrings("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.ConnectionStrings("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
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>
<aspanel 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>
</aspanel>
</div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="System.Guid" SelectMethod="GetFaqs"
TypeName="FaqManager" DeleteMethod="RemoveFaq">
<DeleteParameters>
<asparameter 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.ConnectionStrings("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.ConnectionStrings("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