0

I've made my entire database for my application using only VS2012 express (I'm using localdb), so it handled creating the database as well as creating datasets and table adapters for my application to use. It's worked very well so far. I've also made wrapper classes to allow asynchronous database reads, which is also working great.

Looking at my table adapters in the designer, when I added my insert methods (for example on the Races table) it used the following query:

INSERT INTO Races (ChampionshipID, Driver1, Driver2, RaceTime)
VALUES        (@ChampionshipID,@Driver1,@Driver2,@RaceTime); 
SELECT ID, ChampionshipID, Driver1, Driver2, RaceTime FROM Races WHERE (ID = SCOPE_IDENTITY())

I see it does a select afterwards to return the row that was just inserted. That's perfect for what I need. However, the actual method it generates has the following signature:

int Insert1(long ChampionshipID, long Driver1, long Driver2, DateTime RaceTime)

It's returning an int and not the expected DataRow or DataTable, nor does it take a DataTable as a parameter to insert it into.

Am I doing something wrong? How can I implement this functionality?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
ldam
  • 4,412
  • 6
  • 45
  • 76
  • To clarify, the SELECT *does* read the row back *into the dataset*, but it doesn't return the row to the calling program. This means that any identity or timestamp fields that have been updated by the INSERT are available in the dataset to be read. – peterG Jan 26 '14 at 02:04

1 Answers1

1

The answer is no because, insert method uses the ExcecuteNonQuery method of ADO.NET, that's why your insert method is returning an int with the id of the record inserted.

You can try querying something like select @@identity just after inserting, so then you'll be able to get the record later.

Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92
  • Yeah, I tried changing the execute type to `Reader` and that broke everything, something about a custom tool having a `NullReferenceException`. I'll give this a shot, thank you :) – ldam Jan 26 '14 at 06:36