none
create net datatable according to a Datatable Method

    Question

  • Hi ;

    I have a Datatable method like below ,

    var dt = DtMethod("SELECT TOP 4 NAME,STATE_DESC FROM SYS.DATABASES");

    I want to loop in Datatable and crate new one like it

    I dont want to use some dt method like .Clone , .Copy or others.

    Datatable dt2= new Datatable();

    dt2.Column.Add("How Can I do", typeof (How Can I do)

    also

    dt2.Rows.Add(("How Can I do")

    thanks from now .

    Monday, May 1, 2017 3:08 PM

Answers

  • The question is not very clear, but if I have understood correctly what you want, the easiest way to do it will be to use a for instead of a foreach:

         for (int i = 0; i < dt.Columns.Count; i++)
         {
           DataColumn col = dt.Columns[i];
           metaDataArray[i] = new SqlMetaData(col.ColumnName, colDataType, 100);
         }

    And similarly you can do a for loop on the Rows collection.

    Note that using col.GetType() like you were doing would not have worked. That would always return the type "System.Data.DataColumn", and not the type of the column. That's why I changed it to col.DataType.

    • Marked as answer by Aly14 Tuesday, May 2, 2017 8:43 AM
    Monday, May 1, 2017 4:38 PM
    Moderator

All replies

  • dt2.Column.Add("How Can I do", typeof (How Can I do)

    foreach (DataColumn dc in dt.Columns)
    {
        dt2.Columns.Add(dc.ColumnName, dc.DataTypeType);
    }


    dt2.Rows.Add(("How Can I do")
    foreach (DataRow dr in dt.Rows)
    {
        dt2.Rows.Add(dr.ItemArray);
    }
    Monday, May 1, 2017 3:23 PM
    Moderator
  • Clone the DataTable then to add records from one DataTable to another use DataTable.ImportRow method

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, May 1, 2017 3:28 PM
    Moderator
  • hi,

    it's ok , But How Can fill the below value by checking datatable

     foreach (DataColumn col in dt.Columns)
        {
          metaDataArray[0] = new SqlMetaData(col.ColumnName, col.GetType(), 100);
          metaDataArray[1] = new SqlMetaData(col.ColumnName, col.GetType(), 100);
        }

        foreach (DataRow dr in dt.Rows)
        {
          record.SetString(0, dr[0].ToString());
          record.SetString(1, dr[1].ToString());
        }

    so ,

    metaDataArray[0] ,metaDataArray[1] , record.SetString(0, dr[0].ToString()), record.SetString(1, dr[0].ToString());

    How can I know number of column and rows, how can I set the value dynamically .

    Monday, May 1, 2017 3:42 PM
  • The question is not very clear, but if I have understood correctly what you want, the easiest way to do it will be to use a for instead of a foreach:

         for (int i = 0; i < dt.Columns.Count; i++)
         {
           DataColumn col = dt.Columns[i];
           metaDataArray[i] = new SqlMetaData(col.ColumnName, colDataType, 100);
         }

    And similarly you can do a for loop on the Rows collection.

    Note that using col.GetType() like you were doing would not have worked. That would always return the type "System.Data.DataColumn", and not the type of the column. That's why I changed it to col.DataType.

    • Marked as answer by Aly14 Tuesday, May 2, 2017 8:43 AM
    Monday, May 1, 2017 4:38 PM
    Moderator