0

Basically i'm reflecting a class which is an objectdatasource and I need to be able to create the parameters on the fly. But I have no idea of how to actually add those to the Propertyinfo of Parameters. The aspx method will not work because this is all done on the fly.

This is what I have but it errors when you try to add the Collection Object

        var dataSourceType = typeof(DataSource).Assembly.GetType("DataSource", true);
    var datafield = Activator.CreateInstance(dataSourceType);
    PropertyInfo pinfo = datafield.GetType().GetProperty("Parameters");

    ParameterCollection parmCollection = new ParameterCollection();
    QueryStringParameter myParm = new QueryStringParameter("ber", DbType.String, "ber");
    parmCollection.Add(myParm);
    pinfo.SetValue(pinfo, parmCollection, null);

So yeah I basically want to create the datasource and then attached my parameter collection and then when a grid or other control loads it can do the databind on the source.

josh long
  • 3
  • 1

1 Answers1

0

I'm not sure, but you have a typo... wouldn't you mean this instead:

var dataSourceType = typeof(DataSource).Assembly.GetType("DataSource", true);
var datafield = Activator.CreateInstance(dataSourceType);
PropertyInfo pinfo = datafield.GetType().GetProperty("Parameters");

ParameterCollection parmCollection = new ParameterCollection();
QueryStringParameter myParm = new QueryStringParameter("ber", DbType.String, "ber");
parmCollection.Add(myParm);
pinfo.SetValue(datafield, parmCollection, null);

And please be sure to include the name of the class with namespace (the "DataSource" string). I've tested the code and it completed successfully. :)

This code will set the Parameters property of the datafield object to the specified value. If you want to set the Parameters property of other DataSource object you may just replace the last line:

pinfo.SetValue(«object», parmCollection, null);

Your intention is a little confuse... Why can't you cast the object you're dealing with as an ObjectDataSource and then access/set the properties you want?

object dataSource = new ObjectDataSource();

(...)

var objectDataSource = dataSource as ObjectDataSource;
if (objectDataSource != null)
{
    var selectParams = objectDataSource.SelectParameters;
    selectParams.Clear();
    selectParams.Add(new QueryStringParameter("ber", DbType.String, "ber"));
}

I hope it helps.

DarkCompiled
  • 176
  • 6
  • Thanks for the typo catch but unfortunately that's all it was. I think the real issue is for me Parameters canwrite = false – josh long May 31 '13 at 12:44