Changing customer information via ID

1

I can not change the customer information through the

Code that makes changes to 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 ");
    }

Click event of the 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);
        }
    }

NOTE:whengivingtheerroritspeakstoconvert,butalreadytriedtoconvertwhatisinsidethetextBoxandstilldoesnotwork..aspx

    
asked by anonymous 15.11.2016 / 18:09

1 answer

1

The whole code seems strange to me and there seems to be design issues in it. It may be a matter of style and taste, but I would do the entire SalvarDados() method on only 2 lines. I will not try to teach this way because it may confuse those who do not have a good general notion. I find it odd to create an instance of Crud just for this, but I know there are those who like that. What is certainly a mistake is having this try-catch . He is not solving any problem. It is actually making it difficult to identify the error. Even if you had to catch an exception, should not be Exception .

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

But it's no good trying to convert something that can not be converted. What you may need to do is:

int codigo;
if (int.TryParse(SalvarCodigo.Text, 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 have questions and do not have any other problems.

    
15.11.2016 / 19:08