0

Actually I have some string with N number of plans like, for Example

"Smith     12345678    PlanA"
"Robert    09876543    PlanB"
"Steve     12345678    PlanA"
"Knae      09876543    PlanB"
"GoldSmith 09090909    planC"
......

If we know the number of plans, I can use If condition. But here I dono the number of plans. So, how can I create a datatable and store each records based on planID dynamically.

The Result will be like

All the records belongs to planA stores in one datatable , PlanB in another datatable and so on.

Looking for a help. Thanks in advance !!!

5
  • How do you get the input data? Commented Jan 9, 2017 at 3:37
  • Actually i am uploading a file and read the file using (StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~/Downloads"), fileName))) { string s = sr.ReadToEnd(); string[] Mem = s.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); } Commented Jan 9, 2017 at 3:39
  • Do you want to create an instance of System.Data.DataSet ? Commented Jan 9, 2017 at 3:42
  • Here i am using String[] Mem to split the whole string into two as like i mentioned in the question Commented Jan 9, 2017 at 3:42
  • Once all the plans stores into the datatable (according to plan), i need to store the datatables in datsset Commented Jan 9, 2017 at 3:45

1 Answer 1

1

You can create a DataSet with DataTables with this code:

var list = new[] { new { Name = "", Code = "", Plan = "" } }.ToList();

for (var i = 0; i < mem.Length; i++)
{
    // change the logic to extract all values from line
    var values = mem[i].Split(',');

    list.Add(new { Name = values[0].Trim(), Code = values[1].Trim(), Plan = values[2].Trim() });
}

var plans = list.Where(item => !String.IsNullOrEmpty(item.Plan)).Select(item => item.Plan).Distinct().ToList();

var dataSet = new DataSet();

foreach (var plan in plans)
{
    var dataTable = new DataTable(plan);

    dataTable.Columns.Add(new DataColumn("Name", typeof(String)));
    dataTable.Columns.Add(new DataColumn("Code", typeof(String)));
    dataTable.Columns.Add(new DataColumn("Plan", typeof(String)));

    var childs = list.Where(item => item.Plan == plan).ToList();

    foreach (var child in childs)
    {
        var newRow = dataTable.NewRow();

        newRow["Name"] = child.Name;
        newRow["Code"] = child.Code;
        newRow["Plan"] = child.Plan;

        dataTable.Rows.Add(newRow);
    }

    dataSet.Tables.Add(dataTable);
}

In your case you must to change the logic to split values per line.

Let me know if this is useful.

Sign up to request clarification or add additional context in comments.

11 Comments

But, this code seems it stores all the records in same dataTable. Right ?
Yes, do you want to create multiple data tables inside of DataSet ? So you must to add the for loop before to create data table instance
Yes, I wan to store the records like. If the record belongs to planA it should store in one datatable, If the record belongs to planB it should store in another datatable and so on
"Smith 12345678 PlanA"... This string should store in one datatable. "Robert 09876543 PlanB".... This string should store in one datatable. Like this
It's done, please check it and don't forget mark as correct answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.