The same variable is used by different methods, but the second does not take the information captured by the first

Asked

Viewed 32 times

1

Beginner question you are picking to learn.

Next: I have a variable "taxaDI".

I capture this information by the Capturrtaxadi method().

Then, this value must be inserted into a database by Inserirbanco().

What happens is that I am not able to bring the amount captured by the first method for the second insert into the bank.

When I try to insert in the bank it inserts "0".

I think I should use a property, but I don’t have it yet...

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CapturaFTP
{
    class CapturaFTP

    {   


        public static void CapturarTaxaDI()
        {       
                Campos item = new Campos();

                string data = DateTime.Now.AddDays(-2).ToString("yyyyMMdd");

                string arquivoFtp = data + "_TAXA_DI.TXT";


                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@Constantes.FTP + arquivoFtp);
                request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;


                WebProxy proxyConfigs = new WebProxy(Constantes.proxy, Constantes.portaProxy);
                proxyConfigs.UseDefaultCredentials = true;
                proxyConfigs.Credentials = new NetworkCredential(Constantes.usuario, Constantes.senha, Constantes.dominio);
                request.Proxy = proxyConfigs;


                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                string taxa = reader.ReadToEnd().Trim();

                 item.taxaDI = Convert.ToDecimal(taxa) / 10000;}


               public static void InsereBanco()

               {
                Campos item = new Campos();

                SqlConnection conexaoBanco = new SqlConnection(Constantes.Banco);
                conexaoBanco.Open();

                SqlCommand comandoBanco = new SqlCommand(Constantes.judsp001InsereValorDolarSelic, conexaoBanco);

                comandoBanco.CommandType = CommandType.StoredProcedure;
                comandoBanco.Parameters.AddWithValue("@vr_indice", item.taxaDI);
                comandoBanco.Parameters.AddWithValue("@co_indice_economico", Constantes.coIndiceTaxaDI);

                int rowsAffected = comandoBanco.ExecuteNonQuery();

                }


                class Campos
                {
                    public decimal taxaDI { get; set;}
                }


        }
    }
```


1 answer

2


Objects declared within a method are different from those created in other methods, even if they have the same name and type. Like item which is declared within the method insereBanco is not changed, the properties have the default value (0, in the case of decimal).

A quick way to correct this would be to have the object as a parameter of the method, doing InsereBanco(Campos item) and passing the created item when calling this method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.