Domanda

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

È stato utile?

Soluzione

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top