Can I Assign an Array to a Control's Datasource?

Oct 24 2007 11:22 AM

I'm in the process of integrating a VS2003 Windows Application into a VS2005 2005 Web Application, and am having soe problems with some of the functionality of the windows app working in a web application.

The windows app does the following for several of it's controls - it creates a dataset of any number of columns and then sets them as the datasource for ComboBox controls, i.e.

ArrayList vSchemeList =new ArrayList();
using ( SQLDataReader rdr = vCmd.ExecuteReader() )
{
    while (rdr.Read())
    {
        vSchemeList.Add( new SchemeInfo( rdr.GetString(0), rdr.GetInt32(1), rdr.GetDateTime(2), rdr.GetDouble(3)  ) );
    }
    this.cboScheme.DataSource = vSchemeList;
}

where the SchemeInfo class has been declared as follows:

public class SchemeInfo
{
public string fSchemeName;
        public int fSchemeID;
public DateTime fValnDate;
public string fValnDateStr;
public double fAssetVal;
private const string kDateFmt = "dd MMMM yyyy";

public SchemeInfo( string aName, int aID, DateTime aDate, double aAssetVal )
        {
   fSchemeName = aName;
   fSchemeID   = aID;
   fValnDate   = aDate;
   fValnDateStr = aDate.ToString( kDateFmt  );
   fAssetVal   = aAssetVal;
}

public override string ToString()
{
            return string.Format( "{0}  {1}", fSchemeName, fValnDateStr );
}
}

Also, like this:

private GroupSector[] strSectors;

using ( IDbCommand vCmd = getCommand() )
{
   vCmd.CommandText = "SELECT ColumnA, ColumnB " +
      "FROM DatabaseTable";
   using ( IDataReader rdr = vCmd.ExecuteReader() ) 
   {
      while (rdr.Read()) 
      {
            GroupSector GS = new GroupSector(rdr.GetString(0), rdr.GetInt32(1),

rdr.GetInt32(2)

); 
            strSectors[intIndex] = GS;
            intIndex++;
      }
         this.chklstSector.Items.AddRange(strSectors); 
    }
}

where the GroupSector class has been declared as follows:

public class GroupSector
{
   public string Name;
   public int Code;
   public int GroupCode;

   public GroupSector(string strName, int intCode, int intGroupCode)
   {
      this.Name = strName;
      this.Code = intCode;
      this.GroupCode = intGroupCode;


   }

   public override string ToString()
   {
      return this.Name;
   }
}

Later on, this data is being read back out from the control in the following way:

SchemeInfo vInfo = (SchemeInfo) (sender as ComboBox).SelectedValue; DateTime vDate = vInfo.fValnDate;intSelID = vInfo.fSchemeID; strScheme = vInfo.fSchemeName;

and for the GroupSector, like this:

GroupSector oSector = (GroupSector) this.chklstSector.Items[i];

Trying to do this in a web app fails as for the GroupSector class it's unable to cast the GroupSector type to ListItem[] items and for SchemeInfo class, it's trying to cast a selected dropdownlist item as a SchemeInfo - in both cases it is unable to achieve this. 

Can anyone recommend a way to do this (without having to re-write all the code), or, if I do have to re-write, a better way to do this?

Or am I just missing something obvious?

Thanks in advance

Martin