Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 9th, 2004, 02:16 PM
Authorized User
 
Join Date: Aug 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding rows to DataViews/DataSet

Hi,

Is it possible to add a custom row to a dataview? Here is an example of what I'm trying to do. A dataset is returned from a SQL query with two columns, ID and Name. I want to add a third column at runtime, for example Age. This data comes from somewhere else, say XML or some other datastore.

I am writing this out to DataList using the DataBinder.Eval method. How can I bind this external data to this datalist for each item? Is there another way to use DataBinder.Eval to set data from some other datasource that the one binded to the datalist? Can I add a row to the dataset's results before I pass it to the DataView?

-- shawn
 
Old August 9th, 2004, 02:25 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Are you asking about adding a row or a column?

You can add a column to a datatable after you have retrieved it.
DataTable.Columns.Add(...)
 
Old August 9th, 2004, 04:38 PM
Authorized User
 
Join Date: Aug 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, that was the trick - adding the column to the row in the DataTable, not the dataview or the dataset :)

What I am trying to do is calculate someones age based on 3 database columns of the int type, their birth day, month, and year. Here is my current calculation:

            // Fill Dataset
            sqlCmd.Fill(ds, "Member");

            // DataTable is created here
            DataTable dt = ds.Tables[0];

            // Add an "Age" column
            dt.Columns.Add("HowOld");

            // Calculate their age
            for (int i=0; i<dt.Rows.Count; i++)
            {
                dt.Rows[i]["HowOld"] = DateTime.Now.Year - (int)dt.Rows[i]["bday_year"];
            }


This doesn't take in account their birthday day being present in the current year though. Any idea how to accomindate that?

Thanks,

-- shawn

 
Old August 10th, 2004, 07:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You need to take into account the month and day that a person was born. If you store the whoe date as a DateTime field, DateTime has a .ToShortDateString() value which returns the date in 1/1/2000 format. Then you could parse it apart via .ToShortDateString().Split("/"c). If the month and day (for the bday) is after the current month and day, then it hasn't occurred yet and subtract one from the entire calculation for how old. Otherwise, if the month and day is on or before the current month/day, then don't subtract one, as their birthday has past.

Hope this helps,

Brian





Similar Threads
Thread Thread Starter Forum Replies Last Post
adding all the rows to get a total value KeviJay SQL Language 2 May 29th, 2008 03:22 AM
Fetching rows in dataset lily611 C# 4 April 27th, 2006 12:37 AM
Adding new rows to Datagrid MixedCode General .NET 1 April 10th, 2005 05:00 PM
NullReferenceException updating a DataSet rows yoord BOOK: Beginning ASP.NET 1.0 7 September 1st, 2004 03:49 PM
adding rows to datatable forkhead ADO.NET 2 March 18th, 2004 01:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.