2

I am having trouble trying to put in data for this project I'm working on. I have three GridViews that I want to fill with three sets of data from a query. The code below only generates the three grids; all of them are duplicating the third set.

What I'm trying to do is get the first set and put it into the first grid, get the second set and put it into the second grid, and third set into the third grid.

I don't know why it is retrieving only the last set. I think it has something to do with the adaptor method?

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        DataTable dt3 = new DataTable();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ISALog1ConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand("exec ProxyReport", conn);
        cmd.CommandTimeout = 200;
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        ad.Fill(dt);
        ad.Fill(dt2);
        ad.Fill(dt3);

        GridView1.DataSource = dt;
        GridView1.DataBind();
        GridView2.DataSource = dt2;
        GridView2.DataBind();
        GridView3.DataSource = dt3;
        GridView3.DataBind();
    }
}

Here is what it is getting, it is getting the last set at the bottom and duplicating that one 3 times. I understand the the code does the same thing which is why it duplicates, but why the third set and not the first or middle one? How do i get the first grid to have the first set, and the second to have second set?

enter image description here

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Cloud
  • 213
  • 1
  • 6
  • 14
  • 2
    Because ad.Fill(dt3); is your last statement for filling the adapter – Suave Nti Jul 02 '12 at 18:39
  • Your query does not change or am I missing something? – codingbiz Jul 02 '12 at 18:39
  • You are filling all 3 of them with same sproc "ProxyReport" (ad ) – user1166147 Jul 02 '12 at 18:40
  • Yeah, it doesn't change. It's supposed to but i'm not sure why it's pulling the 3rd set and not just start pulling from the first? the fill dt3 etc is just the object name. I understand that it's putting data into datatable (dt) and the other 2. would it be the adaptor that is causing it to pull only 3rd set and not first or second? – Cloud Jul 02 '12 at 18:43
  • the proxyreport in sql, the code generates a loop which makes the 3 data sets. – Cloud Jul 02 '12 at 18:50
  • You mentioned that it's all duplicating the third set. That makes sense since all three are performing the same query. I would expect to get the same result for all three sets. – Theo Jul 02 '12 at 18:51
  • So what you are saying is that it is impossible to get 3 different set of data from 1 query. and the query has a loop which gets those 3 data sets. and then have it retrieve the 1st, 2nd, and 3rd sets and put them into the grids accordingly? – Cloud Jul 02 '12 at 19:04
  • I can't write a code that will just put the data sets from the loop into the grids? like, the loop in the query will generate the data sets, so i can't write code to have it put the first set it gets into 1 table, get the 2nd and put it in another table, and so on? – Cloud Jul 02 '12 at 19:06

1 Answers1

1

try with dataset, and get datatable from dataset.

DataSet dataSet = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
        ad.Fill(dataSet);

        GridView1.DataSource = dataset.Tables[0];
        GridView1.DataBind();
        GridView2.DataSource = dataset.Tables[1];
        GridView2.DataBind();
        GridView3.DataSource = dataset.Tables[2];
        GridView3.DataBind();
Aghilas Yakoub
  • 27,095
  • 4
  • 42
  • 47
  • Okay, the code i had posted above, the formatting of it was all aligned correctly. but when i changed to the data adapter, it messed up the formatting. I'm not sure how that comes to be.. – Cloud Jul 02 '12 at 20:07