locked
create datatable RRS feed

  • Question

  • User-454568543 posted

    How to create datatable with multiple records

    Help me

    Saturday, June 6, 2009 12:13 AM

Answers

  • User1571885900 posted

    Hi,

    To create datatable is very easy.

    first create DataTable object.

    Create Columns for it and add in datatable.

    then add rows in the datatable.

    ------

      

    //--create datatable with 'No' & 'Name' column.
    DataTable dt = new DataTable();	//-- Create datatable object
    DataColumn cl = new DataColumn("No");	//-- Create column 'No'
    dt.Columns.Add(cl);	//--Add 'No' Column in datatable 
    cl = new DataColumn("Name");	//-- Create column 'Name'
    dt.Columns.Add(cl);	//--Add 'Name' Column in datatable 
    
    
    //-- add first row in datatable
    DataRow dr = dt.NewRow();	//-- get new row for datatable.
    dr[0]=1;	//-- assign value for 'No' column
    dr[1]="John";	//-- assign value for 'Name' column
    dt.Rows.Add(dr); 	// Add row in datatable
    
    //-- add second row in datatable
    dr = dt.NewRow();
    dr[0]=2;
    dr[1]="Smith";
    dt.Rows.Add(dr);
    

     

    you can add as many rows as you want.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 6, 2009 12:31 AM
  • User1571885900 posted

    Hi,

    to fill data from database using datareader please refer http://www.dotnetcurry.com/ShowArticle.aspx?ID=143

    and using Adaptor

    http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx

    or write your problem in detail so somebody can help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 6, 2009 4:35 AM
  • User854688209 posted
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("Select * FROM Employee", conn);
    cmd.CommandType =
    CommandType.StoredProcedure;
    SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmd);
    DataTable myDataTable = new DataTable();
    mySqlAdapter.Fill(myDataTable);

    MyGrid.DataSource = myDataTable; //myDataTable will have data which will be binded to gridview
    MyGrid.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 6, 2009 6:15 AM
  • User-1171043462 posted

    its ok, But i want dynamically add data to datatable from database

    Refer here

    http://www.aspsnippets.com/post/2009/02/09/Parameterized-Queries-ADONet.aspx

    Download code in VB and C#

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 6, 2009 8:37 AM

All replies

  • User1571885900 posted

    Hi,

    To create datatable is very easy.

    first create DataTable object.

    Create Columns for it and add in datatable.

    then add rows in the datatable.

    ------

      

    //--create datatable with 'No' & 'Name' column.
    DataTable dt = new DataTable();	//-- Create datatable object
    DataColumn cl = new DataColumn("No");	//-- Create column 'No'
    dt.Columns.Add(cl);	//--Add 'No' Column in datatable 
    cl = new DataColumn("Name");	//-- Create column 'Name'
    dt.Columns.Add(cl);	//--Add 'Name' Column in datatable 
    
    
    //-- add first row in datatable
    DataRow dr = dt.NewRow();	//-- get new row for datatable.
    dr[0]=1;	//-- assign value for 'No' column
    dr[1]="John";	//-- assign value for 'Name' column
    dt.Rows.Add(dr); 	// Add row in datatable
    
    //-- add second row in datatable
    dr = dt.NewRow();
    dr[0]=2;
    dr[1]="Smith";
    dt.Rows.Add(dr);
    

     

    you can add as many rows as you want.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 6, 2009 12:31 AM
  • User-454568543 posted

    its ok, But i want dynamically add data to datatable from database

    Saturday, June 6, 2009 12:36 AM
  • User1571885900 posted

    Hi,

    to fill data from database using datareader please refer http://www.dotnetcurry.com/ShowArticle.aspx?ID=143

    and using Adaptor

    http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx

    or write your problem in detail so somebody can help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 6, 2009 4:35 AM
  • User854688209 posted
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("Select * FROM Employee", conn);
    cmd.CommandType =
    CommandType.StoredProcedure;
    SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmd);
    DataTable myDataTable = new DataTable();
    mySqlAdapter.Fill(myDataTable);

    MyGrid.DataSource = myDataTable; //myDataTable will have data which will be binded to gridview
    MyGrid.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 6, 2009 6:15 AM
  • User-1171043462 posted

    its ok, But i want dynamically add data to datatable from database

    Refer here

    http://www.aspsnippets.com/post/2009/02/09/Parameterized-Queries-ADONet.aspx

    Download code in VB and C#

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 6, 2009 8:37 AM
  • User1817123911 posted

    refer this link:

    http://articles.sitepoint.com/article/net-shopping-cart-datatables

    Thursday, August 5, 2010 12:44 PM