-1

I want to write business logic with data in a dataset.

The data will be of the below format

AssociateId Action Time

1          Login     12/12/2012 20:33
2          Login     12/13/2012 20:33
1          Login     12/14/2012 20:33
2          Game page 12/15/2012 20:33
1          Home page 12/16/2012 20:33
1          Map       12/17/2012 20:33

I want to convert this to a dataset, i dont want to make database call and populate it from there.

I am assigning it to a string and converting it to an array by splitting using the space. How can convert that array or this string as a whole to a dataset or datatable??

Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150

2 Answers2

5

You could create a DataTable in memory and add it to any DataSet. Something like:

var myTable = new DataTable();
myTable.Columns.Add(new DataColumn("AssociateId"));
myTable.Columns.Add(new DataColumn("Action"));
myTable.Columns.Add(new DataColumn("Time"));

var row = myTable.NewRow();
row["AssociateId"] = 1;
row["Action"] = "Login";
row["Time"] = new DateTime(2012, 12, 12, 20, 33, 0);

myTable.Rows.Add(row);

But does it really need to be a DataTable? Creating a custom object seems like a cleaner and more robust way of holding the data in memory. Something as simple as:

class MyClass
{
    public int AssociateID { get; set; }
    public string Action { get; set; }
    public DateTime Time { get; set; }
}

Then you can create a list of them:

var myList = new List<MyClass>();
myList.Add(new MyClass
{
    AssociateID = 1,
    Action = "Login",
    Time = new DateTime(2012, 12, 12, 20, 33, 0)
});
Esset
  • 916
  • 2
  • 15
  • 17
David
  • 208,112
  • 36
  • 198
  • 279
1

You first need to fill the list from your required data. and then fill dataset from that list.

pls follow link, hope this will help you.

How to fill a datatable with List<T>

Community
  • 1
  • 1
Brijesh
  • 352
  • 5
  • 17