Changing customer information via ID

Asked

Viewed 65 times

1

I cannot change customer information through ID tela do sistema

Code that makes the changes in the Database

         public void Atualizar(Contato contato)//metodo para atualizar dados: update   
    {
        OleDbParameter[] parametros = {
            new OleDbParameter("CODIGO", contato.Codigo),
            new OleDbParameter("NOME", contato.Nome),
            new OleDbParameter("FONE", contato.Fone),
            new OleDbParameter("EMAIL", contato.Email)
        };
        new ConexaoDAL().ConexaoAuto(parametros, "update Contato set NOME=@NOME,EMAIL=@EMAIL,FONE=@FONE  where CODIGO=@CODIGO ", "Erro ao atualizar ");
    }

Event Click page

      public void SalvaDados()
    {
        try
        {
            if (SalvarCodigo == null)
            {
                Crud d = new Crud();
                d.Gravar(new Contato(SalvarNome.Text, SalvarFone.Text, SalvarEmail.Text)); // gravando pessoa 
                Message.Text = "Cadastro salvo com sucesso";
            }
            else
            {
                int codigo = Convert.ToInt32(SalvarCodigo);

                Crud d = new Crud();
                d.Atualizar(new Contato(codigo, SalvarNome.Text, SalvarFone.Text, SalvarEmail.Text));
                Message.Text = "Cadastro alterado com sucesso";
            }
        }
        catch (Exception ex)
        {

            throw new Exception(Message.Text = ex.Message);
        }
    }

Exceção gerada NOTE: when it gives the error it talks to convert, but I have tried to convert what is inside the textbox and even then it doesn’t work. aspx. Código ASP.Net

  • What’s the mistake? Where does it occur? In what situation? As I don’t know the problem, I don’t even know if what was posted is enough for us to help. The impression is that it is not. Take this try-catch that it is not doing anything useful there, otherwise it is caused problem.

  • the error occurs when I enter in the code field the code to be changed it says it has to convert, but even if converting appears the same error.

  • The first thing to do is take the try-catch as I told you to see the actual error, you are hiding it without solving it. Do not capture exception if you cannot solve the problems. Do not capture Exception never, unless you know why you’re doing it. http://answall.com/search?q=user%3A101+%5Bexception%5D. Where are you SalvarCodigo? I find this whole code very strange, there seems to be a general problem of design.

  • I understand, already taking off Try-catch, Salvarcod is coming from the page, where I insert the customer’s Cod.

  • But show him so we can help.

1 answer

1

The whole code seems strange to me and there seems to be problems of design in it. It may be a matter of style and taste, but I would do all the method SalvarDados() in just 2 lines. I won’t try to teach this way because it can confuse those who don’t have a good general sense. I find it strange to create an instance of Crud only for this, but I know that there are those who like it. What is certainly a mistake is to have this try-catch. He is not solving any problem. In fact he is making it difficult to identify the error. Even if he had to capture an exception, shouldn’t be Exception.

You should not convert data you have no control over. You should use TryParse() or something similar, if it fails it has how to recover in the normal flow of the application. This can be seen in that question and in this too. Behold Differences between Parse vs Tryparse and What is the main difference between int. Parse() and Convert.Toint32()?. If it fails you must take some action but cannot save the data. The problem may be there.

But it’s no use trying to convert something that can’t be converted. What you might want to do is:

if (int.TryParse(SalvarCodigo.Text, var out codigo)) { //<=======note o .Text aqui
    var d = new Crud();
    d.Atualizar(new Contato(codigo, SalvarNome.Text, SalvarFone.Text, SalvarEmail.Text));
    Message.Text = "Cadastro alterado com sucesso";
} else {
    //aqui trata se o código é inválido
}

I put in the Github for future reference.

I have doubts and no other problems.

  • Thank you so much for the tips and helps worked here! Obg

  • @Andréfelipejardimfirmo take a look at [tour] to understand how the site works.

  • Let me read it calmly, I apologize for what happened

  • @Andréfelipejardimfirmo Has the answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

Browser other questions tagged

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