Hi,

im trying to populate each datagrid with an individual record from a table by using a loop - ideally id like something like this below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        {
            for (int i = 1; i <= 3; i++)
            {
// datatable dt + i means id like the datatable to be named dt and the i from the loop

                DataTable dt+i = new DataTable();
                String strConnString = System.Configuration.ConfigurationManager.
                    ConnectionStrings["conString"].ConnectionString;

// i think this sql part using i should work atleast
                string strQuery = "select ID, Name from tblFiles where id ='" + i + "'";
                SqlCommand cmd = new SqlCommand(strQuery);
                SqlConnection con = new SqlConnection(strConnString);
                SqlDataAdapter sda = new SqlDataAdapter();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                try
                {
                    con.Open();
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);

// dt1, dt2, dt3 are the result of the loop dt+i
                    GridView1.DataSource = dt1;
                    GridView1.DataBind();
                    GridView1.DataSource = dt2;
                    GridView1.DataBind();
                    GridView1.DataSource = dt3;
                    GridView1.DataBind();


                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
                finally
                {
                    con.Close();
                    sda.Dispose();
                    con.Dispose();
                    dt.Dispose();
                }
            }
        }
    }
}

i know this may be totally wrong - but im trying to indicate what im trying to acheive. i could implement this by simply copying the original code 3 times changing the sql statement and populating 1 grid with 1 copy. - but that seems like a awfull lot of repetition!

any ideas?

There is two way of doing, with an array of DataTable or, which I reocmmed a DataSet

DataSet d = new DataSet();
            d.Tables.Add(new DataTable());

So basicly after you've filled your datatable, you add it to the dataset. you can also access a datatable with an indexer (string or int)

DataSet d = new DataSet();
     d.Tables[1];
     d.Tables["name_used_on_creation_of_datatable"];

thanks for the reply. :)

what i dont understand is how i can populate a datatable to put into the dataset with just one row? and then make another datatable with just one row ... etc etc?

Well if you want different different content in each table you need different queries which means you could pass the query as a parameter to your function as well as the string identifie of that table in the dataset.

public partial class Default2 : System.Web.UI.Page
{
    private DataSet set = new DataSet();

    protected void btn_Click(object sender, EventArgs e)
    {
          string query = "select ID, Name from tblFiles";
          addToDataSet(query, key);
    }

    private void addToDataSet(string query, string key)
    {
         // datatable dt + i means id like the datatable to be named dt and the i from the loop

                DataTable d = new DataTable(key);
                String strConnString = System.Configuration.ConfigurationManager.
                    ConnectionStrings["conString"].ConnectionString;

// i think this sql part using i should work atleast
                SqlCommand cmd = new SqlCommand(query);
                SqlConnection con = new SqlConnection(strConnString);
                SqlDataAdapter sda = new SqlDataAdapter();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                try
                {
                    con.Open();
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);


                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
                finally
                {
                    con.Close();
                    sda.Dispose();
                    con.Dispose();
                    dt.Dispose();
                    if(d != null) set.Tables.Add(d);
                }
    }
}

Edited 6 Years Ago by PierlucSS: n/a

This article has been dead for over six months. Start a new discussion instead.