tags:

views:

1275

answers:

3

Hello guys. I have problem when i using ListView and Linq as datasource. The error down:

 Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.


System.Data.SqlClient.SqlBuffer.get_Int64() +58
   System.Data.SqlClient.SqlDataReader.GetInt64(Int32 i) +38
   Read_ForumThreadPostDetail(ObjectMaterializer`1 ) +95
   System.Data.Linq.SqlClient.ObjectReader`2.MoveNext() +29
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +96
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +7667556
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +61

Source code

Public IEnumerable<IForumThreadPost> GetForumPostByThreadAndPost()
{
    ScoutDataDataContext sd = new ScoutDataDataContext();
    long ThreadId = Convert.ToInt64(HttpContext.Current.Request.QueryString["id"]);
    long PostId = Convert.ToInt64(HttpContext.Current.Request.QueryString["postId"]);
    ///.Skip((pageIndex - 1)*pageSize).Take(pageSize) + int pageIndex, int pageSize
    return sd.ForumThreadPostDetails
       .AsEnumerable()
       .Where(f => f.ThreadId.Equals(ThreadId) && f.PostId.Equals(PostId))
       .Select(f => 
         new IForumThreadPost 
         { 
         Id = f.Id, 
         ThreadId = f.ThreadId, 
         PostId = f.PostId, 
         Title = f.Title, 
         ThreadTitle = f.ThreadTitle, 
         Content = f.Content, 
         UserFullName = f.UserFullName, 
         UserId = f.UserId 
        }).ToList(); // error here

}

This function has work before, so i don't can figure out what the problem is. Thanks for your help.

A: 

Without seeing more of the code or database structure, it will be hard to come to a great solution. Seeing the ForumThreadPostDetails table and generated LinqToSql class would be ideal.

Are the ThreadId and PostId both 'BigInt's in the database?

Do the types match up between the properties within IForumThreadPost and ForumThreadPostDetails (and does the details class inherit from this interface, if it even is an interface)?

Have any changes been made to the ForumThreadPostDetails table in the database (fields changing type, etc)?

Why are you calling .AsEnumerable() on your table?

Ryan Versaw
A: 

The problem was as in the database the Id was int any in the code it was long.

Frozzare
A: 

A can see a few things here. ichiban is correct, change the IForumThreadPost to whatever it should be (ForumThreadPost ???).

Also a friend told me once you should use the most simple objects possible. Because you are using a Listview, I would consider changing IEnumerable to an array.

Eg:

Public ForumThreadPost[] GetForumPostByThreadAndPost()
{
    ScoutDataDataContext sd = new ScoutDataDataContext();
    var result = sd.ForumThreadPostDetails
          .Where(f => f.ThreadId.Equals(ThreadId) && f.PostId.Equals(PostId))
          .Select(f => 
                   new ForumThreadPost 
                   { 
                          Id = f.Id, 
                          ThreadId = f.ThreadId, 
                          PostId = f.PostId, 
                          Title = f.Title, 
                          ThreadTitle = f.ThreadTitle, 
                          Content = f.Content, 
                          UserFullName = f.UserFullName, 
                          UserId = f.UserId 
                  });

    return result.ToArray();

}
Christian Payne