Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Visual Studio 2008 For discussing Visual Studio 2008. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2008 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 September 8th, 2008, 08:38 PM
Registered User
Join Date: Aug 2008
Location: pasig, , Philippines.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Specified cast is not valid.

hello can anyone help me with this error?
thanks alot...

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.

Source Error:

Line 121: while (dr.Read())
Line 122: {
Line 123: memberItem.ID = dr.GetInt32(0);
Line 124: memberItem.memberCode = dr.GetString(1);
Line 125: memberItem.FirstName = dr.GetString(2);

Old September 8th, 2008, 09:15 PM
Friend of Wrox
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts

So which line is causing the problem???

I'm *guessing* (since you didn't tell us) that it is the line
    memberItem.ID = dr.GetInt32(0);

And that presumably means that the value in the database for that field (field zero) is not a valid integer.

Could be that the value in the DB is NULL. Could be that the value in the DB isn't an integer at all (e.g., could be a text field of some kind with values such as 'A17').

So... Time to go look carefully at your SQL query (the query that got you the DataReader) and then at the data in the DB.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Specified cast is not valid. Jose P C# 5 May 11th, 2007 08:04 AM
Specified cast is not valid surajb Crystal Reports 0 January 12th, 2007 02:03 PM
Specified cast is not valid. abstar BOOK: ASP.NET Website Programming Problem-Design-Solution 2 March 31st, 2005 03:00 PM
"Specified cast is not valid" help BaBaBooey ASP.NET 1.0 and 1.1 Basics 2 November 23rd, 2004 12:12 PM

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