Returning data table gives system.obect[]

Hi,
i am trying to return data table from a function to a variable. This is what I did:

function get-data{

#connection open and using sql command with stored procedure and parameters to that stored #procedure

# opening adapter and retuning data table.

# when I try for get type, it shows as it is sending data table

[System.Data.DataTable] $dt = New-Object System.Data.DataTable

   $sqlDataAdapter.Fill($dt) | Out-Null 
   $sqlConnection.Close()
    
    

   return ,$dt

}

$returnAccounts = get-data
$returnAccounts.GetType().FullName

Why I am getting system.object type.
Here is the output:
it is giving me all stored procedure parameters list in addition to data

CompareInfo : None
XmlSchemaCollectionDatabase :
XmlSchemaCollectionOwningSchema :
XmlSchemaCollectionName :
ForceColumnEncryption : False
DbType : String
LocaleId : 0
ParameterName : @pStartDate
Precision : 0
Scale : 0
SqlDbType : NVarChar
SqlValue : 2025-06-01
UdtTypeName :
TypeName :
Value : 2025-06-01
Direction : Input
IsNullable : False
Offset : 0
Size : 10
SourceColumn :
SourceColumnNullMapping : False
SourceVersion : Current

why is it happening. Do I have to loop through and check for datarow type and add to new table?

TIA

Get rid of the comma in

return ,$dt

Hi,
Thanks for the response. I tried without comma first. It didn’t work. Someone suggested to use it. I tried both. None working.

spunny,
Welcome to the forum. :wave:t3:

Please go back, edit your question once again and fix the formatting of your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

Guide to Posting Code - Redux <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

This is usually a hint that you get an array, a list, a collection instead of a single object. I’d try to cast the variable explicitly for an array and inspect what’s in it. :man_shrugging:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.