1

Hi all I am getting a string as follows for the data to be inserted as follows in a variable.

I have written a code to frame it as DataTable as follows:

 protected void Page_Load(object sender, EventArgs e)
        {
            var vData = "ID=1;Name='XYZ';ID=2;Name='PQR'";
            var values = vData.Replace(";", ",");
            var cols = values.Split(',');
            DataTable dt = new DataTable();
            for (int i = 0; i < cols.Length; i++)
            {
                var p = cols[i].Split('=');
                if (!dt.Columns.Contains(p[0].ToString()))
                    dt.Columns.Add(p[0].ToString());
            }
            DataRow dr = dt.NewRow();
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                var value = cols[j].Split('=');
                dr[j] = value[1];
            }
            dt.Rows.Add(dt);
        }

But this is adding only one row as the column will always be 2, but I need add multiple rows if the data exceeds

2
  • Are you able to post some working example? This code has lot of compile errors.. Commented Jan 2, 2014 at 6:36
  • This is the sample code the input which comes to my function I posted and also the converting code Commented Jan 2, 2014 at 6:37

1 Answer 1

3

You can try the below, it is adding multiple rows.

What I did is, I counted ID using regex and add extra for loop below columns for loop.

var v = "ID=1;Name='XYZ';ID=2;Name='PQR';";
        string newRow = "ID";
        var values = v.Replace(";", ",");
        values = values.Remove(values.Length - 1, 1);
        var cols = values.Split(',');

        string pattern = Regex.Escape(cols[0].Split('=')[0]);
        var matches = Regex.Matches(v, "\\b" + pattern + "\\b", RegexOptions.IgnoreCase);
        int count = matches.Count;


        DataTable dt = new DataTable();

        for (int i = 0; i < cols.Length; i++)
        {
            var p = cols[i].Split('=');
            if (!dt.Columns.Contains(p[0].ToString()))
                dt.Columns.Add(p[0].ToString());
        }
        DataRow dr = null;
        int idxCol = 0;
        for (int k = 0; k < count; k++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                var value = cols[idxCol].Split('=');

                if (value[0] == newRow)
                {
                    dr = dt.NewRow();
                    dr[j] = value[1];
                }
                else
                {
                    dr[j] = value[1];
                }
                idxCol++;
            }
            dt.Rows.Add(dr);
        }

You can still re factor it and put the correct values. This is just way of idea to you.

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

6 Comments

Hi thanks for the code but the columns name can be changed these are not static in such cases what to do
But you should know the column name with which a new row starts, isn't it ??
Yes it might be the first Column in most the cases
In that case, instead of hardcoding "ID" in pattern, just take the first column name. See updated code above!!
Yes, i just tested adding multiple rows, I havent tested what values goes into rows, do u want us to show that as well?
|

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.