Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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 January 7th, 2008, 07:47 PM
Authorized User
Join Date: Jan 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Unable to cast object of type

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;

public partial class UserDefinedFunctions
        [Microsoft.SqlServer.Server.SqlFunction(FillRowMeth odName="Obj_Row",
                                                TableDefinition = "ObjID int,ObjCategoryID int,ObjValue nvarchar(400)",
                                                DataAccess= DataAccessKind.Read)]

        public static IEnumerable Obj_IDs(SqlInt32 Data_1, SqlInt32 Data_2, SqlInt32 Data_3)
                using (SqlConnection conn = new SqlConnection("context connection=true"))
                        string sql;
                            case 1:
                             sql= @"Select Obj_ID, Obj_Categoy_ID, Obj_Value
                                    from (select ObjA_ID as Obj_ID, Obj_Category_ID, Obj_Value from dbo.function_A(12,23))A,
                                         (select ObjA_ID as Obj_ID, Obj_Category_ID, Obj_Value from dbo.function_A(15,27))B
                                    Where A.ObjA_ID= B.ObjA_ID
                                        and Obj_ID in (Select ObjB_ID from tbl_Object where Obj_CategoryID=10)";
                                sql = @"Select Obj_ID, Obj_Categoy_ID, Obj_Value from tbl_Object";

                            SqlCommand cmd = new SqlCommand(sql, conn);
                            SqlDataAdapter da = new SqlDataAdapter(cmd);
                            DataTable dt = new DataTable();
                            return dt.Rows;
                    catch (Exception ex)
                        return "";


        public static void Obj_Row(Object item, out int ObjID, out int ObjCategoryID, out string ObjValue)
            DataRow row = (DataRow)item;
            ObjID = Convert.ToInt32(row["Obj_ID"]);
            ObjCategoryID = Convert.ToInt32(row["Obj_Categoy_ID"]);
            ObjValue = row["Obj_Value"].ToString();


compile with no error. call function in SQL studio the error come

Msg 6260, Level 16, State 1, Line 1
An error occurred while getting new row from user defined Table Valued Function :
System.InvalidCastException: Unable to cast object of type 'System.Char' to type 'System.Data.DataRow'.
at UserDefinedFunctions.Object_Row(Object item, Int32& ObjID, Int32& ObjCategoryID, String& ObjValue)
when I change SQL script to simple not "nested select" it work fine. How to get out of this kind of trouble????!!!!
Old January 8th, 2008, 11:17 PM
Authorized User
Join Date: Jan 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts

Look like I get to dead point. It's funny the code didn’t works because complex SQL script.
I'm already trying over 35 SQLs script.
Simple SQL work fine.
Complex SQL get stuck.

Old January 9th, 2008, 08:01 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi

Stop crossposting please!

anyway, maybe your problem is with your Query.. No matter how complex it could be, it should work...

please refer to your original post, and if you want a little more help, write a script that creates the tables and fill a little data in it and I will try it in my SQL server here..



================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
================================================== =========
I know that CVS was evil, and now i got the proof:
================================================== =========
Old January 9th, 2008, 01:31 PM
Authorized User
Join Date: Jan 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts

To Admin,

Please delete this topic, I'm return to my original post.


Dear Gonzalo,

I'll do it. My data little funny.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to cast object of type 'TheBeerHouseSection DyerOppenheimer BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 June 10th, 2008 12:13 PM
Receiving Error Msg:Unable to cast object of type cesemj ASP.NET 2.0 Basics 2 March 4th, 2008 02:18 PM
problem in cast of type yoord SQL Language 1 October 18th, 2004 03:19 PM

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