Question

I need to create an objectdatasource from a datatable. I currently have a method, that generates a datatble:

static DataTable GetTableForDropDown()
    {

        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("DurationType", typeof(string)));

        dr = dt.NewRow();
        dr["DurationType"] = "Hours";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["DurationType"] = "Days";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["DurationType"] = "Weeks";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["DurationType"] = "Months";
        dt.Rows.Add(dr);
        return dt;
    }

And i need to create and objectdatasource and load it into it. I have found very little documentation or on how to do this. I found the following code to try and convert it, but it just throws an error when i try it.

var edgeDataSource = new ObjectDataSource(
            "MyNamespace.MyClass, MyNamespace.MyClasss, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ce8ab85a8f42a5e8",
            "GetTableForDropDown") {ID = "EdgeDataSource"};
Was it helpful?

Solution

What you could try is this (I put it in one file just for simplicity)

namespace MyNamespace
{
    public class MyClassData
    {
        public string DurationType { get; set; }
    }

    public class MyClass
    {
        public List<MyClassData> GetTableForDropDown()
        {
            List<MyClassData> myList = new List<MyClassData>();
            myList.Add(new MyClassData { DurationType = "Hours" });
            myList.Add(new MyClassData { DurationType = "Days" });
            myList.Add(new MyClassData { DurationType = "Weeks" });
            myList.Add(new MyClassData { DurationType = "Month" });
            return myList;
        }
    }

    public partial class WebForm18 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var edgeDataSource = new ObjectDataSource("MyNamespace.MyClass", "GetTableForDropDown");
            var x = edgeDataSource.Select();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top