Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:55:15+00:00 2026-06-13T00:55:15+00:00

I have a stored procedure such as the following: CREATE PROCEDURE procTest — Add

  • 0

I have a stored procedure such as the following:

CREATE PROCEDURE procTest 
    -- Add the parameters for the stored procedure here
    @gameId int = NULL, 
    @memberID int = NULL,
    @mediatorID int = NULL,
    @id int OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO tblSiteChallengeMember (gameId, creatorId, mediatorId, completed, dateAdded) VALUES (@gameId, @memberId, @mediatorID, 0, GETDATE())
END
GO

I need to return the ID of tblSiteChallengeMember, but I have no clue how to do it.

I have seen the example ’round about here, but I am afraid that:

SELECT TOP 1 @ProductName=PRODUCTNAME, @Quantity =quantity 
FROM Products P, [Order Details] OD, Orders O, Customers C 
WHERE C.CustomerID = @CustomerID 
AND C.CustomerID = O.CustomerID AND O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID

Wont work, due to the fact that a single user could enter multiple times, and those times may not be unique at all.

How can I get the ID of the last row inserted to my ASP.net page?


Updated my Stored Procedure to:

ALTER PROCEDURE [dbo].[procCreateChallengeMember]
    -- Add the parameters for the stored procedure here
    @gameId int = NULL, 
    @memberID int = NULL,
    @mediatorID int = NULL,
    @id int OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO tblSiteChallengeMember (gameId, creatorId, mediatorId, completed, dateAdded) VALUES (@gameId, @memberId, @mediatorID, 0, GETDATE())

    SELECT @id = SCOPE_IDENTITY()
END

SQL table is:

GO

/****** Object:  Table [dbo].[tblSiteChallengeMember]    Script Date: 10/17/2012 08:05:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblSiteChallengeMember](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [gameId] [int] NULL,
    [creatorId] [int] NOT NULL,
    [mediatorId] [int] NULL,
    [completed] [tinyint] NULL,
    [dateAdded] [datetime] NULL,
 CONSTRAINT [PK__tblSiteD__3213E83F628FA481] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

aspx.cs is:

SqlConnection db = DataConn.SqlConnection();

db.Open();
SqlTransaction transaction = db.BeginTransaction();

try
{
    int id = 0;

    SqlCommand sqlComm =
            new SqlCommand(
                "procCreateChallengeMember @gameId, @creatorId, @mediatorId, @id", db, transaction) { CommandType = CommandType.Text };

    sqlComm.Parameters.Add(new SqlParameter("@gameId", SqlDbType.Int)).Value = 1;
    sqlComm.Parameters.Add(new SqlParameter("@creatorId", SqlDbType.Int)).Value = 1;
    sqlComm.Parameters.Add(new SqlParameter("@mediatorId", SqlDbType.Int)).Value = 1;
    SqlParameter param = new SqlParameter("@id", SqlDbType.Int)
        {
            Direction = ParameterDirection.Output
        };

    sqlComm.Parameters.Add(param);
    sqlComm.ExecuteNonQuery();

    //if(param.Value != DBNull.Value)
    //{
        id = Convert.ToInt32(param.Value);
        Response.Write("One: " + id + "<br/>");
    //}
    transaction.Commit();
    //Response.Write("Two: " + sqlComm.Parameters["expr"].Value + "<br/>");

}
catch (Exception ess)
{
    transaction.Rollback();
    Response.Write(ess.Message);
}

Now getting the error
Object cannot be cast from DBNull to other types. (When I remove that IF statement)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:55:16+00:00Added an answer on June 13, 2026 at 12:55 am

    Finally found a solution on my own (that works).

    SQL:

    -- =============================================
    -- Author:      Darren Whitfield
    -- Create date: 7 October 2012
    -- Description: This will create a new challenge holder, and return its ID
    -- =============================================
    ALTER PROCEDURE [dbo].[procCreateChallengeMember]
        -- Add the parameters for the stored procedure here
        @gameId int = NULL, 
        @memberID int = NULL,
        @mediatorID int = NULL,
        @id int OUTPUT
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        -- SET NOCOUNT ON;
    
        -- Insert statements for procedure here
        INSERT INTO tblSiteChallengeMember (gameId, creatorId, mediatorId, completed, dateAdded) VALUES (@gameId, @memberId, @mediatorID, 0, GETDATE())
    
        SET @id = @@IDENTITY
        SELECT @id AS iden
    END
    

    aspx.cs:

    string identity = "";
            SqlConnection db = DataConn.SqlConnection();
    
            db.Open();
            SqlTransaction transaction = db.BeginTransaction();
    
            try
            {
    
                SqlCommand sqlComm =
                        new SqlCommand(
                            "procCreateChallengeMember @gameId, @creatorId, @mediatorId, @id", db, transaction) { CommandType = CommandType.Text };
    
                sqlComm.Parameters.Add(new SqlParameter("@gameId", SqlDbType.Int)).Value = 2;
                sqlComm.Parameters.Add(new SqlParameter("@creatorId", SqlDbType.Int)).Value = 1;
                sqlComm.Parameters.Add(new SqlParameter("@mediatorId", SqlDbType.Int)).Value = 1;
                sqlComm.Parameters.Add(new SqlParameter("@id", SqlDbType.Int) { Direction = ParameterDirection.Output });
    
                using (SqlDataReader rdr = sqlComm.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        identity = rdr["iden"].ToString();
                    }
                }
    
                Response.Write("One: " + identity + "<br/>");
    
                transaction.Commit();
    
            }
            catch (Exception ess)
            {
                transaction.Rollback();
                Response.Write(ess.Message);
            }
    

    Returns ID
    Thanks to all those who took their time to put in their thoughts and opinions. Without it, I would of never made it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure which update a table based on such calculation and
I have a stored procedure in which if I write the following query without
I have a stored procedure (SP) in oracle: CREATE OR REPLACE PROCEDURE SP_SEL_LOGIN_INFO (
I have the following Stored Procedure, Im looking for the correct syntax so I
I have a stored procedure as follows: CREATE PROC [dbo].[Incidents] (@SiteName varchar(200)) AS SELECT
I have the following code in a very long stored procedure, where P equals
I have the following table structure: CREATE TABLE a ( a_id int(10) unsigned NOT
I have the following stored procedure: proc_main:begin declare done tinyint unsigned default 0; declare
I have stored procedure in my database and i need to look up a
I have a stored procedure in oracle which is using cursor.Its fetching the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.