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

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesGetting newly created rowid in the dbGetting newly created rowid in the db
Previous
 
Next
New Post
1/16/2010 9:54 AM
 

Yen,

Your help was great, I really appreciate it.

Here is how it worked for me now in case someone else needs it:

Yen, if you think it should be written otherwise please feel free to make notes for others (and me of course)


DataProvider.vb
        Public MustOverride Function ATR_PSCH_Questions_Add_GetNewRowID(ByVal Q_Text As String, ByVal Q_Status As Integer, ByVal Q_Level_AVG As Decimal, ByVal Q_Quality_AVG As Decimal, ByVal Q_Created_By As Integer, ByVal Q_Create_Time As Date, ByVal Q_Last_Upd_By As Integer, ByVal Q_Last_Upd_Time As Date, ByVal Q_Is_Example As Boolean, ByVal Q_Bundle As Integer, ByVal Q_Type As Integer, ByVal Q_Ans_Explanation As String, ByVal Q_Pic1 As Integer, ByVal Q_Pic2 As Integer, ByVal Q_Pic3 As Integer, ByVal newQID As Integer) As String
  
SqlDataProvider.vb
        Public Overrides Function ATR_PSCH_Questions_Add_GetNewRowID(ByVal Q_Text As String, ByVal Q_Status As Integer, ByVal Q_Level_AVG As Decimal, ByVal Q_Quality_AVG As Decimal, ByVal Q_Created_By As Integer, ByVal Q_Create_Time As Date, ByVal Q_Last_Upd_By As Integer, ByVal Q_Last_Upd_Time As Date, ByVal Q_Is_Example As Boolean, ByVal Q_Bundle As Integer, ByVal Q_Type As Integer, ByVal Q_Ans_Explanation As String, ByVal Q_Pic1 As Integer, ByVal Q_Pic2 As Integer, ByVal Q_Pic3 As Integer, ByVal newQID As Integer) As String
            Return CType(SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("ATR_PSCH_Questions_Add_GetNewRowID"), Q_Text, Q_Status, Q_Level_AVG, Q_Quality_AVG, Q_Created_By, Q_Create_Time, Q_Last_Upd_By, Q_Last_Upd_Time, Q_Is_Example, Q_Bundle, Q_Type, Q_Ans_Explanation, Q_Pic1, Q_Pic2, Q_Pic3, newQID), String)
        End Function

PsychoAddQController.vb
        Public Function ATR_PSCH_Questions_Add_GetNewRowID(ByVal objATR_PSCH_Questions_Add As PsychoAddQInfo, ByVal newQID As Integer) As String
            Return (DataProvider.Instance().ATR_PSCH_Questions_Add_GetNewRowID(objATR_PSCH_Questions_Add.Q_Text, objATR_PSCH_Questions_Add.Q_Status, objATR_PSCH_Questions_Add.Q_Level_AVG, objATR_PSCH_Questions_Add.Q_Quality_AVG, objATR_PSCH_Questions_Add.Q_Created_By, objATR_PSCH_Questions_Add.Q_Create_Time, objATR_PSCH_Questions_Add.Q_Last_Upd_By, objATR_PSCH_Questions_Add.Q_Last_Upd_Time, objATR_PSCH_Questions_Add.Q_Is_Example, objATR_PSCH_Questions_Add.Q_Bundle, objATR_PSCH_Questions_Add.Q_Type, objATR_PSCH_Questions_Add.Q_Ans_Explanation, objATR_PSCH_Questions_Add.Q_Pic1, objATR_PSCH_Questions_Add.Q_Pic2, objATR_PSCH_Questions_Add.Q_Pic3, newQID))
        End Function  
 

Thank you,

Yehuda


Yehuda Tiram
AtarimTR
AtarimTR
972-2-5700114   |   972-54-4525492   |    http://www.atarimtr.co.il
 
New Post
1/17/2010 2:11 PM
 

Yehuda,

Just checking as it's not completely clear -- GetNewRowID is a separate call to the database AFTER the insert?  One issue to consider is the possibility of someone adding a record at the exact time you are ... the second call to get the GetNewRowID may possibly get the other row.   Depends on your site ... but possible.

In such a case I return the new row id as the status of the Create. 

Here is the stored procedure .... return 0 or the row id ..

CREATE PROCEDURE CreateCarrierContacts
 @CustomerCode as varchar(5),
    @CarrierCode as varchar(10),
    @Name as varchar(50),
    @Role as varchar(100),
    @Email as varchar(100),
    @WebSite as varchar(100),
    @Contact1 as varchar(250),
    @Contact2 as varchar(250),
    @CreatedDT as datetime
AS

Set NoCount On
Declare @Rtn as Int

Insert FZ_CarrierContacts with (ROWLOCK) (
    [CustomerCode], [CarrierCode], [Name], [Role], [Email], [WebSite], 
    [Contact1], [Contact2], [CreatedDT] )
Values (
    @CustomerCode, @CarrierCode, @Name, @Role, @Email, @WebSite,
    @Contact1, @Contact2, @CreatedDT )

Set @Rtn = 0
If @@RowCount = 1   -- update successful
  Begin
    Set @Rtn = cast(SCOPE_IDENTITY() as int)
  End
Return @Rtn

