Jes Sie

Jes Sie

  • 741
  • 1.2k
  • 281.9k

Get the ID of a Newly Inserted Row

Jan 25 2018 3:54 AM
Hi everyone, I need to get the ID of a newly inserted row. And I was able to create an SP from the SQL Server. Please see below:
  1. ALTER PROCEDURE [dbo].[spInser_CategoryHeader]   
  2.     -- Add the parameters for the stored procedure here  
  3.     (  
  4.         @SchoolYear nvarchar(50),  
  5.            @Term nvarchar(50),  
  6.            @GradeLvl nvarchar(50),  
  7.            @SubjectCode nvarchar(50),  
  8.            @TotalCategoryPercentage decimal(5,2),  
  9.            @SchoolCode nvarchar(50),  
  10.            @getUser nvarchar(50),  
  11.            @CategoryHeaderID int OUTPUT  
  12.            )  
  13. AS  
  14. BEGIN  
  15.     -- SET NOCOUNT ON added to prevent extra result sets from  
  16.     -- interfering with SELECT statements.  
  17.     SET NOCOUNT ON;  
  18.   
  19.     -- Insert statements for procedure here  
  20.     BEGIN TRANSACTION  
  21.     IF NOT EXISTS (SELECT CategoryHeaderID FROM tblCategoryHeader WHERE CategoryHeaderID=@CategoryHeaderID)  
  22.         BEGIN  
  23.             INSERT INTO [dbo].[tblCategoryHeader]  
  24.                ([SchoolYear]  
  25.                ,[Term]  
  26.                ,[GradeLvl]  
  27.                ,[SubjectCode]  
  28.                ,[TotalCategoryPercentage]  
  29.                ,[SchoolCode]  
  30.                ,[getUser])  
  31.   
  32.                 VALUES  
  33.                 (@SchoolYear,   
  34.                 @Term,   
  35.                 @GradeLvl,   
  36.                 @SubjectCode,   
  37.                 @TotalCategoryPercentage,  
  38.                 @SchoolCode,   
  39.                 @getUser)  
  40.   
  41.                 SELECT @CategoryHeaderID = SCOPE_IDENTITY()  
  42.   
  43.         COMMIT TRANSACTION  
  44.         END  
  45.     ELSE  
  46.     BEGIN  
  47.         ROLLBACK TRANSACTION  
  48.         RAISERROR('Duplicate record found. All entries are reverted to it original state.',16,1)  
  49.     END  
  50.   
  51. END  
In my c# application, I am using a 3 tier architecture. I was able to get my objective if I will write my ADO code in my .cs file. Please see below:
 
  1. private void InsertGradeCategory()  
  2.         {  
  3.             using (SqlConnection con = DBCS.GetConString())  
  4.             {  
  5.                 con.Open();  
  6.                 SqlTransaction sqlTran = con.BeginTransaction();  
  7.                 SqlCommand cmd = con.CreateCommand();  
  8.                 cmd.Transaction = sqlTran;  
  9.                 try  
  10.                 {  
  11.                     using (SqlConnection cons = DBCS.GetConString())  
  12.                     {  
  13.                         SqlCommand cmds = new SqlCommand("spInser_CategoryHeader", cons);  
  14.                         cmds.CommandType = CommandType.StoredProcedure;  
  15.   
  16.                         cmds.Parameters.AddWithValue("@SchoolYear", ddlSchoolYear.SelectedValue);  
  17.                         cmds.Parameters.AddWithValue("@Term", ddlTerm.SelectedValue);  
  18.                         cmds.Parameters.AddWithValue("@GradeLvl", ddlGradeLvl.SelectedValue);  
  19.                         cmds.Parameters.AddWithValue("@SubjectCode", ddlSubjects.SelectedValue);  
  20.                         cmds.Parameters.AddWithValue("@TotalCategoryPercentage", lblTotalPercentage.Text);  
  21.                         cmds.Parameters.AddWithValue("@SchoolCode", lblSchoolCode.Text);  
  22.                         cmds.Parameters.AddWithValue("@getUser", lblUsername.Text);  
  23.   
  24.                         SqlParameter outputParameter = new SqlParameter();  
  25.                         outputParameter.ParameterName = "@CategoryHeaderID";  
  26.                         outputParameter.SqlDbType = SqlDbType.Int;  
  27.                         outputParameter.Direction = ParameterDirection.Output;  
  28.                         cmds.Parameters.Add(outputParameter);  
  29.   
  30.                         cons.Open();  
  31.                         cmds.ExecuteNonQuery();  
  32.   
  33.                         string empId = outputParameter.Value.ToString();  
  34.                         hfCategoryHeaderId.Value = empId;  
  35.                     }  
  36.                   
  37.                 }  
  38.             }  
  39.         }  
 But I prefer writing my ADO from my DataAccess code. Here's what I started:
 
  1. public int InsertCategoryHeader(CategoryHeaderTable head)  
  2.         {  
  3.             using (SqlConnection con = DBCS.GetConString())  
  4.             {  
  5.                 SqlCommand cmd = new SqlCommand("spInser_CategoryHeader", con);  
  6.                 cmd.CommandType = CommandType.StoredProcedure;  
  7.                 con.Open();  
  8.                 try  
  9.                 {  
  10.   
  11.                     cmd.Parameters.AddWithValue("@SchoolYear", head.SchoolYear);  
  12.                     cmd.Parameters.AddWithValue("@Term", head.Term);  
  13.                     cmd.Parameters.AddWithValue("@GradeLvl", head.GradeLvl);  
  14.                     cmd.Parameters.AddWithValue("@SubjectCode", head.SubjectCode);  
  15.                     cmd.Parameters.AddWithValue("@TotalCategoryPercentage", head.CategoryPercentage);  
  16.                     cmd.Parameters.AddWithValue("@SchoolCode", head.SchoolCode);  
  17.                     cmd.Parameters.AddWithValue("@getUser", head.getUser);  
  18.   
  19.                     SqlParameter outputParameter = new SqlParameter();  
  20.                     outputParameter.ParameterName = "@CategoryHeaderID";  
  21.                     outputParameter.SqlDbType = SqlDbType.Int;  
  22.                     outputParameter.Direction = ParameterDirection.Output;  
  23.                     cmd.Parameters.Add(outputParameter);  
  24.   
  25.                     return cmd.ExecuteNonQuery();  
  26.   
  27.                     //string studId = outputParameter.Value.ToString(); //problem here 
  28.   
  29.                 }  
  30.                 catch (Exception)  
  31.                 {  
  32.   
  33.                     throw;  
  34.                 }  
  35.             }  
  36.         }  
Thanks in advance for any help.
 

Answers (3)