Error while trying to update table using C#

Asked

Viewed 642 times

7

I created a form to change a table, but my update is not working.

    cnxCli.sel =/*"set dateformat dmy \n"+ */
                "update Cliente" +
                 "set Nome = '" + txtNome.Text +"'," +
                 "Rg = '" + mskRg.Text + "'," +
                 "Cpf ='" + mskCpf.Text + "'," +
                 "Celular ='" + mskCelular + "'," +
                 "Telefone ='" + mskTelefone + "'," +
                 "DataNascimento = '" + mskNascimento.Text + "'," +
                 "Endereco ='" + txtEndereco.Text + "'," +
                 "Bairro='" + txtBairro.Text + "'," +
                 "Cidade='" + "'," +
                 "Cep ='" + mskCep.Text + "'," +
                 "Observacao='" + txtObs.Text + "'" +
                 "where IDCliente =" + IdCliSel;
    cnxCli.selCmd.CommandText = cnxCli.sel;
    bool deuErro = false;
    try
    {
        cnxCli.selCmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        deuErro = true;
        //throw;
    }
    if (!deuErro)
        MessageBox.Show("Cadastro do cliente " + IdCliSel + " atualizado com sucesso!");

The exception thrown: System.Data.SqlClient.SqlException - Incorrect syntax near 'Nome'.

For me the syntax seems correct, so much so that I copied the code directly from SQL Server 2014, where I had just run the command.

2 answers

7


There is a syntax error because a space is missing in "update Cliente" +, in this way brought it all together.

But this is an even smaller problem, the whole code has several problems.

Never ride a query in this way, it is insecure and disorganized. See in documentation the right way to do it.

There may still be memory leakage.

Even doing it that way could still be simplified, and simpler things make fewer mistakes and when they happen, they’re easier to find.

The way of exception capture is quite wrong. I teach in several responses.

All columns are varchar? It is unlikely. Will give other mistakes by correcting this.

There are other minor problems and something indicates that the rest of the code is bad too. So it accumulates problems and gets tired and thinking that the problem is in the programming language. You have to learn how to do it, it has to be structured, there’s no point following cake recipes.

  • I agree with everything you said. The code has been causing several problems. But this is still my first system. Besides, I’m using the spare minutes at work or after dinner to program. Until 2 months ago I could spend a lot of time programming, now no more. Thanks for the feedbacks.

3

One should give a space between the table Cliente and the command set:

cnxCli.sel =/*"set dateformat dmy \n"+ */
                "update Cliente" +
                 "set Nome = '"

Change to:

cnxCli.sel =/*"set dateformat dmy \n"+ */
                "update Cliente " + /*<-----Mudei aqui.*/
                 "set Nome = '"
  • Now gave string or binary data would be truncated. The statement has been terminated.

  • Which line is reporting error?

  • Usually this error is related to the size of a field’s content, see if you are sending out more data than your field might contain.

  • I discovered the bug. I put a breakpoint just before running the query, see what was inside my select update Cliente set Nome = 'AAAA',Rg = '11.111.111-1',Cpf ='111.111.111-11',Celular ='System.Windows.Forms.MaskedTextBox, Text: (11) 1 1111-1111',Telefone ='System.Windows.Forms.MaskedTextBox, Text: (11) 1111-1111',DataNascimento = '01/01/1900',Endereco ='AAAA',Bairro='AAAA',Cidade='',Cep ='11111-111',Uf = 'AA',Observacao='aaaa'where IDCliente =1 For some reason Masked textboxes are sending not only the value, but this strange value. 'System.Windows.Forms.MaskedTextBox

  • In the mskCelular and in the mskTelefone place the property Text. I also suggest you follow the tips of the bigown.

Browser other questions tagged

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