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 8836725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:33:38+00:00 2026-06-14T09:33:38+00:00

I need to fetch scope_identity() value from a stored procedure after inserting data in

  • 0

I need to fetch scope_identity() value from a stored procedure after inserting data in C# code.

Here I am using 3 tier architecture. Please anyone help me

protected void Submit_Click(object sender, EventArgs e)
{
    this.CreateUserGroups(strGroupName, strDescription, strGroupType);
}

protected void CreateUserGroups(string strGroupName, string strDescription, string strGroupType)
{
    string strReturn = string.Empty;
    strReturn = objUser.SaveCreateGroups(strGroupName, strDescription, strGroupType);
}

public string SaveCreateGroups(string strGroupName, string strDescription, string strGroupType)
{
    try
    {
        DataManager.ParamBuilder param = new DataManager.ParamBuilder();
        if (strGroupName != string.Empty)
            param.AddParam(System.Data.SqlDbType.VarChar, "@c_groupname", strGroupName);
        else
            param.AddParam(System.Data.SqlDbType.Int, "@c_groupname", DBNull.Value);

        if (strDescription != string.Empty)
            param.AddParam(System.Data.SqlDbType.VarChar, "@c_description", strDescription);
        else
            param.AddParam(System.Data.SqlDbType.Int, "@c_description", DBNull.Value);

        if (strGroupType != string.Empty)
            param.AddParam(System.Data.SqlDbType.Int, "@n_grouptype", strGroupType);
        else
            param.AddParam(System.Data.SqlDbType.VarChar, "@n_grouptype", DBNull.Value);


        String strIsUserExitsInGroup = DataManager.ExecuteScalar("sp_create_user_groups", param.Parameters, true).ToString();

        return strIsUserExitsInGroup;
    }
    catch (Exception e)
    {
        throw new Exception(e.ToString());
    }
}

public static object ExecuteScalar(string procedureNameOrSql, List<SqlParameter> parameters, bool isStoredProcedure)
{
    object scalarValue;
    using (SqlConnection cn = createConnection())
    {
        SqlCommand cmd = new SqlCommand(procedureNameOrSql, cn);
        if (isStoredProcedure)
            cmd.CommandType = CommandType.StoredProcedure;
        if (parameters != null)
            cmd.Parameters.AddRange(parameters.ToArray());
        scalarValue = cmd.ExecuteScalar();
    }
    return scalarValue;
}

My stored procedure:

CREATE Procedure [dbo].[sp_create_user_groups]
  @n_user_group_id int OUTPUT,
  @c_groupname varchar(200),
  @c_description varchar(200),
  @n_grouptype int
As
   Declare @IsGroupNameExists int,        
           @b_active bit

   select @IsGroupNameExists = 0
   set @b_active = 1

BEGIN
IF exists(select top 1 * from gdt_user_groups where c_group_name = @c_groupname)
BEGIN
 select @IsGroupNameExists = 1
END
ELSE
 BEGIN
    SET NOCOUNT ON;
    insert into gdt_user_groups(c_group_name,c_description,n_group_id,b_active,d_modified_dttm,
    d_created_dttm)values(@c_groupname,@c_description,@n_grouptype,@b_active,GETDATE(),GETDATE())
    select @n_user_group_id = SCOPE_IDENTITY();
    select @IsGroupNameExists = 0
 END 

 select @IsGroupNameExists
 END

Here where exactly I can fetch the the scope_identity value in C# code. Anyone help?

  • 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-14T09:33:39+00:00Added an answer on June 14, 2026 at 9:33 am

    You need to get value through output parameter, so you must follow these link:
    Get output parameter value in ADO.NET
    How to use OUTPUT parameter in Stored Procedure

    you just need to create a SqlParameter, set the Direction to Output,
    and add it to the SqlCommand’s Parameters collection. Then execute the
    stored procedure and get the value of the parameter.

    While adding parameter do as below after adding extra parameter in your method

    param.AddParam(SqlDbType.Int, "@c_Id", IdValue, ParameterDirection.Output);
    

    then access the value of your output parameter as below:

    object value = cmd.Parameters["@c_Id"].Value;
    

    Source: Getting the identity of the most recently added record
    Code Snippet:

    string query = "AddCategory";
    int ID;
    string connect = @"Server=.\SQLExpress;Database=Northwind;Trusted_Connection=Yes;";
    using (SqlConnection conn = new SqlConnection(connect))
    {
      using (SqlCommand cmd = new SqlCommand(query, conn))
      {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Category", Category.Text);
        cmd.Parameters.Add("@CategoryID", SqlDbType.Int, 0, "CategoryID");
        cmd.Parameters["@CategoryID"].Direction = ParameterDirection.Output;
        conn.Open();
        cmd.ExecuteNonQuery();
        ID = (int)cmd.Parameters["@CategoryID"].Value;
      }
    }
    

    procedure…

    CREATE PROCEDURE AddCategory
      -- Add the parameters for the stored procedure here
      @Category NVARCHAR(15),
      @CategoryID INT OUTPUT
    AS
    BEGIN
      SET NOCOUNT ON;
    
      -- Insert statements for procedure here
      INSERT INTO Categories (CategoryName) VALUES (@Category)
      SET @CategoryID = SCOPE_IDENTITY()
    END
    

    Hope the above reference help you more to understand all of this.

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

Sidebar

Related Questions

Im using vb.net and I need to fetch data from two different tables and
I need to fetch data from normalized MSSQL db and feed them in Solr
I need to fetch images and some other data from server and then display
I need to fetch data from a URL with non-ascii characters but urllib2.urlopen refuses
Need to fetch data from a few tables -table one has all product details
Lets say I need to fetch some records from the database, and filter them
Here's what I need to fetch: - posts that have comments - number of
I need to fetch data having a specific id and which id is defined
I need fetch user's friends data with next fields: id, name, gender, birthday, city,
I need to fetch revisions from SVN into GIT. I have two types of

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.