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/14/2010 8:44 PM
 

 Hi,
I need to insert a new row in a table and get the newly created rowid at the same time.
I have this SPROC:
==============
USE [dnn520]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Questions_Add]
(@Q_Text ntext)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[Questions]
(Q_Text)
VALUES
(@Q_Text)
 

-- Get the newly created rowID

SELECT cast(SCOPE_IDENTITY() as int) AS newQID
END
==============

newQID is the new rowID.
This works in mssql studio and when I run the SPROC it inserts the new row and displays the new rowID.

Now I need to set the procedure that handles this in DataProvider.vb, AddQController.vb and SqlDataProvider.vb to do the insert and getting the rowID at the same query to the DB.
I use these code snippets:
in  DataProvider.vb:
...
 


Public MustOverride Sub Questions_Add(ByVal Q_Text As String)


...
In SqlDataProvider.vb :
...
 


Public Overrides Sub Questions_Add(ByVal Q_Text As String)
SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("Questions_Add"), Q_Text)
End Sub


...
 


in PsychoAddQController.vb:
Public Sub Questions_Add(ByVal objQuestions_Add As AddQInfo)
DataProvider.Instance().Questions_Add(objQuestions_Add.Q_Text)
End Sub


...

 

How do I do this?
Thanks,
Yehuda

 

 


Yehuda Tiram
AtarimTR
AtarimTR
972-2-5700114   |   972-54-4525492   |    http://www.atarimtr.co.il
 
New Post
1/15/2010 1:04 AM
 

Hi Yehuda,

I'm not sure I totally undestand what you are asking but I'll take a shot...

Now that you've inserted a new row in the db you want to be able to use that in your Business Logic or display it on the page, yes?

If that's the case you need to modify the following:

Public MustOverride Sub Questions_Add(ByVal Q_Text As String) becomes 
Public MustOverride Function Questions_Add(ByVal Q_Text As String) as String
... In SqlDataProvider.vb : ...   Public Overrides Sub Questions_Add(ByVal Q_Text As String) becomes
Public Overrides Function Questions_Add(ByVal Q_Text As String) as String 
in PsychoAddQController.vb:

Public Sub Questions_Add(ByVal objQuestions_Add As AddQInfo) becomes

Public Function Questions_Add(ByVal objQuestions_Add As AddQInfo) as String
DataProvider.Instance().Questions_Add(objQuestions_Add.Q_Text) becomes
Dim qText as String = DataProvider.Instance().Questions_Add(objQuestions_Add.Q_Text) End Sub becomes End Function Make sure you end each function with returns Q_text too Let me know if that helps
regards

 
New Post
1/15/2010 6:42 AM
 

Hello Yen,

Thank you for taking the time to make it so clear.

I will try that but have just one note. The code supposed to insert data to the DB and uses the regullar procedure to do that task. The need to mix insert and select commands in the same query and how it works is not clear to me.

I'll appreciate if you could elaborate about how it works.

Anyway, I'll put the changed code and will inform you.

Thanks,

Yehuda


Yehuda Tiram
AtarimTR
AtarimTR
972-2-5700114   |   972-54-4525492   |    http://www.atarimtr.co.il
 
New Post
1/15/2010 9:15 AM
 

Hello Yen,

I tried to implement the code but immediatly noticed that in order to simplify things I missled you.

The query inserts more than 1 parameter, actually 12 of them and need to return only 1 parameter, the newly created rowID.

Now, my problem is that using the code you suggested does not allow me to do that.

Any idea about this issue?

Thanks,

Yehuda


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

Hi Yehuda,

I'll explain it using 1 argument and then you can expand on the functions and stored procedure to accept 12 arguments.

Firstly, you need further modify your SqlDataProvider and DataProvider classes.

SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("Questions_Add"), Q_Text) becomes

Return ctype(SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("Questions_Add"), Q_Text)
,string)

Execute Scalar will accept the integer from SELECT cast(SCOPE_IDENTITY() as int) AS newQID in your stored proc
And the Return will pass it back to the calling function Hope that makes sense regards

 
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