Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi friends

I have a table(asp table) with data in it. I would like to copy all these data into a DataTable/Dataset. If anyone has idea about this please help me.



Thank You
Posted
Updated 2-Jun-14 21:19pm
v2
Comments
Thanks7872 3-Jun-14 3:19am    
Dont repost questions. Follow original thread only.

You have to use foreach like in the next example:

C#
foreach (TableRow tableRow in aspTable.Rows)
            {
                yourDataTable.Rows.Add(tableRow.Cell[0].Text, tableRow.Cell[1].Text,tableRow.Cell[2].Text, ...);
            }
 
Share this answer
 
Enample for dataset
C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ShowData_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection connection ;
            SqlCommand command ;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string Sql = null;
           
            connetionString = Your Connection string;
            Sql = "SQL Statement";
            
            connection = new SqlConnection(connetionString);

            try
            {
                connection.Open();

                command = new SqlCommand(Sql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds, "First Table");

                adapter.Dispose();
                command.Dispose();
                connection.Close();

                //retrieve  data 
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}
 
Share this answer
 
Thank You all.. I got the solution...Thanks a lot....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900