Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 March 11th, 2007, 09:38 AM
Authorized User
Join Date: Jan 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default converting and sending data problem

I want to send the FK from parent table to child table by getting the object with executescalar and converting the value of it to string and then to integer finally pass it to child table. The problem is that the converted data will lose the information. I mean it will become NULL.
This is how did I do:

insertCommand.CommandText = "SELECT @@IDENTITY AS P_ID ";

SqlParameter pIDParameter = new SqlParameter("@P_ID", SqlDbType.Int);
pIDParameter.Direction = ParameterDirection.ReturnValue;
object o = insertCommand.ExecuteScalar();
string s = o.ToString();
int P_ID = Int32.Parse(s);

what is the better way to the convertion or better way to pass it to child table?
Old March 11th, 2007, 02:13 PM
Friend of Wrox
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts

The FK (Foreign Key) in the Parent Table IS the PK (Primary Key) in the Child Table, which is created at the Database Design Phase. If this is not done, then you MUST create the RELATIONSHIP between the Parent Table and the Child Table first.

More detail information can be found in Wrox's Beginning ASP.NET Databases Using VB.NET, Defining Relationships between DataTable Objects, p 155.

Hope this helps.

Disclaimer: The above comments are solely the opinion of one person and not to be construed as a directive or an incentive to commit fraudulent acts.
Old March 24th, 2007, 06:38 AM
Authorized User
Join Date: Mar 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to erictamlam Send a message via MSN to erictamlam Send a message via Yahoo to erictamlam

You can use "DataRelation" object to create relationship between parent table and child table...of course you hafta fill DataSet with tables with DataAdapter object.
for example:Parent table:Order[primary key:"OrderID"] Child table:OrderDetail[foreign key:"OrderID"]
you can create bond between them like this: DataRelation orderRel=thisDataSet.Relations.Add("orderbond",thi sDataSet.Tables["Order"].Colums["OrderID"],thisDataSet.Tables["OrderDetail"].Colums["OrderID"]);
then use "foreach" statement..."OrderID" in Parent Table will be automatically passes to Child Table:
 for each(DataRow orderRow in thisDataSet.Tables["Order"].Rows)
   ....//parent table processing
    foreach(DataRow orderDetailRow in orderRow.GetChildRows(orderRel) )
    .....//Child table processing
sometimes .GetParentRows can also come into handy.
Hope this can help.


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in e-mail structure sending data from data tiawebchd General .NET 3 May 5th, 2008 08:07 AM
Converting an XML data island to 2 arrays NeilAtWork Classic ASP XML 0 April 14th, 2004 10:38 AM
Converting int64 data type in VB6 yeeck VB How-To 1 December 9th, 2003 10:13 PM
Converting int64 data type in VB6 yeeck Beginning VB 6 0 November 27th, 2003 11:32 PM

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