CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2007
    Posts
    37

    CastException while reading data

    Code:
    using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True";
    
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = "select FirstName,LastName,ModifiedDate from Person.Contact order by Title;select * from HumanResources.Employee";
                        con.Open();
    
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
    //Error triggered here
                                Console.WriteLine("  {0} {1} - {2,18:D}", reader["FirstName"], reader[1], reader.GetDateTime(0));
                            }
                            Console.WriteLine(Environment.NewLine);
                            reader.NextResult();
                            Console.WriteLine("Employee Table Metadata");
                            for (int field = 0; field < reader.FieldCount; field++)
                            {
                                Console.WriteLine("Column Name : {0} Type : {1}", reader.GetName(field), reader.GetDataTypeName(field));
                            }
                        }
                    }
    and the error is:
    InvalidCastException was unhandled
    Specified cast is not valid

    also I tried
    Code:
    Console.WriteLine("  {0} {1} - {2,18:D}", (string)reader["FirstName"].ToString(), (string)reader[1].ToString(), reader.GetDateTime(0));
    it still showing me same error

    Help me!

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: CastException while reading data

    The problem is this statement:
    Code:
    reader.GetDateTime(0)
    try:
    Code:
    reader(0)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2007
    Posts
    37

    Re: CastException while reading data

    no the error is:
    Code:
    reader.GetDateTime(2)
    However thanks a lot for help!

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: CastException while reading data

    if youre on .net 2 you can make your life a lot easier by reading teh DW2 link in my signature
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured