Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:09:04+00:00 2026-06-12T13:09:04+00:00

In my SQL Database I have a following view: CREATE VIEW MyView AS (SELECT

  • 0

In my SQL Database I have a following view:

CREATE VIEW MyView AS
(SELECT ChangeType FROM MyTable) UNION ALL
(SELECT NULL AS ChangeType FROM MyTable)

where ChangeType has type TINYINT. Then in my C# code I have a following class:

[Table(Name = "MyView")]
public class MyView
{
    [Column]
    public Nullable<int> ChangeType;
}

which is used as follows:

var table = dataContext.GetTable<MyView>();
var elements = table.ToArray();

and when this code runs I get the following exception:

[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlBuffer.get_Int32() +6393860
Read_MyView(ObjectMaterializer`1 ) +3404
System.Data.Linq.SqlClient.ObjectReader`2.MoveNext() +42
System.Linq.Buffer`1..ctor(IEnumerable`1 source) +488
System.Linq.Enumerable.ToArray(IEnumerable`1 source) +104
MyCodeHere

If I change the second part of UNION as follows:

(SELECT CONVERT(INT,NULL) AS ChangeType FROM MyTable)

the same code runs just fine.

Why does that “cast is not valid” there? Why won’t the TINYINT just be read in place of int?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:09:05+00:00Added an answer on June 12, 2026 at 1:09 pm

    Why won’t the TINYINT just be read in place of int?

    Because TINYINT maps to either short or sbyte.

    Edit: Looked it up and it’s an unsigned byte, so use byte.

    [Column]
    public Nullable<byte> ChangeType; 
    

    Your stack-trace shows that the the Linq2Sql mapper calls .get_Int32(). Assuming this is similar to SqlDataReader.GetInt32() then that means it will attempt to read a 32 bit value, no conversions are applied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL: SELECT * FROM [Database].dbo.[TagsPerItem] INNER JOIN [Database].dbo.[Tag] ON [Tag].Id
I have a following scenario in my SQL Server 2005 database. zipcodes table has
I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity
I have a list view which is populated from a SQL database. I am
I have a SQL database with has the following: Customer, Item, Clothing and Food.
I have the following 2 tables defined in a SQL Server database: CREATE TABLE
I have the following stored procedure in an SQL Server 2005 database (meant simply
I have a tabel in an Sql Server 2005 database. I have the following
I have the following SQL code that runs against a Change Request database. Each
I have the following PL/SQL stored procedure on an Oracle database: PROCEDURE MyProcedure (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.