Return Newely Created ID of Record using Typed Datasets


When Using ADO.net typed datasets, Visual Studio Automatically creates the Update, and Insert Methods, however the return integer value indicate if the record was added successfully or not, in order to get the ID of the newely created entry one can access the ID field of the original dataset that was used to add contents to the database.

In order to make this happen one needs to enable “Refresh the data table” in Advanced Options

once done, on update the source datatable contents will be updated with the inserted Id of the new record.

[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
public int AddModule(dsDomainModules.tblDomainModulesRow Module)
{
int nResult = 0;
try
{
dsDomainModules.tblDomainModulesDataTable objModule = new dsDomainModules.tblDomainModulesDataTable();
objModule.AddtblDomainModulesRow(Module);
if (_dsDomainModulesTableAdapter.Update(objModule) == 1) //update was successful
nResult = Module.nModuleId;
}
catch (Exception ex)
{
//do something with exception
}
return nResult;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s