Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

retrieve the identity value in my data layer

Posted on 2010-09-04
5
273 Views
Last Modified: 2012-05-10
hi expert, i have this store procedure
create procedure [dbo].[EvaluacionSentenciasJudicialesInsertar]
(
  @tipoDocumento char(1) ,
      @numeroDocumento varchar(50) ,
...
)
AS
SET NOCOUNT ON
INSERT INTO [EvaluacionSJ].[dbo].[EvaluacionSentenciasJudiciales]
           ([tipoDocumento]
           ,[numeroDocumento]
   ...
     
     VALUES
           (
                                    @tipoDocumento
           ,@numeroDocumento
           ...
           )
           
select @@IDENTITY

as I can do to get this code (@ @ identity) in my data layer. net?

this i my code in .net
protected void btnSave_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(ObtenerConexion());

        cmd = new SqlCommand("EvaluacionSentenciasJudicialesInsertar", con);

        cmd.Parameters.Add("@tipoDocumento", SqlDbType.Char).Value = ddlTipoDocumento.SelectedItem.Value;
        cmd.Parameters.Add("@numeroDocumento", SqlDbType.VarChar).Value = txtNumeroDocumento.Text.Trim();
        cmd.Parameters.Add("@condicionMagistrado", SqlDbType.Char).Value = ddlCondicionMagistrado.SelectedItem.Value;
        cmd.Parameters.Add("@obsManejoJurisprudencial", SqlDbType.VarChar).Value = txtManejoJurisprudencial.Text.Trim();

        cmd.CommandType = CommandType.StoredProcedure;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            LimpiarControles();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }

     }
0
Comment
Question by:enrique_aeo
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:puru1981
ID: 33605500
in this case you need to use executereader.

alternatively, make an output parameter and set the value of output parameter in SP and retrieve the same in your C# code.
0
 

Author Comment

by:enrique_aeo
ID: 33605503
can you give the code to:
 make an output parameter and set the value of output parameter in SP and retrieve the same in your C# code
0
 
LVL 9

Accepted Solution

by:
puru1981 earned 500 total points
ID: 33605674

//sql
create procedure [dbo].[EvaluacionSentenciasJudicialesInsertar]
(
  @tipoDocumento char(1) ,
      @numeroDocumento varchar(50) ,
...
@ID int output
)
AS
SET NOCOUNT ON
INSERT INTO [EvaluacionSJ].[dbo].[EvaluacionSentenciasJudiciales]
           ([tipoDocumento]
           ,[numeroDocumento]
   ...
     
     VALUES
           (
                                    @tipoDocumento
           ,@numeroDocumento
           ...
           )
           
set @ID= @@IDENTITY

//c#
 cmd.Parameters.Add("@ID", SqlDbType.Int).Direction=ParameterDirection.Output;

Open in new window

0
 

Author Comment

by:enrique_aeo
ID: 33606699
and how to retrieve the value to assign to a text box?
0
 
LVL 9

Assisted Solution

by:puru1981
puru1981 earned 500 total points
ID: 33607064
you can check the value and assign it to the textbox

textbox.text=cmd.Parameters["@ID"].Value.ToString();
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question