Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

DataTable question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ApeX - 24 Jan 2008 14:00 GMT
hey guys, i got a problem concerning a datatable...so i have a table
like

Type  City  Value
------------------
PO    XX       23
PR    XX       20
PO    XY       11
PR    XY       15
PO    YY       18

OK, now i need to make a new datatable
that's gonna contain sumed values and PO and PR will be diffenrent
columns...
Like this...

Type  City  PO   PR    Value
----------------------------
PO    XX    23   20     43
PR    XY    11   15     26
PO    YY    18    0     18

..please help if you know any solutions..Thanx!
Analizer1 - 24 Jan 2008 17:13 GMT
Im not Sure of What your doing.....
are u client side, server side, in a Stored Proc

if client side , I generallly use the schema of the table
to create my 2nd table and add the columns required

below is a smal sample using datareader -> directly into a table
/// <summary>

/// Ads some Functionality

/// does not close reader here ..mite call readToTable many times for one
Read into multiple tables

/// from multiple selects from one Stored Proc

/// user of this code must close the reader

/// </summary>

public static class MySqlReader

{

/// <summary>

/// Reades the SqlReader Result Set into A Datatable

/// Provide a New DataTable or Existing Datatable

/// this Routing only Adds Columns on a New DataTable

/// </summary>

/// <param name="Reader"></param>

/// <param name="UserTable"></param>

/// <returns></returns>

public static int ReadToTable(SqlDataReader Reader, DataTable UserTable)

{

//dont Add columns if they Already Exist only if this incomming usertable is
a empty Datatableclass

if (UserTable.Columns.Count == 0)

{

DataTable Schema = Reader.GetSchemaTable();

DataRow Row;

DataColumn Col;

//define the Columns and Add to User Table

for (int x = 0; x < Schema.Rows.Count; x++)

{

Row = Schema.Rows[x];

string ColumnName = (string)Row["ColumnName"];

Col = new DataColumn(ColumnName, (Type)Row["DataType"]);

UserTable.Columns.Add(Col);

}

}

object[] ColumnValues = new Object[Reader.FieldCount];

while (Reader.Read())

{

Reader.GetValues(ColumnValues);

UserTable.LoadDataRow(ColumnValues, true);

}

return UserTable.Rows.Count;

}

/// <summary>

/// Creates New Table When Called

/// </summary>

/// <param name="Reader"></param>

/// <returns></returns>

public static DataTable ReadToTable(SqlDataReader Reader)

{

// Creates a New Table Each Time...lets say this is used by a service

// if Result Set changes we need a new table

DataTable NewTable = new DataTable();

DataTable Schema = Reader.GetSchemaTable();

DataRow Row;

DataColumn Col;

//define the Columns and Add to User Table

for (int x = 0; x < Schema.Rows.Count; x++)

{

Row = Schema.Rows[x];

string ColumnName = (string)Row["ColumnName"];

Col = new DataColumn(ColumnName, (Type)Row["DataType"]);

NewTable.Columns.Add(Col);

}

object[] ColumnValues = new Object[Reader.FieldCount];

while (Reader.Read())

{

Reader.GetValues(ColumnValues);

NewTable.LoadDataRow(ColumnValues, true);

}

return NewTable;

}

/// <summary>

/// Creates and Defines columns for a new DataTable from Table Schema

/// Returns new Table;

/// </summary>

/// <param name="Reader"></param>

/// <returns></returns>

public static DataTable CreateTable(SqlDataReader Reader)

{

DataTable Schema = Reader.GetSchemaTable();

DataTable NewTable = new DataTable();

DataColumn Col = new DataColumn();

for (int x = 0; x < Schema.Rows.Count; x++)

{

DataRow row = Schema.Rows[x];

string ColumnName = (string)row["ColumnName"];

Col = new DataColumn(ColumnName, (Type)row["DataType"]);

NewTable.Columns.Add(Col);

}

return NewTable;

}

}

> hey guys, i got a problem concerning a datatable...so i have a table
> like
[quoted text clipped - 19 lines]
>
> ..please help if you know any solutions..Thanx!

Free Magazines

Get 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 ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.