Salut,

J'ai une procedure stocké dans ma base de donnée qui me rend un entier dont voici le code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
create procedure GetNextPkValue @tableID nvarchar(50),@NewPKval int = -1
AS Set nocount on

begin transaction

-- get the current value of the id and place it in a temporary var
declare @CurrPKval int
select @CurrPKval = pkValue
from PKtable 
where tableName = @tableID

-- increment the current val
update PKtable
set pkValue = pkValue + 1
where tableName = @tableID

if @@error <> 0 goto ErrorHandler

-- get a copy of the value just updated

select @NewPKval = pkValue
from PKtable
where tableName = @tableID

-- test if update worked and return new value or error (-1

commit transaction
begin
	select pkValue from PKtable where tableName = @tableID	
end
return @CurrPKval

ErrorHandler:
rollback transaction
return -1
quand je l'execute dans SQL Server Management ça marche trés bien j'obtient la valeur de retour

j'aimerai savoir comment la récuperer a partir de mon code....

j'ai essayé ceci:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static int GetNextKey()
{
using (SqlConnection cnn = new SqlConnection(Common.GetConnectionString("CongreS")))
        {

            using (SqlCommand cmd = new SqlCommand())
            {
                cnn.Open();
                cmd.CommandText = "GetNextPkValue";
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter param;

                param = cmd.Parameters.Add("@CurrPKval", SqlDbType.Int);
                param.Direction = ParameterDirection.ReturnValue;              
                
                cmd.ExecuteNonQuery();
                int current = Convert.ToInt32(param.Value);

                return current;

            }
        }
}
mais j'obtient l'exception suivante:

ExecuteNonQuery Connection property has not been initialized

et merci d'avance.....................