Skip to content

The parametar doesn't change

Nel

Asked Jul 29, 2012, 11:56 PM EDT4 answers
Share
LinkedInX
Hi,

I have this code with parameter saldo1:

public bool napKI(DateTime data)
        {
bool result = false;
            int posl_kas_izv = 0;
            decimal iznosd = 0;
            decimal saldo1 = 0;
            int broj=0;
            SqlConnection connection = null;

 try
  {
 
  connection = GetConnection();
  connection.Open();
 
  SqlCommand command2 = new SqlCommand("izbrojZapisi", connection);
  command2.CommandType = CommandType.StoredProcedure;
  command2.Parameters.AddWithValue("@data", data);
  int brojac = (int)command2.ExecuteScalar();
 
 
  if (brojac == 0)
  {
  posl_kas_izv = 0;
  SqlCommand command3 = new SqlCommand("prvosaldo", connection);
  command3.CommandType = CommandType.StoredProcedure;
  saldo1 = Convert.ToDecimal(command3.ExecuteScalar());
 
  }
 
  else
  {
  SqlCommand command1 = new SqlCommand("najdiposlKIzvod", connection);
  command1.CommandType = CommandType.StoredProcedure;
  command1.Parameters.AddWithValue("@data", data);
  posl_kas_izv = (int)command1.ExecuteScalar();
 
 
  SqlCommand command5 = new SqlCommand("presmetajSaldo", connection);
  command5.CommandType = CommandType.StoredProcedure;
  command5.Parameters.AddWithValue("@idki", posl_kas_izv);
  command5.Parameters.AddWithValue("@data", data);
  SqlParameter p = new SqlParameter();
  p.ParameterName = "@saldo";
  p.Direction = ParameterDirection.InputOutput;
  p.SqlDbType = SqlDbType.Decimal;
 
  command5.Parameters.AddWithValue("@saldo", saldo1);
  saldo1 = Convert.ToDecimal(command5.ExecuteScalar());
  }
....
....

when brojac=0, it inserts the right value in the datatable, but when brojac is not 0, saldo1 is 0, insetad of taking into account the last inserted value in the datatable for performing the calculation.Does anybody have an idea where do I make a mistake?
Thanks in advance

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Vinoth Raj
    0
    Could you provide profiler call when its calling command5? and also if you could disclose presmetajsaldo procedure's code then it will be easy for us to diagnose and the reply you back.
    0
  • 0
    Hi Vinoth,
    Here is the sp code:

    ALTER procedure [dbo].[presmetajSaldo]
    (
    @idki int,
    @data DateTime,
    @saldo numeric(12,3) output
    )
    as
    Declare @priem numeric(12,3) 
    Declare @isplata numeric(12,3)
    declare @saldo1 numeric(12,3)
     
    select @priem = sum(iznosden) from blagajna_jspturs
    where data=@data and vp>=0 and vp<=10
    select @isplata = sum(iznosden)
    from blagajna_jspturs
    where vp>10 and data=@data
    set @saldo1 = isnull(@saldo,0) + (isnull(@priem,0)) - (isnull(@isplata,0))
    update Kasovizvestaj
    set 
     idki=@idki,
     saldo=@saldo1
     where data=@data

    select saldo from Kasovizvestaj
    return

    And I don't understand what "profiler call" is? Would you please excuse my ignorance and tell me what does it mean?

    Thanks a lot for everything

    P.S. If I don't put the bolded row, it calculates correctly, but when I put it, it throughs only the last entered value for saldo in the data table .
    0
  • Vinoth Raj
    0
    If you need last inserted value then you have to use select @Saldo = saldo from kasovizvestaj where data=@data in your stored proc. Because you were declared @saldo as output but you werent assigned any value to it. Please let us know if you are looking for something..
    0
  • 0
    No, I don't want the last value, if I don't put the bolded line it takes the last value and performs the calculation. But when I type that row, it takes only the last row value, doesn't perform the calculations
    0