.NET Forum / ASP.NET / General / February 2008
display data to user question
|
|
Thread rating:  |
Dave - 13 Feb 2008 20:05 GMT I'm stuck on something and I've been trying to find some examples on it. I'm trying to show the data to my users as such:
year1 year2 Q1 Q2 Q1 Q2 Smith
my dataset looks like this: <?xml version="1.0" standalone="yes"?> <NewDataSet> <Table> <year>2006</year> <SalesName>Smith</SalesName> <Q1>25</Q1> <Q2>50</Q2>
</Table> <Table> <year>2005</year> <SalesName>Smith</SalesName> <Q1>25</Q1> <Q2>50</Q2> <Table>
is there any possible way with either the gridview, repeater, datalist, anything, that I can use to to show my data side by side? Any examples would help as well and be greatly apprecaited.
Angel - 14 Feb 2008 00:01 GMT I can't pinp point you problem. The answer is yes to all of the above. The trick is to you use template to shape how your data is display. a Repeater would work also a datalist is even easier.
But since I do not understand your problem is difficult for me to help. For example This simple way is to use SQLServerControl or ObjServerControl and follow instruction. You can drop a datalist click on smart tag select choose Data Source and follow wizard instructions.
Suggest you start when you get stuck give us a sample of your code and what you were trying to achieve then we will be able to help.
Regards,
 Signature aaa
> I'm stuck on something and I've been trying to find some examples on it. I'm > trying to show the data to my users as such: [quoted text clipped - 25 lines] > > Dave - 14 Feb 2008 00:18 GMT I'm just trying to figure out how to do it. I'm calling my stored procedure and returning a dataset if the format as I described above. So I'm just trying to figure out how to get my data to show as I need it with the output format I'm getting from my stored procedure.
I'm not using a SQLServerControl or a ObjServerControl to get my data.
>I can't pinp point you problem. The answer is yes to all of the above. >The [quoted text clipped - 46 lines] >> would >> help as well and be greatly apprecaited. Angel - 14 Feb 2008 03:09 GMT I suggest you start with a datalist then. if you are using SQL Server it is a bit easier. Once you have a dataset
you could do the following:
DataList1.DataSource = dsDataSet.Tables(0) DataList1.DataBind()
You can do this at the page load event if you like however if the DataList is not properly configured it won't work
Here is an example of one in C#
First the Markup Source:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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:DataList ID="DataList1" runat="server" DataKeyField="CustomerID" > <ItemTemplate> CustomerID: <asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' /> <br /> CompanyName: <asp:Label ID="CompanyNameLabel" runat="server" Text='<%# Eval("CompanyName") %>' /> <br /> Phone: <asp:Label ID="PhoneLabel" runat="server" Text='<%# Eval("Phone") %>' /> <br /> <br /> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>
The code behind:
using System; 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;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { BusinessRules.BusinessRules b = new BusinessRules.BusinessRules(); DataList1.DataSource = b.GetData("select CustomerID, companyname, phone from customers"); DataList1.DataBind(); } }
If you use ask the datalist control to help you it will do so proving you with a vast list of options and it will even configure it self. It depends how experienced you are.
 Signature aaa
> I'm just trying to figure out how to do it. I'm calling my stored procedure > and returning a dataset if the format as I described above. So I'm just [quoted text clipped - 53 lines] > >> would > >> help as well and be greatly apprecaited. Dave - 14 Feb 2008 12:37 GMT I can show the data in a datalist, repeater, grid, gridview, but not side by side and that's where I'm stuck.
the data looks like this when i run the proc in query analyzer
year Lastname q1 q2 2006 Smith 50 25 2007 Smith 10 75
the XML from the dataset looks like this:
<?xml version="1.0" standalone="yes"?> <NewDataSet> <Table> <year>2007</year> <SalesName>Smith</SalesName> <Q1>25</Q1> <Q2>50</Q2> </Table> <Table> <year>2006</year> <SalesName>Smith</SalesName> <Q1>25</Q1> <Q2>50</Q2> <Table>
and this is the output format that I need: 2006 2007 Q1 Q2 Q1 Q2 Smith 25 50 25 75
how can I get this output format? I can get 1 year showing in the repeater, grid, gridview, but I need both years to show side by side of each other. Also, I need to do calculations within the years, so I need to do a calc from 2007 and 2007 data but it shows under 2007
>I suggest you start with a datalist then. if you are using SQL Server it >is [quoted text clipped - 143 lines] >> >> would >> >> help as well and be greatly apprecaited. Angel - 14 Feb 2008 14:07 GMT Okay, choose one control and I will build an example for you that works if you like. I will use your something similar to your table and use that. Do you think that would be of some help?
 Signature aaa
