Search code examples
c#asp.net.netdatasourceobjectdatasource

ObjectDataSource in c# Code Behind?


Is it possible to read the data provided by an ObjectDataSource created in code behind? Take the following for instance:

ObjectDataSource myObjectDataSource= new ObjectDataSource();
myObjectDataSource.SelectParameters.Add(new SessionParameter("createdDate", TypeCode.String, "FilterCreated"));

How could you then get the rows from this? For example in a Dataset you would do something like:

foreach (DataRow dr in myDataset.Tables[0].Rows) {
     string abc = dr["myColumn"];
}

Solution

  • you can try like this , convert objectdatasource to dataset and than read that

    private DataSet ConvertObjectSourceToDataSet(ObjectDataSource ods)
    {
       var ds = new DataSet();
       var dv = (DataView)ods.Select();
       if (dv != null && dv.Count > 0)
       {
         var dt = dv.ToTable();
         ds.Tables.Add(dt);
       }
      return ds;
    }
    

    Code source : http://www.aspdotnet-suresh.com/2010/09/how-to-bind-dataset-with.html