# DataRow displaying the value compared to using it

**URL:** <https://forums.powershell.org/t/datarow-displaying-the-value-compared-to-using-it/21039>\
**Category:** PowerShell Help\
**Created:** [November 29, 2022, 2:53pm UTC](https://forums.powershell.org/t/datarow-displaying-the-value-compared-to-using-it/21039 "2022-11-29T14:53:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mg2017](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mg2017](https://forums.powershell.org/u/mg2017)\
**Post date:** [November 29, 2022, 2:53pm UTC](https://forums.powershell.org/t/datarow-displaying-the-value-compared-to-using-it/21039/1 "2022-11-29T14:53:46Z")

</div>

Fellow Powershellers.  
I am trying to understand why powershell behaves in a specific way. Please read the comments in the code.

```auto
	$OtherDB_connection = New-Object System.Data.OleDb.OleDbConnection $OtherDB_connectionString
         $OtherDB_command = New-Object System.Data.OleDb.OleDbCommand $QeuryOtherInstance,$OtherDB_connection
         $OtherDB_command.CommandTimeout = 300
         $OtherDB_connection.Open()
         $OtherDB_adapter = New-Object System.Data.OleDb.OleDbDataAdapter $OtherDB_command
         $OtherDB_dataset = New-Object System.Data.DataSet
         $OtherDB_recordcount = $OtherDB_adapter.Fill($OtherDB_dataSet)
         $OtherDB_CMdata = $OtherDB_dataset.Tables[0]
         $OtherDB_connection.Close() 
	foreach ($OtherDB_row2 in $OtherDB_CMdata)
         { 
		$OtherDB_rows.InstanceKeyID #this displays perfectly
		$mystring = " Insert into dbo.TEMP_InstanceDBDriver (InstanceKeyID) Select $OtherDB_rows.InstanceKeyID"
		
		#the above SELECT does not display the value but delivers System.Data.DataRow.Item("InstanceKeyID") when I display the $mystring to the output
                #to make this work like one would think it should work, I have to move the original value into a variable and use the variable.

		#for example, below works.
		$mykey = $OtherDB_rows.InstanceKeyID
		$mystring = " Insert into dbo.TEMP_InstanceDBDriver (InstanceKeyID) Select $mykey"

	}	

	#what do I have to do to reference the value in $OtherDB_rows.InstanceKeyID in teh SELECT string I am building without moving it into a variable?

```

Thanks Mark

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 29, 2022, 3:07pm UTC](https://forums.powershell.org/t/datarow-displaying-the-value-compared-to-using-it/21039/2 "2022-11-29T15:07:13Z")

</div>

> [@mg2017](#):
>
> ```auto
> #for example, below works.
> $mykey = $OtherDB_rows.InstanceKeyID
> $mystring = " Insert into dbo.TEMP_InstanceDBDriver (InstanceKeyID) Select $mykey"
> 
> }	
> 
> #what do I have to do to reference the value in $OtherDB_rows.InstanceKeyID in teh SELECT string I am building without moving it into a variable?
> 
> ```

```auto
# I am actually wondering that your SQL query string works since it is actually build wrong. PowerShell expends variable instring in a special way. If you want to expand particular properties you can use the subexpression operator for proper variable expansion in quoted strings.
"Insert into dbo.TEMP_InstanceDBDriver (InstanceKeyID) Select $($OtherDB_rows.InstanceKeyID)"

```

---

<div class="post-metadata">

**Author:** ![mg2017](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mg2017](https://forums.powershell.org/u/mg2017)\
**Post date:** [November 29, 2022, 3:11pm UTC](https://forums.powershell.org/t/datarow-displaying-the-value-compared-to-using-it/21039/3 "2022-11-29T15:11:51Z")

</div>

Hi Olaf.  
Actually my SQL string is correct - it works perfectly using the variable. I have been using this appraoch for some time. But obviously inefficient. I would rather not have to make variables and then use the variables in the select string  
So, I will try your $($OtherDB\_rows.InstanceKeyID) appraoch.

Will keep you posted. Many thanks. Mark

---

<div class="post-metadata">

**Author:** ![mg2017](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mg2017](https://forums.powershell.org/u/mg2017)\
**Post date:** [November 29, 2022, 4:51pm UTC](https://forums.powershell.org/t/datarow-displaying-the-value-compared-to-using-it/21039/4 "2022-11-29T16:51:33Z")

</div>

Hi olaf,  
You suggestion works great. Many thanks.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/datarow-displaying-the-value-compared-to-using-it/21039/5 "2024-05-16T20:16:17Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


