3

I am encountering a problem when using LINQ in C#, I am constantly getting "Specified cast is not valid". This is what I am trying to do.

I create a class in which I declare all the columns of the table.

    [Table(Name="tbl_Aff")]
    public class Affiliate 
    {
        [Column]
        public string name;
        [Column]
        public string firstname;
        [Column]
        public string surname;
        [Column]
        public string title;
    }

I then declare a strongly typed DataContext in which I declare all Table collections as members of the context.

    public partial class Database : DataContext 
    {
        public Table<Affiliate> affiliate;

        public Database() : base(Settings.getConnectionString()) { } //This method gets the connection string by reading from an XML file.
    }
public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Database database = new Database();

            try
            {
                var q = from a in database.affiliate
                        select a;

                foreach (var aff in q) // Here I get the error "Specified cast is not valid"
                {
                    lblMessage.InnerHtml += aff.name + "
"; } } catch (Exception ex) { System.Console.WriteLine(ex.Message); } } }
4
  • 1
    Table<T> is a generic class - but your code appears to be declaring a variable of type Table. What's the exact type of affiliate? Also, what does the cast exception say in terms of what it's trying to cast from/to? Commented Jun 11, 2010 at 10:16
  • 1
    I have just edited my code forgot to format the code, sry. Table<T> is of type class Affiliate Table<Affiliate>. As regards to what the cast exception say, it says only "Specified cast is not valid" and nothing else.. Maybe I should catch the error in a different exception? Commented Jun 11, 2010 at 10:26
  • 2
    Please post the exception stack trace Commented Jun 11, 2010 at 10:29
  • 1
    at System.Data.SqlClient.SqlBuffer.get_Int32() at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i) at Read_Affiliate(ObjectMaterializer1 ) at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader2.MoveNext() at new_signup.Default.Page_Load(Object sender, EventArgs e) in C:\Users\m.buhagiar\Desktop\Signup_Solution\signupLogic\new_signup\Default.aspx.cs:line 28 Commented Jun 11, 2010 at 11:34

1 Answer 1

4

The GetInt32() suggests that at least one of the columns of tbl_Aff is not actually a string ([n]varchar(...)/[n]text), and you haven't given it any hints. The simplest trick here is to simply hake that property an int (since that is what it clearly wants).

I'm also wondering if there is more to Affiliate that you haven't shown; interfaces, base-classes, a separate partial class etc (as those columns look like they should be strings).

The main time this problem bites me is when I have (for example) a tinyint in the DB, and forget to type an enum accordingly (: byte in that case).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this.. I have manually checked that all columns in class Affiliate match the data types declared in the database, and found out that I was declaring some columns as int instead of Int16. Thanks once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.