Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 May 10th, 2007, 07:00 PM
Registered User
 
Join Date: May 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Specified cast is not valid.

Hi I am writing this method but get an error that says "Specified cast is not valid. " in the "Books books = new Books((string)reader[0], (string)reader[1]," line. Anyone pls suggests me.

public List<Books> GetBooks()
        {
            List<Books> bookList = new List<Books>();
            SqlConnection myConnection = new SqlConnection(connectionString);
            SqlCommand myCommand = new SqlCommand("SPGetAllBooks", myConnection);
            myConnection.Open();
            SqlDataReader reader = myCommand.ExecuteReader();
            while (reader.Read())
            {
                Books books = new Books((string)reader[0], (string)reader[1],
                    (string)reader[2], (string)reader[3], (int)reader[4]
                    ,(double)reader[5], (int)reader[6], (int)reader[7]
                    ,(string)reader);
                bookList.Add(books);
            }
            myConnection.Close();
            reader.Close();
            myCommand.Dispose();
            if (bookList != null && bookList.Count > 0)
                return bookList;
            else return null;
        }

The following is my constructor in Books class :
  public Books(string bookID, string title, string authorFirstname
            , string authorLastname, int edition, double price,
            int discount, int amount, string category)

 
Old May 10th, 2007, 10:07 PM
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont think, that you have to cast the outpur of the reader object. You can simply type
Books books = new Books(reader[0], reader[1], reader[2],
                        reader[3], reader[4], reader[5],
                        reader[6], reader[7],reader);

Just try, it will work

-Pallav

 
Old May 11th, 2007, 12:22 AM
Registered User
 
Join Date: May 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried that then I got this message instead.

Argument '1': cannot convert from 'object' to 'string'


 
Old May 11th, 2007, 01:57 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You needn't worry too much over the string casts but the others may not work. What data types are declared in the database for the fields returned in the reader?

--

Joe (Microsoft MVP - XML)
 
Old May 11th, 2007, 02:56 AM
Registered User
 
Join Date: May 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, That is the point. In my DB I set the type of Price column to money. But I created Price in Books class as Double as following:
        public Books(string bookID, string title, string authorFirstname
            , string authorLastname, int edition, double price,
            int discount, int amount, string category)...
next, tried to cast this "(double)reader[5]".....what do u think could be the possible solution?

 
Old May 11th, 2007, 08:04 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to cast every variable from the datareader to the appropriate type (unless the type is 'Object').

So for this constructor signature:

    public Books(string bookID, string title, string authorFirstname
            , string authorLastname, int edition, double price,
            int discount, int amount, string category)

you will at least need something like this:

Books books = new Books(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(),
                        reader[3].ToString(), (int)reader[4], (double)reader[5],
                        (int)reader[6], (int)reader[7], reader.ToString());

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Specified cast is not valid. ayem Visual Studio 2008 1 September 8th, 2008 09:15 PM
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.