> I can show the data in a datalist, repeater, grid, gridview, but not side by > side and that's where I'm stuck. [quoted text clipped - 179 lines] > >> >> would > >> >> help as well and be greatly apprecaited. Dave - 14 Feb 2008 14:15 GMT yeah that would be great.
I would like to use the gridview if possible, if not that then the repeater
> Okay, choose one control and I will build an example for you that works if > you like. I will use your something similar to your table and use that. [quoted text clipped - 198 lines] >> >> >> would >> >> >> help as well and be greatly apprecaited. Angel - 14 Feb 2008 14:47 GMT Okay,
I will get to it tonight because right now my boss keeps interrupting me with work, how rude is that :-)
 Signature aaa
> yeah that would be great. > [quoted text clipped - 202 lines] > >> >> >> would > >> >> >> help as well and be greatly apprecaited. Dave - 14 Feb 2008 14:53 GMT lol,
thanks i appreciate it
> Okay, > [quoted text clipped - 220 lines] >> >> >> >> would >> >> >> >> help as well and be greatly apprecaited. Angel - 15 Feb 2008 14:14 GMT Dave,
I just realized what you were trying to do. What you want to do is to turn the table on its side basically to display rows as columns. There is no simple way of forcing the controls to do this. You can shape the data with a SQL query that actually perform the rotation.
I am working on the example anyway but it would not be as trivial as I first thought because I misunderstood the problem. So I am not going to be ready today with this solution as I promise so in the mean-time you can help you self by reviewing some the info below.
Definition
A pivot table is a data summarization tool found in data visualization programs such as spreadsheets (e.g. Excel sheet). Among other functions, it can automatically sort, count, and total the data stored in one table or spreadsheet and create a second table displaying the summarized data. Pivot tables are useful to create crosstabs quickly. The user sets up and changes the summary's structure by dragging and dropping fields graphically. This "rotation" or pivoting of the summary table gives the concept its name
Checkout these links
http://technet.microsoft.com/en-us/library/ms177410.aspx
http://jdixon.dotnetdevelopersjournal.com/pivot_table_data_in_sql_server_2000_an d_2005.htm
http://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables
http://www.databasejournal.com/features/mssql/article.php/3516331
Once I have a sample I will posted (if you still need it)
Hope this helps
 Signature aaa
> I'm stuck on something and I've been trying to find some examples on it. I'm > trying to show the data to my users as such: [quoted text clipped - 25 lines] > > Mike - 15 Feb 2008 14:44 GMT I'm doing the pivot table on SQL already.
the data is coming out like this
YEAR Lastname Q1 Q2 -------------------------------------------------------------------
1995 Smith 125,000.90 136,000.75 1996 Smith 328,000.82 422,000.13
so I have the data "flipped" on the SQL side already, so it comes out with the columns named instead of of the normal output format
year name q1 q2
> Dave, > [quoted text clipped - 68 lines] >> would >> help as well and be greatly apprecaited. Dave - 17 Feb 2008 01:04 GMT Angel, Using the sql pivot still won't allow me to show my data side by side. this thing is driving me nuts
> Dave, > [quoted text clipped - 68 lines] >> would >> help as well and be greatly apprecaited. Angel - 17 Feb 2008 01:26 GMT That's interesting because I have something here that I started whre I used sql to do it. I stop because I figured we had the answer but I will look it up a gaind and send it to you. The problem is that you want to show the years followed by name and the Quaters side by side in the appropriate slot with the years. The Grid will not do this easily. We can pump code to it manipulate the datatable to provide us the right format but it will not be simple.
Think if we can do it in t-sql we can do it in code but no matter how you slice it it will require a significant amount of coding it will not be trivial.
 Signature aaa
