Reputation: 970
I got a Dataset that fills a gridview. The gridview has to be filled with one dataset and multiple queries (or datatables in this instance).
As you can see in the code I used 2 DataTables. These Datatables are filled from a database (with sql server). The Datatables are added to one Dataset. In the ASP code you can see there are 3 labels and also 3 columns in the gridview. The first 2 columns are from the first datatable or the first query. The second query fills the last or third column.
When I run this code it doesn't find lblThree
in the dataset. LblThree
is in the dataset but in the second table and .NET doesn't know that I guess. Do you guys know a solution for this error so I can fill my gridview with one dataset?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace TwoDatatableToDataset
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadAll();
}
}
private void loadAll()
{ // STEP ONE Put one sql output in an empty dataset: (works well)
//DataSet ds = new DataSet();
//DataTable dt2 = new DataTable();
//string sql = "select SID1 as lblOne, Charge_Amount as lblTwo from [xxx].[xxxx].[xxxx]";
//SqlConnection Scon = new SqlConnection(ConfigurationManager.ConnectionStrings["testserver"].ConnectionString);
//SqlCommand Scmd = new SqlCommand(sql, Scon);
//Scmd.CommandType = CommandType.Text;
//SqlDataAdapter adp = new SqlDataAdapter(Scmd); // all data in adapter
//Scon.Open();
//adp.Fill(ds); //Adapter in dataset
//GridView1.DataSource = ds; //dataset in gridview
//GridView1.DataBind();
//STEP TWO Make 2 datatables set them in one dataset
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
string sql = "select SID1 as lblOne, Charge_Amount as lblTwo from [xxxx].[xxxx].[xxxx]";
SqlConnection Scon = new SqlConnection(ConfigurationManager.ConnectionStrings["testserver"].ConnectionString);
SqlCommand Scmd = new SqlCommand(sql, Scon);
Scmd.CommandType = CommandType.Text;
SqlDataAdapter adp = new SqlDataAdapter(Scmd); // all data in adapter
Scon.Open();
adp.Fill(dt); //Adapter in dataset
sql = "select Currency as lblThree from [xxx].[dbo].[tbl_xx_NI]";
Scmd = new SqlCommand(sql, Scon);
Scmd.CommandType = CommandType.Text;
adp = new SqlDataAdapter(Scmd); // all data in adapter
adp.Fill(dt2); //Adapter in dataset
ds.Tables.Add(dt); //add data table to data set
ds.Tables.Add(dt2);
GridView1.DataSource = ds; //dataset in gridview
GridView1.DataBind(); // ERROR: A field or property with the name 'lblThree' was not found on the selected data source.
Scon.Close();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TwoDatatableToDataset._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="False" AutoGenerateEditButton="False">
<Columns>
<asp:BoundField DataField="lblOne" HeaderText="One" SortExpression="lblOne">
<ItemStyle Width="20%"/>
</asp:BoundField>
<asp:BoundField DataField="lblTwo" HeaderText="Two" SortExpression="lblTwo">
<ItemStyle Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="lblThree" HeaderText="Three" SortExpression="lblThree">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Upvotes: 0
Views: 7792
Reputation: 129
Alternatively, couldn't you create a view in your database, using the filters that you would use in your queries so that it would represent a dataset that would contain all the information that you need, so that you could query the view and store the results in a single data table?
Upvotes: 1
Reputation: 314
Maybe you can use UNION in your sql command like this :
select SID1 as lblOne, Charge_Amount as lblTwo from [xxxx].[xxxx].[xxxx] UNION select Currency as lblThree from [xxxx].[dbo].[tbl_xx_NI]
Upvotes: 1
Reputation: 46047
The right solution really depends on what you're trying to do. There are probably two main options that you'll want to look into though:
DataSet
programmatically, and merge the results using LINQGridView
or ListView
to generate the expected outputPersonally, I would opt for the LINQ option, but that's up to you. The article below should help to get you pointed in the right direction.
http://geekswithblogs.net/shahed/archive/2009/02/11/129310.aspx
Upvotes: 1
Reputation: 6434
Potentially you could call Merge
.
DataTable dt1 = ds.Tables[0];
DataTable dt2 = ds.Tables[1];
dt1.Merge(dt2);
It seems as though this is the table you essentially want to bind. Anyway, try this and breakpoint the merge method. Step over and see what dt1
looks like.
Upvotes: 2