Solved

retrieve the identity value in my data layer

Posted on 2010-09-04
5
275 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

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!
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

687 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