DataGridView works well when multiple records are passed and errors out when a single record is passed

Hi,

Am in the process of building a GUI interface with the help of Powershell Studio 2014 that displays data from xml file.

My Requirement:

1. Clicking on the Browse button on the windows form will open a file dialog box for selecting the xml file.

XML File:

<?xml version="1.0"?>
<root>
<Employee>
  <Records>
    <Record>
      <EmpID>100</EmpID>
      <EmpName>Naveen</EmpName>
      <EmpAddress><![CDATA[#10, TNHB, EWS QUARTERS (PO), INDIA]]></EmpAddress>
      <CountryID>7</CountryID>
    </Record>
    <Record>
      <EmpID>101</EmpID>
      <EmpName>Kumar</EmpName>
      <EmpAddress><![CDATA[#29, T.R Nagar, EWS QUARTERS (PO), INDIA]]></EmpAddress>
      <CountryID>7</CountryID>
    </Record>
    <Record>
      <EmpID>102</EmpID>
      <EmpName>Nagareddy</EmpName>
      <EmpAddress><![CDATA[#201, Lake View, EWS QUARTERS (PO), INDIA]]></EmpAddress>
      <CountryID>7</CountryID>
    </Record>
  </Records>
</Employee>
<Country>
  <Records>
    <Record>
      <CountryID>7</CountryID>
      <CountryName>India</CountryName>
      <CountryGUID><![CDATA[4e6wiyj2gqwbuyswx92pfvvams5pppjw@]]></CountryGUID>
    </Record>
  </Records>
</Country>
</root>

2. List of tables are populated to combo-box, DropDownStyle property is DropDownList.

3. Data is displayed in the DataGridView control when a table is selected from DropDown.

4. DataGridView works better if there are multiple records in table for example Employee table in XML file and errors out for a single record for example Country table in XML file.

Combo-box script:

$combobox1_SelectedIndexChanged={
  $array = New-Object System.Collections.ArrayList
  if ($combobox1.Text)
  {
    $tablename = $combobox1.Text
    $index = [array]::IndexOf($global:tablelist, $tablename)
    $Results = foreach ($x in $global:xml.root.($global:tablelist[$index]).Records.Record)
    {
      $cols = $x.childnodes.localname
      $props = New-Object PSObject
      For ($i = 0; $i -lt $cols.count; $i++)
      {
        $props | Add-Member $cols[$i] $(If (($x.($cols[$i]).innertext) -or ($x.($cols[$i]).innertext -eq "")) { $($x.($cols[$i]).innertext) }
      else { $x.($cols[$i]) })
    }$props
  }
  $array.AddRange($Results)
  $datagridview1.DataSource = $array
 }	
}

Gives out below error for Country table

ERROR: Cannot convert argument "c", with value: "@{CountryID=7; CountryName=India; CountryGUID=4e6wiyj2gqwbuyswx92pfvvams5pppjw@}", for "AddRange" to type
ERROR: "System.Collections.ICollection": "Cannot convert the "@{CountryID=7; CountryName=India; CountryGUID=4e6wiyj2gqwbuyswx92pfvvams5pppjw@}" value of type
ERROR: "System.Management.Automation.PSCustomObject" to type "System.Collections.ICollection"."
Detailed Info Test.psf (153): ERROR: At Line: 153 char: 4
ERROR: +                 $array.AddRange($Results)
ERROR: +                 ~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : NotSpecified: (:) [], MethodException
ERROR:     + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
ERROR:

Let me know for full code.

Please kindly suggest me in overcoming the above error.

Thanks in Advance.

Regards,

Naveen. N

July 29th, 2014 6:56pm

Try this:

$array.AddRange(@($Results))

Wrapping your results in a @() forces an array, even when $Results is just a single object.

  • Marked as answer by Naveen N Tuesday, July 29, 2014 7:12 PM
Free Windows Admin Tool Kit Click here and download it now
July 29th, 2014 7:03pm

Try this:

$array.AddRange(@($Results))

Wrapping your results in a @() forces an array, even when $Results is just a single object.

  • Marked as answer by Naveen N Tuesday, July 29, 2014 7:12 PM
July 29th, 2014 7:03pm

Hi Rohn,

That really works like a charm.

Thanks for your quick reply.

Regards,

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2014 7:13pm

You're welcome
July 29th, 2014 7:15pm

Worked for me too, thanks.
Free Windows Admin Tool Kit Click here and download it now
August 11th, 2015 10:00am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics