Answered Invalid Cast AsEnumberable()

  • Wednesday, April 04, 2012 10:44 AM
     
      Has Code
    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace ConsAppAutoGenSql
    {
        class Program
        {
            static void Main(string[] args)
            {
                SqlConnection conn = new SqlConnection(@"Data Source=localhost\sqlexpress;Initial Catalog=Northwind;Integrated Security=True");
    
                SqlCommand command = new SqlCommand("sp_sproc_columns", conn);
                command.CommandType = CommandType.StoredProcedure;
    
                SqlParameter prmSprocName = new SqlParameter("@procedure_name",SqlDbType.NVarChar, 384);
                Console.WriteLine("enter name of sproc");
                prmSprocName.Value = Console.ReadLine();
    
                command.Parameters.Add(prmSprocName);
    
                SqlDataAdapter da = new SqlDataAdapter(command);
                DataTable dt = new DataTable();
    
                try 
    	        {	        
    		        conn.Open();
    
                    da.Fill(dt);
    
                    var x = from myRow in dt.AsEnumerable()
                            where myRow.Field<int>("COLUMN_TYPE") == 1
                            select myRow;
                    
                    foreach (DataRow aRow in x)
                    {                    
                        Console.WriteLine(aRow.Field<string>("COLUMN_NAME"));
                    }
    
                    Console.ReadLine();
    	        }
    	        catch (Exception ex)
    	        {
                    throw ex;
    	        }
                finally
                {
                    conn.Close();
                }
                           
            }
        }
    }

    I am getting the following runtime exception;

    System.InvalidCastException was caught
      Message=Specified cast is not valid.
      Source=System.Data.DataSetExtensions
      StackTrace:
           at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
           at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
           at ConsAppAutoGenSql.Program.<Main>b__0(DataRow myRow) in C:\Users\xxx\Documents\Visual Studio 2010\Projects\ConsoleApps\ConsAppAutoGenSql\ConsAppAutoGenSql\Program.cs:line 32
           at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
           at ConsAppAutoGenSql.Program.Main(String[] args) in C:\Users\xxx\Documents\Visual Studio 2010\Projects\ConsoleApps\ConsAppAutoGenSql\ConsAppAutoGenSql\Program.cs:line 35
      InnerException: 

    Can someone help, please?


    UML, then code


All Replies

  • Thursday, April 05, 2012 1:58 AM
    Moderator
     
     Answered

    Hi Berlioz99,

    Welcome!

    Please check the type of column "COLUMN_TYPE" to see whether it is "int". Your query and Loop look right.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked As Answer by Danny Rosales Friday, April 13, 2012 11:07 PM
    •  
  • Tuesday, April 10, 2012 7:54 AM
    Moderator
     
     

    Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Tuesday, April 10, 2012 9:05 AM
     
     

    Hello,

    I agree with Alan please check the data type of the field "COLUMN_TYPE" I think there some issue there.

  • Friday, April 13, 2012 11:08 PM
     
     
    Yes, the column data type was incorrect...Thank you

    UML, then code