Re: ObjectDataSource and Linq. Please, help! I don't know what else to try. Thank You.



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
.



Relevant Pages

  • Re: HTML doc save dialog
    ... After getting the string from database do not set the string to ... > and end html tags. ... In the document complete event of browser, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ObjectDataSource and Linq. Please, help! I dont know what else to try. Thank You.
    ... Public Shared Function GetTags() As ICollection ... Dim database As New LabDataContext ... Dim tags = From t In database.Tags _ ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help needed on custom tag
    ... > I need to develop a Custom tag that retrieves the values from ... > two database tables. ... > here display data from second table here display data from second table ... Although I wouldn't recommend it for production the JSTL tags provide ...
    (comp.lang.java.developer)
  • [Help] Struts/JSP -- easy way associate an "ID" to a "Value"?
    ... I'm using "ID" and "Value" in the generic sense here... ... to be saved to a database (as a reference in a table ... this with JSP tags? ... Page2ActionForm property "ID" as a value in a inside ...
    (comp.lang.java)
  • [Help] Struts/JSP -- easy way associate an "ID" to a "Value"?
    ... to be saved to a database (as a reference in a table ... Now, I have easy access to the "ID" on the Page2ActionForm, but I ... this with JSP tags? ... Page2ActionForm property "ID" as a value in a inside ...
    (comp.lang.java.help)