> Angel, > Using the sql pivot still won't allow me to show my data side by [quoted text clipped - 72 lines] > >> would > >> help as well and be greatly apprecaited. Dave - 17 Feb 2008 13:47 GMT Yeah that's the point I'm at. I have my data pivoted in SQL, I created a datatable in code, but getting it to show on the GUI side by side isn't happening very easy or at all.
> That's interesting because I have something here that I started whre I > used [quoted text clipped - 94 lines] >> >> would >> >> help as well and be greatly apprecaited. Angel - 17 Feb 2008 17:31 GMT Note I can send this in a zip file if you supply me with an email. most of the info like table schema and images did not made it. Sorry
I created a project that has an embedded SQl table with the scenario you described
Database: DavesDataase.mdf Tables: 1 - SalesSumm 2 - Yeartable 3 - Qttable 4 - Nametable
SaleSumm col name data type
year nchar(4)
salesname nvarchar(50)
Q1 SMALLINT
Q2 SMALLINT
Then other 3 table are design to denormalize the first
Yeartable Column Name Data type
col1 nvarchar(250) year 1 col2 nvarchar(250) year 2 col3 nvarchar(250)
col4 nvarchar(250)
col5 nvarchar(250)
col6 nvarchar(250)
col7 nvarchar(250)
seq smallint
Qttable
Column Name Data type
col1 nvarchar(250)
col2 nvarchar(250)
col3 nvarchar(250) 1q1 col4 nvarchar(250) 1q2 col5 nvarchar(250) 2q1 col6 nvarchar(250) 2q2 col7 nvarchar(250)
seq smallint
Nametable Column Name Data type
col1 nvarchar(250)
col2 nvarchar(250)
col3 nvarchar(250)
col4 nvarchar(250)
col5 nvarchar(250)
col6 nvarchar(250)
col7 nvarchar(250) name seq smallint
================================================================================================ Class module: DavesDataAccess.vb
Imports Microsoft.VisualBasic Imports System.Data Imports System.Data.SqlClient Public Class DavesDataAccess Public Function AReallyBigSql() As Data.DataTable TruncateTables() Dim sql As String = _ "declare @YearOne nvarchar(4) " & _ "declare @YearTwo nvarchar(4) " & _ "declare @SalesName nvarchar(50) " & _ "declare @1Q1 nvarchar(4) " & _ "declare @1Q2 nvarchar(4) " & _ "declare @2Q1 nvarchar(4) " & _ "declare @2Q2 nvarchar(4) " & _ " " & _ "select top 1 @YearOne = cast([year] as nvarchar) from SalesSumm " & _ "select top 1 @YearTwo = cast ([year] as nvarchar) from SalesSumm where year <> @YearOne " & _ "select top 1 @SalesName = salesName from salesSumm " & _ "select top 1 @1Q1 = cast (q1 as nvarchar(2)) from salessumm " & _ "select top 1 @1Q2 = cast(q2 as nvarchar(2)) from salessumm " & _ "select top 1 @2Q1 = cast(q1 as nvarchar(2)) from salessumm where year <> @YearOne " & _ "select top 1 @2Q2 = cast(q2 as nvarchar(2)) from salessumm where year <> @YearOne " & _ " " & _ "insert into YearTable (seq, col1, col2) values(1, @YearOne, @YearTwo) " & _ "insert into QtTable (seq,col3,col4,col5,col6) values(2, @1q1, @1q2,@2q1, @2q2) " & _ "insert into NameTable (seq, col7) values(3, @salesName) " & _ " " & _ "select * from YearTable " & _ "union " & _ "select * from qttable " & _ "union " & _ "select * from NameTable" & _ " order by seq"
Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DevCenter\WebSites\DisplayOnItsSide\App_Data\DavesDatabase.mdf;Integrated Security=True;User Instance=True") cn.Open() Dim da As New SqlDataAdapter(sql, cn) Dim dt As New Data.DataTable da.Fill(dt) cn.Close() Return dt End Function
Public Sub TruncateTables() Dim sql As String = _ "delete from nameTable" Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DevCenter\WebSites\DisplayOnItsSide\App_Data\DavesDatabase.mdf;Integrated Security=True;User Instance=True") cn.Open() Dim cmd As New SqlCommand(sql, cn)
cmd.ExecuteNonQuery() sql = "delete from YearTable" cmd.CommandText = sql cmd.ExecuteNonQuery()
sql = "delete from qttable" cmd.CommandText = sql cmd.ExecuteNonQuery()
cn.Close() End Sub
End Class
================================================================================================ Page - Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_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> <style type="text/css"> .style1 { width: 100%; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" Height="495px" Width="288px"> <ItemTemplate> <table class="style1"> <tr> <td align="center" colspan="2"> <asp:Label ID="lblYearOne" runat="server" Text='<%# Eval("col1") %>'></asp:Label> </td> <td align="center" colspan="2"> <asp:Label ID="lblYearTwo" runat="server" Text='<%# Eval("col2") %>'></asp:Label> </td> </tr> <tr> <td align="center"> <asp:Label ID="lbl1Q1" runat="server" Text='<%# Eval("col3") %>'></asp:Label> </td> <td align="center"> <asp:Label ID="lbl1Q2" runat="server" Text='<%# Eval("col4") %>'></asp:Label> </td> <td align="center"> <asp:Label ID="lbl2Q1" runat="server" Text='<%# Eval("col5") %>'></asp:Label> </td> <td align="center"> <asp:Label ID="lbl2Q2" runat="server" Text='<%# Eval("col6") %>'></asp:Label> </td> </tr> <tr> <td colspan="4"> <asp:Label ID="lblName" runat="server" Text='<%# Eval("col7") %>'></asp:Label> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> </table> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>
Code behind
Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load BindDataList() End Sub
Private Sub BindDataList() Dim da As New DavesDataAccess
Dim dt As Data.DataTable = da.AReallyBigSql
DataList1.DataSource = dt DataList1.DataBind()
End Sub End Class
===========================================================================================
A second example Page: Repeater.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Repeater.aspx.vb" Inherits="Repeater" %>
<!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:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <asp:Label ID="lblYrOne" runat="server" Text='<%# Eval("col1") %>'></asp:Label> <asp:Label ID="lblYrTwo" runat="server" Text='<%# Eval("col2") %>'></asp:Label> <br /> <asp:Label ID="lbl1Q1" runat="server" Text='<%# Eval("col3") %>'></asp:Label> <asp:Label ID="lbl1Q2" runat="server" Text='<%# Eval("col4") %>'></asp:Label> <asp:Label ID="lbl2Q1" runat="server" Text='<%# Eval("col5") %>'></asp:Label> <asp:Label ID="lbl2Q2" runat="server" Text='<%# Eval("col6") %>'></asp:Label> <br /> <asp:Label ID="lblName" runat="server" Text='<%# Eval("col7") %>'></asp:Label> </ItemTemplate> </asp:Repeater> </div> </form> </body> </html>
Code behind
Partial Class Repeater Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load BindDataList()
End Sub
Private Sub BindDataList() Dim da As New DavesDataAccess
Dim dt As Data.DataTable = da.AReallyBigSql
Repeater1.DataSource = dt Repeater1.DataBind()
End Sub
End Class
So what it does if you look at dataaccess module
"declare @YearOne nvarchar(4) " & _ "declare @YearTwo nvarchar(4) " & _ "declare @SalesName nvarchar(50) " & _ "declare @1Q1 nvarchar(4) " & _ "declare @1Q2 nvarchar(4) " & _ "declare @2Q1 nvarchar(4) " & _ "declare @2Q2 nvarchar(4) " & _ " " & _ "select top 1 @YearOne = cast([year] as nvarchar) from SalesSumm " & _ "select top 1 @YearTwo = cast ([year] as nvarchar) from SalesSumm where year <> @YearOne " & _ "select top 1 @SalesName = salesName from salesSumm " & _ "select top 1 @1Q1 = cast (q1 as nvarchar(2)) from salessumm " & _ "select top 1 @1Q2 = cast(q2 as nvarchar(2)) from salessumm " & _ "select top 1 @2Q1 = cast(q1 as nvarchar(2)) from salessumm where year <> @YearOne " & _ "select top 1 @2Q2 = cast(q2 as nvarchar(2)) from salessumm where year <> @YearOne " & _ " " & _ "insert into YearTable (seq, col1, col2) values(1, @YearOne, @YearTwo) " & _ "insert into QtTable (seq,col3,col4,col5,col6) values(2, @1q1, @1q2,@2q1, @2q2) " & _ "insert into NameTable (seq, col7) values(3, @salesName) " & _ " " & _ "select * from YearTable " & _ "union " & _ "select * from qttable " & _ "union " & _ "select * from NameTable" & _ " order by seq"
is create a temp output using a union to join three sql queries making sure the rows are in the right order using the order by seq.
The rest of the magic is in the databinding which you can look at in the aspx stuff.
I did test this and it does give
Untitled Pagehttp://localhost:2248/DisplayOnItsSide/Repeater.aspx Screen clipping taken: 2/17/2008, 12:19 PM
Hope this help. If you want the entire project I can zip it and email to a point of preference.
 Signature aaa
> Yeah that's the point I'm at. I have my data pivoted in SQL, I created a > datatable in code, but getting it to show on the GUI side by side isn't [quoted text clipped - 98 lines] > >> >> would > >> >> help as well and be greatly apprecaited. Dave - 18 Feb 2008 13:30 GMT you can send it here,. csharpcoder@optonline.net
> Note I can send this in a zip file if you supply me with an email. most > of [quoted text clipped - 539 lines] >> >> >> would >> >> >> help as well and be greatly apprecaited. Dave - 18 Feb 2008 20:16 GMT Angel, I actaully got it to work with the Gridview. After spending hours on it, I finally got everything to show in the gridview side by side.
> Note I can send this in a zip file if you supply me with an email. most > of [quoted text clipped - 539 lines] >> >> >> would >> >> >> help as well and be greatly apprecaited.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|