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# / March 2008

Tip: Looking for answers? Try searching our database.

dataAdapter InsertCommand problems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
J - 28 Feb 2008 06:00 GMT
I previously used the following which works fine:

SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(daAccount);
daAccount.Update(dsAccount, "Account");

But now I also want to retrieve the resulting identity column value from an
INSERT.  So I tried the following:

daAccount.InsertCommand.CommandText = "INSERT into account (userid,
organization, groupdesc, stage, sizepotential) VALUES (@userid,
@organization, @groupdesc, @stage, @sizepotential); Select accountid =
SCOPE_IDENTITY()";
daAccount.Update(dsAccount, "Account");

But the InsertCommand returns a "NullReferenceException...Object reference
not set to an instance of an object".  The dataAdapter is not null as it
worked in the first case (also "? daAccount == null" returns false).  It's
not clear what the exception is telling me.  Secondly, how does the
dataAdapter.Update know to plug in the correct dataSet columns into the
corresponding @valuename's?

Many thanks
J - 28 Feb 2008 15:11 GMT
Sorry, I'd mistakenly posted in c# where I wanted it in asp.net.

>I previously used the following which works fine:
>
[quoted text clipped - 18 lines]
>
> Many thanks
viepia@nospam.com - 05 Mar 2008 14:05 GMT
I use this SPROC
ALTER PROCEDURE [dbo].[GetSysIdentity]
@TableName VARCHAR(64),
@LastIdentity BIGINT OUTPUT
AS
BEGIN
    SELECT @LastIdentity = IDENT_CURRENT(@TableName)
    DECLARE @TestIdentiy BIGINT  
    SELECT @TestIdentiy = scope_identity()
    IF @TestIdentiy <> @LastIdentity
        RETURN 1
    RETURN 0
END

this way
Trace.Assert(db.GetSysIdentity("MyTable", ref lastIdentity) == 0, "Bad SPROC");

so far it has always returned 0 and set LastIdentity to the correct value.   I think
scope_identity works because all LINQ requests  in SQL Server Profiler show the same SPID,
ClientProcessID,  and ApplicationName: ".Net SqlClient Data Provider".

In your example "userid"  is the  _columnName of the "userid" column of your dataset.
Viepia

 

>I previously used the following which works fine:
>
[quoted text clipped - 18 lines]
>
>Many thanks

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.