Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Return Identity value for new recordReturn Identity value for new record
Previous
 
Next
New Post
2/9/2008 7:23 PM
 

I am developing a module using the standard DNN model of DateProvider, SqlDataProvider, DataInfo and DataController classes.

I have a need to save a new record to my database and capture the identity value of the new record (output value returned by the stored procedure) to save additional data in related tables for the new record.

I'm stuck at the DataController class getting the correct structure to return the output value from the stored procedure.

I would appreciate any ideas - even code samples that show this in action.

HELP!!

Randy Land

 

 
New Post
2/10/2008 2:55 PM
 

Randy,  If you followed the standard DNN coding model what you want to do should be no problem.  I will illustrate below some sample code.  First, the very last thing that should be in your stored procedure after the insert should be the statement "select SCOPE_IDENTITY()".  This will return to the caller the id of the record inserted.

/*
    Your SQL Stored Procedure...
*/
ALTER PROCEDURE [dbo].[AddName]
 @PortalID int,
 @ModuleID int,
 @FirstName nvarchar(64),
 @LastName nvarchar(64),
AS

INSERT INTO Names (
 [PortalID],
 [ModuleID],
 [FirstName],
 [LastName]
) VALUES (
 @PortalID,
 @ModuleID,
 @FirstName,
 @LastName
)

select SCOPE_IDENTITY()

Next, all the methods in your abstract data provider class and concrete data provider class should be written as functions that return an integer which is the SCOPE IDENTITY that the stored procedure returns.

/*
    Your modules concrete SQLDataProvider Class (SQLDataProvider.vb)...
*/
Public Overrides Function AddName(ByVal portalID As Integer, ByVal moduleID As Integer, ByVal firstName As String, ByVal lastName As String) As Integer
  Return CType(SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("AddName"), portalID, moduleID, firstName, lastName), Integer)
End Function

/*
    Your modules abstract DataProvider Class (DataProvider.vb)...
*/
Public MustOverride Function AddName(ByVal portalID As Integer, ByVal moduleID As Integer, ByVal firstName As String, ByVal lastName As String) As Integer

/*
    Your modules Controller Class (NamesController.vb)...
*/
  Public Function AddName(ByVal objInfo As AgentInfo) As Integer
      Return CType(DataProvider.Instance().AddAgent(objInfo.PortalID, objInfo.ModuleID, objInfo.FirstName, objInfo.LastName), Integer)
  End Function

 

The only place that you need to make the change is in the code behind of your module's edit control (EditNames.ascx.vb).  Typically , the event handler of the Update button simply calls the Controller's "Add" method not setting anything to the return value.  You would change that as indicated below.

/*
    Your modules Code Behind File (EditForm.ascx.vb)
*/

  '-- Add or Update the record (not capturing the record id).
  If RecordID = -1 Then
      objController.AddName(objInfo)
  Else
      objController.UpdateName(objInfo)
  End If

  '-- Redirect back to the caller.
  Response.Redirect(NavigateURL())

Modify your code to set an integer variable equal to the results of the call to the Add function.  On a successful insert the value  will contain the record ID of the new record.  I believe that in the case of an error the returned value will be a negetive integer  representing the error code.


  '-- Add or Update the record.
  Dim intNewRecordId As Integer
  If RecordID = -1 Then
      intNewRecordId = objController.AddName(objInfo)
  Else
      objController.UpdateName(objInfo)
  End If

  '-- Redirect back to the caller.
  Response.Redirect(NavigateURL())

That should do it!!  Now that you've got the record id of the new record you can do whatever you need to with it.  With all the above code, only the stuff highlighted in blue is actually the change to the standard DNN coding methods. 

I hope this helps,

Chuck R.

 
New Post
2/11/2008 8:52 AM
 

I was on the right track - you solved a syntax issue for me in the Controller class.

Thanks Chuck!!

 
New Post
2/11/2008 10:11 PM
 

Glad to have been some help.  Happy coding!!

Chuck R.

 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Return Identity value for new recordReturn Identity value for new record


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out