Adrián Heredia
Adrián Heredia

Reputation: 23

Convert Object DataSource to datatable C #

I am working with a solution that handles several projects with classes and a website, in a file Reports.aspx in website that represents a view is the following code

                    </asp:ObjectDataSource>
                                    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="ObtenerEncabezadoReportePedido"
                                        TypeName="com.heinsohn.fabricaSw.sodexho.Pedidos.model.enterprise.FachadaContabilizacion">
                                        <SelectParameters>
                                            <asp:SessionParameter Name="unNumeroPedido" SessionField="NumeroPedido" Type="Double" />
                                        </SelectParameters>
                                    </asp:ObjectDataSource>

the method ObtenerEncabezadoReportePedido return a datatable,

In the view logic aspx.cs uses the ObjectDataSource1 to fill a report

miFuente = new ReportDataSource("DataSet2_DataTable1", this.ObjectDataSource1);

the response datable became an ObjectDataSource, I need to modify fields of that return, and save some in variables. It's possible? can I convert that ObjectDataSource to a datatable and use it as I need? , can I later convert it back to an already modified ObjectDataSource?

Thank you very much for your help

Upvotes: 1

Views: 1959

Answers (1)

Jack J Jun- MSFT
Jack J Jun- MSFT

Reputation: 5986

You can try to use the following code to convert object Datasource to datatable.

public  DataTable ChangeData()
        {
            DataTable dt = ((DataView)ObjectDataSource1.Select()).Table;
            dt.Rows[0]["Age"] = 33;   //Change data
            return dt;

        }

The completed code:

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //set Processing Mode of Report as Local   
                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                //set path of the Local report   
                ReportViewer1.LocalReport.ReportPath = @"D:\Report1.rdlc";
                //creating object of DataSet dsMember and filling the DataSet using SQLDataAdapter   
             
                //Providing DataSource for the Report   
                ReportDataSource rds = new ReportDataSource("DataSet1", ObjectDataSource1);
                ReportViewer1.LocalReport.DataSources.Clear();
                //Add ReportDataSource   
                ReportViewer1.LocalReport.DataSources.Add(rds);
               
            }
        }

        public  DataTable ChangeData()
        {
            DataTable dt = ((DataView)ObjectDataSource1.Select()).Table;
            dt.Rows[0]["Age"] = 33;
            return dt;

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable table = ChangeData();
            ReportDataSource rds = new ReportDataSource("DataSet1", table);
            ReportViewer1.LocalReport.DataSources.Clear();
            //Add ReportDataSource   
            ReportViewer1.LocalReport.DataSources.Add(rds);
        }
    }

    public class Student
    {
        public static DataTable GetDataTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Age", typeof(int));
            table.Columns.Add("ID", typeof(int));
            table.Rows.Add("test1", 22, 1001);
            table.Rows.Add("test2", 23, 1002);
            table.Rows.Add("test3", 24, 1003);
            table.TableName = "table1";

            return table;
        }
    }

Result:

enter image description here

Upvotes: 1

Related Questions