Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
|
SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server ASP 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 December 22nd, 2006, 01:18 PM
Authorized User
 
Join Date: Jun 2005
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQLDataSource.InsertParameter

Hi,

I m using VS 2005 - ASP.net with C#
i have declared a sqldatasource to insert values in a table, lets say table1. One of the attribute i wanna insert is an FK, which is the PK of some other table, lets say table2. Now the problem is that, to get the value of this FK to insert in table1, i have to run other sql SELECT query, which will fetch/select that FK value from table2 using given info.
How can i do this, as nested queries are not supported by datasource.I have already tried this.

What i hav tried till now is that declared a separate datasource & fetch that FK value by datasource.select() method, but its return type is IEnumerable.
Here is short pic of my code:

IEnumerable locId = datasource.Select(DataSourceSelectArguments.Empty) ;
IEnumerator locationId = locId.GetEnumerator();
locationId.MoveNext();
DataSource.InsertParameters.Add("LocId", locationId.Current.ToString());
DataSource.Insert();


It gives error while executing DataSource.Insert() line, & says INPUT STRING IS NOT IN CORRECT FORMAT. I have already checked & found that everything else is fine .. the problem only occurs while converting IEnumerator into string .. & then into int(while inserting into table)
My FK's datatype is Int32.

Plz Help.

Regards,
Shazia.
 
Old January 30th, 2007, 03:52 PM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In your InsertCommand of your SqlDataSource, you can do an INSERT INTO... SELECT FROM in one statement, where you can pass the values you want to insert to your table from one of your controls and the FK value from another table:

INSERT INTO Table1 ( Col1, COl2, FKCol )
SELECT @Col1, @Col2, PKCol
FROM Table2

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQLDatasource cymerman ASP.NET 1.0 and 1.1 Basics 1 February 28th, 2008 12:38 AM
sqldatasource imjayakumar ASP.NET 2.0 Professional 3 May 21st, 2007 01:56 PM
sqldatasource imjayakumar Crystal Reports 0 May 19th, 2007 03:12 AM
sqldatasource imjayakumar ASP.NET 2.0 Professional 0 May 19th, 2007 02:58 AM
SQLDataSource bmains ASP.NET 2.0 Basics 1 December 19th, 2004 09:12 AM





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