So the Function then becomes the following...  it return an Integer value of the New row ID

 Public Overrides Function CreateCustomers(ByVal psCustomerCode As String, ByVal psName As String,
     ByVal psAddress1 As String, ByVal psAddress2 As String, 
  ByVal psCityCode As String, ByVal psPostalCode As String) As Integer

  Dim sProc As String = DatabaseOwner & ObjectQualifier & "CreateCustomers"
  Return CType(SqlHelper.ExecuteScalar(ConnectionString, sProc, psCustomerCode, psName, 
               psAddress1, psAddress2, psCityCode, psPostalCode, System.DateTime.Now),
               Integer)
End Function

Here is how I use the example.

    oInfo.ItemID = SavedItemID
    oInfo.CustomerCode = txtCustomerCode.Text
    oInfo.Name = txtName.Text
    oInfo.Address1 = txtAddress1.Text
    oInfo.Address2 = txtAddress2.Text    
    oInfo.CityCode = ctlCityCode.SelectedCityCode

         oInfo.PostalCode = txtPostalCode.Text
    oInfo.ModifiedDT = txtModifiedDT.Text
    oInfo.CreatedDT = txtCreatedDT.Text

    If SavedItemID < 0 Then
        SavedItemID = oController.Create(oInfo)
        oInfo = oController.ReadByItemID(SavedItemID)
    Else
        oController.Update(oInfo)
    End If

Notice that I user the Returned value to read the newly inserted record. 

Hope this helps
Paul.

 

 
New Post
1/17/2010 5:55 PM
 

Hi Yehuda,

Glad to have been of assistance.

All the best

regards

 
New Post
1/20/2010 2:58 AM
 

Paul,

Thanks. Sorry for the delayed response, the forum's notification does not notify somehow.

The motivation for my initial question was the fact that I must get the current user's newly inserted rowID of course.

   Public Function ATR_PSCH_Questions_Add_GetNewRowID is the actual insert function. I do not fully understand the theory behind it but after I used the code Yen sent and played with it it worked (my original sub was replaced with the new function I posted)

If i get it right, the main part of your code is:

Set @Rtn = 0
If @@RowCount = 1   -- update successful
  Begin
    Set @Rtn = cast(SCOPE_IDENTITY() as int)
  End
Return @Rtn

where you reset the rows count and then you check the counter to see if it counts only 1

Is that correct?

Also, I thought that the sql code

SELECT cast(SCOPE_IDENTITY() as int) AS newQID
 

returns the current insert new id, isn't that correct?

Yehuda

 

Paul Scarlett wrote:
 

Yehuda,

Just checking as it's not completely clear -- GetNewRowID is a separate call to the database AFTER the insert?  One issue to consider is the possibility of someone adding a record at the exact time you are ... the second call to get the GetNewRowID may possibly get the other row.   Depends on your site ... but possible.

In such a case I return the new row id as the status of the Create. 

Here is the stored procedure .... return 0 or the row id ..

CREATE PROCEDURE CreateCarrierContacts
 @CustomerCode as varchar(5),
    @CarrierCode as varchar(10),
    @Name as varchar(50),
    @Role as varchar(100),
    @Email as varchar(100),
    @WebSite as varchar(100),
    @Contact1 as varchar(250),
    @Contact2 as varchar(250),
    @CreatedDT as datetime
AS

Set NoCount On
Declare @Rtn as Int

Insert FZ_CarrierContacts with (ROWLOCK) (
    [CustomerCode], [CarrierCode], [Name], [Role], [Email], [WebSite], 
    [Contact1], [Contact2], [CreatedDT] )
Values (
    @CustomerCode, @CarrierCode, @Name, @Role, @Email, @WebSite,
    @Contact1, @Contact2, @CreatedDT )

Set @Rtn = 0
If @@RowCount = 1   -- update successful
  Begin
    Set @Rtn = cast(SCOPE_IDENTITY() as int)
  End
Return @Rtn

So the Function then becomes the following...  it return an Integer value of the New row ID

 Public Overrides Function CreateCustomers(ByVal psCustomerCode As String, ByVal psName As String,
     ByVal psAddress1 As String, ByVal psAddress2 As String, 
  ByVal psCityCode As String, ByVal psPostalCode As String) As Integer

  Dim sProc As String = DatabaseOwner & ObjectQualifier & "CreateCustomers"
  Return CType(SqlHelper.ExecuteScalar(ConnectionString, sProc, psCustomerCode, psName, 
               psAddress1, psAddress2, psCityCode, psPostalCode, System.DateTime.Now),
               Integer)
End Function

Here is how I use the example.

    oInfo.ItemID = SavedItemID
    oInfo.CustomerCode = txtCustomerCode.Text
    oInfo.Name = txtName.Text
    oInfo.Address1 = txtAddress1.Text
    oInfo.Address2 = txtAddress2.Text    
    oInfo.CityCode = ctlCityCode.SelectedCityCode

         oInfo.PostalCode = txtPostalCode.Text
    oInfo.ModifiedDT = txtModifiedDT.Text
    oInfo.CreatedDT = txtCreatedDT.Text

    If SavedItemID < 0 Then
        SavedItemID = oController.Create(oInfo)
        oInfo = oController.ReadByItemID(SavedItemID)
    Else
        oController.Update(oInfo)
    End If

Notice that I user the Returned value to read the newly inserted record. 

Hope this helps
Paul.

 

 

 


Yehuda Tiram
AtarimTR
AtarimTR
972-2-5700114   |   972-54-4525492   |    http://www.atarimtr.co.il
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesGetting newly created rowid in the dbGetting newly created rowid in the db


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