Solved

retrieve the identity value in my data layer

Posted on 2010-09-04
5
274 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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