Re: ObjectDataSource and Linq. Please, help! I don't know what else to try. Thank You.
- From: shapper <mdmoura@xxxxxxxxx>
- Date: Fri, 22 Feb 2008 07:53:53 -0800 (PST)
On Feb 22, 7:40 am, Jon Skeet [C# MVP] <sk...@xxxxxxxxx> wrote:
shapper <mdmo...@xxxxxxxxx> wrote:
I used a "Linq to SQL classes" in VS 2008 to map the tables of a
database: MyDbDataContext
One of the tables is named Tags and has 2 columns: TagID and Text.
This table is related to a second table named Articles through a table
ArticlesTags.
I would like to use an ObjectDataSource as a layer between my data and
a ListView.
In my page I created the following:
private void odsTags_Init(object sender, EventArgs e)
{
odsTags.ID = "odsTags";
odsTags.SelectMethod = "GetTags";
odsTags.TypeName = "MyDbDataContext";
}
public static ICollection GetTags()
{
MyDbDataContext database = new MyDbDataContext();
var query = from t in db.Tags
select new
{
TagId = t.TagId,
TagText = t.TagText,
Active = t.ArticlesTags.Any
};
return tags;
}
That's not your actual code - note the value returned from GetTags().
I get the following error:
ObjectDataSource 'odsTags' could not find a non-generic method
'GetTags' that has no parameters.
Is your type name really just MyDbDataContext with no namespace? That's
the first thing to try.
--
Jon Skeet - <sk...@xxxxxxxxx>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk
Yes, it is my code!
I just did a mistake when converting from VB.NET, which I am using for
this example, to C#.
I now create a sample which is only a web site with Sample.Aspx and
LabDataContext.
I always get the same error ...
Could someone please provide me a work example. I am going crazy.
The only way I was able to make this work was doing the following:
I placed the GetTags inside "Extensibility Method Definitions" region
in LabDataContext class using the following:
<DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
Public Shared Function GetTags() As IQueryable
Dim database As New LabDataContext
Dim tags = From t In database.Tags _
Select t.TagID, _
t.Text, _
Active = t.ArticlesTags.Any
Return tags
End Function ' GetTags
Now I get the following error:
ListView with id 'ListView1' must have a data source that either
implements ICollection or can perform data source paging if
AllowPaging is true.
Then I changed to:
<DataObjectMethodAttribute(DataObjectMethodType.Select, True)> _
Public Shared Function GetTags() As ICollection
Dim database As New LabDataContext
Dim tags = From t In database.Tags _
Select t.TagID, _
t.Text, _
Active = t.ArticlesTags.Any
Return tags.ToList
End Function ' GetTags
Now it works but I am not sure if this is the right way to do this.
Any idea?
I would prefer to have the GetTags methods outside the DataContext.
Finally, I tried to add a DeleteMethod to my ObjectDataSource:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetTags" DeleteMethod="DeleteTag"
TypeName="LabDataContext">
<DeleteParameters>
<asp:Parameter Type="Object" Name="Tag"></asp:Parameter>
</DeleteParameters>
</asp:ObjectDataSource>
And I get the following error:
ObjectDataSource 'ObjectDataSource1' could not find a non-generic
method 'DeleteTag' that has parameters: Tag, TagID.
I am using the "Tag" delete parameter because I was thinking in using
the default method inside "Extensibility Method Definitions" region in
LabDataContext class:
Partial Private Sub DeleteTag(ByVal instance As Tag)
End Sub
I have been Goggling for information on how to implement
ObjectDataSource with a Linq to SQL classes but until now I wasn't
able to find anything.
Could someone, please, advise me on the issues I just posted.
Thank You,
Miguel
.
- Follow-Ups:
- Re: ObjectDataSource and Linq. Please, help! I don't know what else to try. Thank You.
- From: Jon Skeet [C# MVP]
- Re: ObjectDataSource and Linq. Please, help! I don't know what else to try. Thank You.
- References:
- Prev by Date: Re: Threading WinForm
- Next by Date: Re: ObjectDataSource and Linq. Please, help! I don't know what else to try. Thank You.
- Previous by thread: Re: ObjectDataSource and Linq. Please, help! I don't know what else to try. Thank You.
- Next by thread: Re: ObjectDataSource and Linq. Please, help! I don't know what else to try. Thank You.
- Index(es):
Relevant Pages
|