Error creating parameterized SQL

Asked

Viewed 25 times

-1

I need help with an error message.

Invalid column name@.

This message appears as an error at the end of the program run

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cadastro
{
    public partial class FrmMenu : Form
    {
        public FrmMenu()
        {
            InitializeComponent();
            txtPesquisa.Enabled = false;
            txtNome.Enabled = false;
            txtTelefone.Enabled = false;
            txtCelular.Enabled = false;
            txtEmail.Enabled = false;
            txtEndereco.Enabled = false;
            txtNumero.Enabled = false;
            txtCidade.Enabled = false;
            txtSexo.Enabled = false;
        }

        SqlConnection sqlCon = null;
        private string strCon = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Genesis;Data Source=DESKTOP-U3IG6CV\SQLEXPRESS";
        private string strSql = string.Empty;

        private void addFuncionarioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Adicionar add = new Adicionar();
            add.ShowDialog();
        }

        private void sAIRToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAdicionar_Click(object sender, EventArgs e)
        {
            txtPesquisa.Enabled = false;
            txtNome.Enabled = true;
            txtTelefone.Enabled = true;
            txtCelular.Enabled = true;
            txtEmail.Enabled = true;
            txtEndereco.Enabled = true;
            txtNumero.Enabled = true;
            txtCidade.Enabled = true;
            txtSexo.Enabled = true;
        }

        private void btnSalvar_Click(object sender, EventArgs e)
        {
            strSql = "insert into ClientesAuE (nome, telefone, celular,email,endereco,numero,cidade,sexo) values (nome@, telefone@, celular@,email@,endereco@,numero@,cidade@,sexo@)";
            //Criar a conexão com o banco de dados
            sqlCon = new SqlConnection(strCon);99
            //Criar o comando que executa a instrução sql
            SqlCommand comando = new SqlCommand(strSql, sqlCon);

            comando.Parameters.Add("@nome", SqlDbType.VarChar).Value = txtNome.Text;
            comando.Parameters.Add("@telefone", SqlDbType.VarChar).Value = txtTelefone.Text;
            comando.Parameters.Add("@celular", SqlDbType.VarChar).Value = txtCelular.Text;
            comando.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
            comando.Parameters.Add("@endereco", SqlDbType.VarChar).Value = txtEndereco.Text;
            comando.Parameters.Add("@numero", SqlDbType.VarChar).Value = txtNumero.Text;
            comando.Parameters.Add("@cidade", SqlDbType.VarChar).Value = txtCidade.Text;
            comando.Parameters.Add("@sexo", SqlDbType.VarChar).Value = txtSexo.Text;

            try
            {
                sqlCon.Open();
                comando.ExecuteNonQuery();

                MessageBox.Show("CADASTRO REALIZADO COM SUCESSO.");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sqlCon.Close();
            }

            txtPesquisa.Enabled = true;
            txtNome.Clear();
            txtTelefone.Clear();
            txtCelular.Clear();
            txtEmail.Clear();
            txtEndereco.Clear();
            txtNumero.Clear();
            txtCidade.Clear();
            txtSexo.Clear();
        }
    }
}
  • 1

    In Insert should not be VALUES (@nome, @telefone, @etc...)?

1 answer

2

Dude I believe the mistake is on the Lina

strSql = "insert into ClientesAuE (nome, telefone, celular,email,endereco,numero,cidade,sexo) values (nome@, telefone@, celular@,email@,endereco@,numero@,cidade@,sexo@)";

I believe you should put the values in the following way @name, @phone... with @beforethe name

Browser other questions tagged

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