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?