Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2007, 01:28 PM
Authorized User
 
Join Date: Jul 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Casting an SQL smallint to an int in the DAL

I was running into a problem trying to convert an SQL Server smallint column type to an int in my DAL Provider class. Here is a snippet of my code that was failing (the names are different but the pattern is the same as outlined in the book):

Code:
protected virtual SupplementalSheetPlayerDetails GetSupplementalSheetPlayerFromReader(IDataReader reader)  {
  SupplementalSheetPlayerDetails supplementalSheetPlayer = new SupplementalSheetPlayerDetails((int)reader["SupplementalSheetID"],(int)reader["PlayerID"],(int)reader["Seqno"]);
  return supplementalSheetPlayer;
}
In my database, the 'Seqno' column is declared as a smallint, but when I cast it as an integer it causes a runtime error. I solved the error by instead casting it as a short (even though the constructor for the SupplementalSheetPlayerDetails object accepts an integer for the Seqno property) and that seemed to work, but I'm still not sure why I'm getting a casting error in the first place.

Any ideas on why I can cast a smallint as a short but not an int, and also why the smallint is then implicitly cast to an int in the constructor of the entity object?

Thanks.

 
Old August 1st, 2007, 10:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Did you try the Convert class? Maybe:
  int i = Convert.ToInt32(val);

Aren't smallint and short both 16 bits wide?

 
Old August 4th, 2007, 11:54 PM
Authorized User
 
Join Date: Jul 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'll give that a shot, but I thought the whole idea of casting is to convert to a new type; why you can't convert from a 16-bit number to a 32-bit number is beyond me. Thanks for the advice.






Similar Threads
Thread Thread Starter Forum Replies Last Post
BLL and DAL kss BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 13 November 24th, 2008 03:59 PM
Help with BLL and DAL kss BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 November 20th, 2008 09:23 PM
Important changes to DAL jimibt BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 May 16th, 2008 10:26 AM
Cast varchar as int SQL Server 2005 mr_pc1963 SQL Language 2 April 23rd, 2008 06:09 AM
Error converting data type varchar to int. - SQL baysaa SQL Server ASP 1 March 16th, 2008 11:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.