p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ADO.NET (http://p2p.wrox.com/forumdisplay.php?f=109)
-   -   converting and sending data problem (http://p2p.wrox.com/showthread.php?t=55222)

csharpa March 11th, 2007 09:38 AM

converting and sending data problem
 
Hi,
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?

peace95 March 11th, 2007 02:13 PM

csharpa:
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.

erictamlam March 24th, 2007 06:38 AM

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.


ERIC


All times are GMT -4. The time now is 04:02 PM.

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