ObjectDataSource 's' could not find a non-generic method GetAllUsers
-
20-06-2021 - |
Question
I've looked over the web and have found loads of answers none of which have helped me resolve this issue!
I have a method in a data project. I then have a project with the displaying element.
The code-behind:
public static DataTable GetAllobjs(SPWeb objWeb)
{
DataTable tmpData = new DataTable();
try
{
allProjects.Columns.Add(ColumnNames.1);
allProjects.Columns.Add(ColumnNames.2);
allProjects.Columns.Add(ColumnNames.3);
allProjects.Columns.Add(ColumnNames.4);
allProjects.Columns.Add(ColumnNames.5);
allProjects.Columns.Add(ColumnNames.6);
allProjects.Columns.Add(ColumnNames.7);
allProjects.Columns.Add(ColumnNames.8);
allProjects.Columns.Add(ColumnNames.9);
//Get the raw project data
List<obj> data= DataAquisition.GetAllobj(objWeb);
/// Loop through the raw data.
foreach (obj currentInformation in data)
{
// Creates a new data row containing information required and adds it to the DATAtable
}
}
catch (Exception exc)
{
// Logs errors here
}
return tmpData;
}
In the page behind(cs) of the user control i have a Method that calls GetAllobjs(SPWeb objWeb)
[DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
public DataTable GetAllUsers()
{
return Data.GetAllobjs(SPContext.Current.Web);
}
Then the page load:
ObjectDataSource d = new ObjectDataSource();
d.ID = "s";
d.SelectMethod = "GetAllUsers";
d.TypeName = SPGridView.GetType().AssemblyQualifiedName;
this.Controls.Add(d);
/// Apply a data source to the SPGrid View
SPGridView.DataSourceID = d.ID;
Then on the create child control i create and bind my fields to the SPGridview and call the databind.
I thought this would work but i am getting the error. I'm not sure if i am missing some more component model [] tags somewhere i was hoping someone could possibly point out where i am going wrong.
Solution
Change:
d.TypeName = SPGridView.GetType().AssemblyQualifiedName;
to:
d.TypeName = this.GetType().AssemblyQualifiedName;
Example: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Q11454649WebApp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ObjectDataSource d = new ObjectDataSource();
d.ID = "s";
d.SelectMethod = "GetAllUsers";
//d.TypeName = SPGridView.GetType().AssemblyQualifiedName;
d.TypeName = this.GetType().AssemblyQualifiedName;
this.Controls.Add(d);
/// Apply a data source to the SPGrid View
SPGridView.DataSourceID = d.ID;
}
}
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
public string[] GetAllUsers()
{
return new string[] { "Joe", "Alan", "Michel" };
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="Q11454649WebApp._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="SPGridView" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Container.DataItem %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
archive project: Q11454649WebApp.7z
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow