Insert MYSQL in Web form C # - I can not insert anything

-1

I'm trying to make an insert in my bd from an app from the college I'm developing. However, I'm having difficulties.

Getting the texbox values

 protected void button1_cad_cliente(object sender, EventArgs e)
    {
        Clientes cl = new Clientes();
        cl.Nome = nome_clientes.Text;
        cl.Cpf = Convert.ToInt32(cpf_clientes.Text);
        cl.Rg = Convert.ToInt32(rg.Text);
        cl.Endreco = endereco.Text;
        cl.Email = email_clientes.Text;

        ClienteDAL.cadastra(cl);

    }

DAL performing the insert or at least that is the intention

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Geax.Model;
using MySql.Data;
using MySql.Data.MySqlClient;

 namespace Geax.DAL
 {
public class ClienteDAL
{
    public static void cadastra(Clientes obj)
    {
        Conexao conn1 = new Conexao();
        conn1.AbrirConexao();
        String InsertCliente = ("INSERT INTO tab_cliente (nome,cpf,rg,endereco,telefone,email) VALUES('Cl.Nomes','Cl.Cpf','Cl.Rg','Cl.Endereco','Cl.Telefone','Cl.Email')");

        MySqlCommand cmd = new MySqlCommand(InsertCliente);

        cmd.ExecuteNonQuery();
    }
 }
}

Class that connects to the bank:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace Geax.DAL
{
public class Conexao
{
private static MySqlConnection objConexao = null;
   private String conn_db = "server=localhost; Database=xpto; User=root;    Password='';";

    public void AbrirConexao()
   {
        objConexao = new MySqlConnection();
        objConexao.ConnectionString = conn_db;
        objConexao.Open();
   } 
 }   
}
    protected void button1_cad_cliente(object sender, EventArgs e)
    {
        Clientes cl = new Clientes();
        cl.Nome = nome_clientes.Text;
        cl.Cpf = Convert.ToInt32(cpf_clientes.Text);
        cl.Rg = Convert.ToInt32(rg.Text);
        cl.Endreco = endereco.Text;
        cl.Email = email_clientes.Text;

        ClienteDAL.cadastra(cl);

    }

I have already tried to associate cmd.Parameters.AddWithValue("@nome", stringComNome); with the data I got above.

Pus: cmd.Parameters.AddWithValue("@nome", nome_clientes.txt); but does not recognize

    
asked by anonymous 22.09.2014 / 05:53

1 answer

1

Never trigger an SQL like this:

String InsertCliente = ("INSERT INTO tab_cliente (nome,cpf,rg,endereco,telefone,email) VALUES('Cl.Nomes','Cl.Cpf','Cl.Rg','Cl.Endereco','Cl.Telefone','Cl.Email')");

Parametrize always values, even if it's just a test:

String InsertCliente = ("INSERT INTO tab_cliente (nome,cpf,rg,endereco,telefone,email) VALUES(@nome, @cpf, @rg, @endereco, @telefone, @email)");

MySqlCommand cmd = new MySqlCommand(InsertCliente);
cmd.Parameters.AddWithValue("@nome", stringComNome);
cmd.Parameters.AddWithValue("@cpf", stringComCpf);
cmd.Parameters.AddWithValue("@rg", stringComRg);
cmd.Parameters.AddWithValue("@endereco", stringComEndereco);
cmd.Parameters.AddWithValue("@telefone", stringComTelefone);
cmd.Parameters.AddWithValue("@email", stringComEmail);

Another thing: The execution block must be within try so you can intercept exceptions if they occur:

try 
{
    Conexao conn1 = new Conexao();
    conn1.AbrirConexao();
    String InsertCliente = ("INSERT INTO tab_cliente (nome,cpf,rg,endereco,telefone,email) VALUES(@nome, @cpf, @rg, @endereco, @telefone, @email)");

    MySqlCommand cmd = new MySqlCommand(InsertCliente);
    cmd.Parameters.AddWithValue("@nome", stringComNome);
    cmd.Parameters.AddWithValue("@cpf", stringComCpf);
    cmd.Parameters.AddWithValue("@rg", stringComRg);
    cmd.Parameters.AddWithValue("@endereco", stringComEndereco);
    cmd.Parameters.AddWithValue("@telefone", stringComTelefone);
    cmd.Parameters.AddWithValue("@email", stringComEmail);

    cmd.ExecuteNonQuery();
}
catch (Exception e) {
    throw e;
}

Another thing is that the way it is, you are not relating the connection to the command, so it will never work. Put the command inside the connection block of a connection:

using (var conn = new MySqlConnection("server=localhost; Database=xpto; User=root;    Password='';")) 
{
    try 
    {
        conn.Open();
        String InsertCliente = ("INSERT INTO tab_cliente (nome,cpf,rg,endereco,telefone,email) VALUES(@nome, @cpf, @rg, @endereco, @telefone, @email)");

        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = InsertCliente;
        cmd.Connection = conn;
        cmd.Parameters.AddWithValue("@nome", stringComNome);
        cmd.Parameters.AddWithValue("@cpf", stringComCpf);
        cmd.Parameters.AddWithValue("@rg", stringComRg);
        cmd.Parameters.AddWithValue("@endereco", stringComEndereco);
        cmd.Parameters.AddWithValue("@telefone", stringComTelefone);
        cmd.Parameters.AddWithValue("@email", stringComEmail);

        cmd.ExecuteNonQuery();
    }
    catch (Exception e) {
        throw e;
    }
}

The method at the end looks like this:

public static void Cadastra(Clientes obj)
{
    using (var conn = new MySqlConnection("server=localhost; Database=xpto; User=root;    Password='';")) 
    {
        try 
        {
            conn.Open();
            String InsertCliente = ("INSERT INTO tab_cliente (nome,cpf,rg,endereco,telefone,email) VALUES(@nome, @cpf, @rg, @endereco, @telefone, @email)");

            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = InsertCliente;
            cmd.Connection = conn;
            cmd.Parameters.AddWithValue("@nome", obj.Nome);
            cmd.Parameters.AddWithValue("@cpf", obj.Cpf);
            cmd.Parameters.AddWithValue("@rg", obj.Rg);
            cmd.Parameters.AddWithValue("@endereco", obj.Endereco);
            cmd.Parameters.AddWithValue("@telefone", obj.Telefone);
            cmd.Parameters.AddWithValue("@email", obj.Email);

            cmd.ExecuteNonQuery();
        }
        catch (Exception e) {
            throw e;
        }
    }
}
    
22.09.2014 / 06:17