I'm trying to code an ObjectDataSource for a local ASP.NET page.
Reading the Help files on MSDN always leave me scratching my head, wondering what exactly they might be implying. For example, I am not sure what my TypeName should be (though that link has an interesting example).
Once I get the basics to work, I will venture into deeper waters.
The first line of my *.aspx file contains my definitions:
<%@ Page Title="Reporter" Language="C#" MasterPageFile="~/local.Master"
AutoEventWireup="true" CodeBehind="Reporter.aspx.cs"
Inherits="AcpServiceNS.Reporter" %>
In this page, I have TextBox controls named txtStartDate
and txtEndDate
and a number of DropDownList controls named ddlStartTime
, ddlEndTime
, ddlAction
, ddlFilter1
, and ddlFilter2
.
I also have the following ObjectDataSource:
<asp:ObjectDataSource ID="dsReport" runat="server"
SelectMethod="GetData"
TypeName="System.Data.DataTable"
ConvertNullToDBNull="True" >
<SelectParameters>
<asp:ControlParameter ControlID="txtStartDate" Name="startDate" PropertyName="Text" Type="String" DefaultValue="" />
<asp:ControlParameter ControlID="ddlStartTime" Name="startTime" PropertyName="Text" Type="String" DefaultValue="" />
<asp:ControlParameter ControlID="txtEndDate" Name="endDate" PropertyName="Text" Type="String" DefaultValue="" />
<asp:ControlParameter ControlID="ddlEndTime" Name="endTime" PropertyName="Text" Type="String" DefaultValue="" />
<asp:ControlParameter ControlID="ddlAction" Name="action1" PropertyName="Text" Type="String" DefaultValue="" />
<asp:ControlParameter ControlID="ddlFilter1" Name="filter1" PropertyName="Text" Type="String" DefaultValue="" />
<asp:ControlParameter ControlID="ddlFilter2" Name="filter2" PropertyName="Text" Type="String" DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
A GridView control is going to be on the *.aspx page, and will be used to display the data:
<asp:GridView ID="gvReport" AllowSorting="true" AutoGenerateColumns="false" AllowPaging="true" Font-Size="Small" PageSize="30" Width="100%"
OnRowDataBound="Report_RowDataBound"
OnRowCommand="Report_RowCommand"
DataKeyNames="Op_ID,Serial_Number,Date_Time,Test_Result"
DataSourceID="dsReport"
runat="server">
<Columns>
<asp:TemplateField HeaderText="Op_ID" HeaderStyle-Width="20%">
<ItemTemplate>
<asp:LinkButton ID="lbOp_ID" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Op_ID" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Serial_Number" HeaderStyle-Width="20%">
<ItemTemplate>
<asp:LinkButton ID="lbSerial_Number" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Serial_Number" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Date_Time" HeaderText="Date_Time" SortExpression="Date_Time" HeaderStyle-Width="20%" />
<asp:BoundField DataField="Test_Result" HeaderText="Test_Result" SortExpression="Test_Result" HeaderStyle-Width="40%" />
</Columns>
</asp:GridView>
To get this to load an empty dataset, I have created this simple stub in my codebehind:
namespace AcpServiceNS {
public partial class Reporter : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
[DataObjectMethod(DataObjectMethodType.Select)]
protected static DataTable GetData(string startDate, string startTime, string endDate, string endTime,
string action1, string filter1, string filter2) {
var table = new DataTable();
table.Columns.Add("Op_ID", typeof(string));
table.Columns.Add("Serial_Number", typeof(string));
table.Columns.Add("Date_Time", typeof(DateTime));
table.Columns.Add("Test_Result", typeof(string));
return table;
}
}
}
Yes! All that is above is basically a Table Definition, but it should be enough to start my *.aspx page.
When I try to run this, I get the following Exception:
System.InvalidOperationException: ObjectDataSource 'dsReport' could not find a non-generic method 'GetData' that has parameters: startDate, startTime, endDate, endTime, action1, filter1, filter2.
It appears that I have spelled and cased all of the parameters correctly, so what have I done wrong? Is TypeName used incorrectly?
From the ObjectDataSource, I would say yes you're using TypeName
wrong. In the MSDN article, it refers to the class which hosts the method, not the return type of the method.
Try changing TypeName="System.Data.DataTable"
to TypeName="AcpServiceNS.Reporter"