Mysql Connector sending null value

Asked

Viewed 111 times

0

I have the following code:

MySqlConnection mysql = new MySqlConnection(CONEXAO);
mysql.Open();
try
{
    MySqlCommand dbcmd = mysql.CreateCommand();
    dbcmd.CommandText = "INSERT INTO robos (id,nome,cabelo,olhos,bracos,boca,cabeca,pernas,corpo) VALUES (@id,@nome,@cabelo,@olhos,@bracos,@boca,@cabeca,@pernas,@corpo); ";
    this.ativo = new Robo();
    this.ativo.Nome = this.nome.Text;
    this.ativo.Cabelo = new Cor(this.cabelo.SelectedItem.ToString(), this.defineCor(this.cabelo.SelectedItem.ToString()));
    this.ativo.Cabeca = new Cor(this.cabeca.SelectedItem.ToString(), this.defineCor(this.cabeca.SelectedItem.ToString()));
    this.ativo.Olho = new Cor(this.olhos.SelectedItem.ToString(), this.defineCor(this.olhos.SelectedItem.ToString()));
    this.ativo.Boca = new Cor(this.boca.SelectedItem.ToString(), this.defineCor(this.boca.SelectedItem.ToString()));
    this.ativo.Braco = new Cor(this.bracos.SelectedItem.ToString(), this.defineCor(this.bracos.SelectedItem.ToString()));
    this.ativo.Corpo = new Cor(this.corpo.SelectedItem.ToString(), this.defineCor(this.corpo.SelectedItem.ToString()));
    this.ativo.Perna = new Cor(this.pernas.SelectedItem.ToString(), this.defineCor(this.pernas.SelectedItem.ToString()));

    Console.WriteLine(
        "Id: " + ativo.Id + "\n" +
        "nome: " + ativo.Nome + "\n" +
        "Cabelo: " + ativo.Cabelo.Nome.ToString() + "\n" +
        "Cabeca: " + ativo.Cabeca.Nome.ToString() + "\n" +
        "Olho: " + ativo.Olho.Nome.ToString() + "\n" +
        "Boca: " + ativo.Boca.Nome.ToString() + "\n" +
        "Corpo: " + ativo.Corpo.Nome.ToString() + "\n" +
        "Perna: " + ativo.Perna.Nome.ToString() + "\n"
        );

    dbcmd.Parameters.AddWithValue("@id", ativo.Id);
    dbcmd.Parameters.AddWithValue("@nome", ativo.Nome.ToString());
    dbcmd.Parameters.AddWithValue("@cabelo", ativo.Cabelo.Nome.ToString());
    dbcmd.Parameters.AddWithValue("@olhos", ativo.Olho.Nome.ToString());
    dbcmd.Parameters.AddWithValue("@bracos", ativo.Braco.Nome.ToString());
    dbcmd.Parameters.AddWithValue("@boca", ativo.Boca.Nome.ToString());
    dbcmd.Parameters.AddWithValue("@cabeca", ativo.Cabeca.Nome.ToString());
    dbcmd.Parameters.AddWithValue("@pernas", ativo.Perna.Nome.ToString());
    dbcmd.Parameters.AddWithValue("@corpo", ativo.Corpo.Nome.ToString());
    dbcmd.ExecuteNonQuery();

    dbcmd.Dispose();
    dbcmd = null;
    mysql.Close();
    mysql = null;
    this.info.Text = "SALVOU";
}
catch (Exception ex)
{
    this.info.Text = ex.Message;
}

Every time I try to run it the error returned from mysql is that the column name does not accept null value, but in the Console itself.Writeline displays the values, as an example:

nome: stack
Cabelo: Preto
Cabeca: Branco
Olho: Rosa
Boca: Rosa
Corpo: Azul
Perna: Amarelo

I am using Mysql Connector 5.0 because the database is Mysql 3, hence this is the latest compatible nector.

Does anyone know what could be going wrong? the SELECT * FROM robos is working well.

Follow the Stack Trace:

----------------STACK----------


   at MySql.Data.MySqlClient.MySqlStream.OpenPacket()
   at MySql.Data.MySqlClient.NativeDriver.ReadResult(UInt64& affectedRows, Int64& lastInsertId)
   at MySql.Data.MySqlClient.MySqlDataReader.GetResultSet()
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at Fabrica.Form1.save() in C:\Documents and Settings\Leonardo\Meus documentos\Visual Studio 2008\Projects\FeiraDeCarro\FeiraDeCarro\Form1.cs:line 201



---------------- FIM STACK----------

Where the line 201 is : dbcmd.ExecuteNonQuery();

  • A question: why use .ToString() string?

  • 1

    Please put the stack trace complete in question. What type of column nome? Id is not Identity Insert?

  • @jbueno, the ToString() it was just an attempt, because I don’t know what else to do rs.. I’m going to put

  • 1

    It makes no sense to use ToString in a string. The problem is elsewhere. If you try to pass a value hardcoded works?

  • It doesn’t really make sense, but in the times of C there were some bugs like this, then I try everything. I tried with fixed values, also it does not work.

  • Ideally you would check which fields in the table robots do not accept NULL values, and check in the application what you are going through in these fields, see if this specifying all fields in the table also... as was specified also, do not use the (id, on Insert if your field and auto_increment .

  • @Gokussjgod all fields are NOT NULL, nor passing fixed parameters to the MySqlCommand is working, all table columns are in the query, and even removing the ID worked. The only way I tested now and worked is the 'pig' way to concatenate string and create the query.

Show 2 more comments

1 answer

0

Your id is primary key and auto increment? if it is auto increment, it cannot be placed in the insert.

Edit: I did the following: Banco robo

with the following code:

static void Main(string[] args)
{
    var CONEXAO = "Server=localhost;Database=robo;Uid=root;Pwd="; //Aqui você substitui pelos seus dados

    MySqlConnection mysql = new MySqlConnection(CONEXAO);
    mysql.Open();
    try
    {
        MySqlCommand dbcmd = mysql.CreateCommand();
        dbcmd.CommandText = "INSERT INTO robos (id,nome,cabelo,olhos,bracos,boca,cabeca,pernas,corpo) VALUES (@id,@nome,@cabelo,@olhos,@bracos,@boca,@cabeca,@pernas,@corpo); ";

        dbcmd.Parameters.AddWithValue("@id", 1);
        dbcmd.Parameters.AddWithValue("@nome", "Gabriel");
        dbcmd.Parameters.AddWithValue("@cabelo", "Preto");
        dbcmd.Parameters.AddWithValue("@olhos", "Azul");
        dbcmd.Parameters.AddWithValue("@bracos", "2 braços");
        dbcmd.Parameters.AddWithValue("@boca", "1 boca");
        dbcmd.Parameters.AddWithValue("@cabeca", "1 cabeça");
        dbcmd.Parameters.AddWithValue("@pernas", "2 pernas");
        dbcmd.Parameters.AddWithValue("@corpo", "body");
        dbcmd.ExecuteNonQuery();

        dbcmd.Dispose();
        dbcmd = null;
        mysql.Close();
        mysql = null;

    }
    catch (Exception ex)
    {

    }
}

And it worked.

Obs.:

  • Mysql version: 5.7.14
  • Latest version of Mysqlconnector.
  • Yes, it’s like auto_increment

  • I’m not absolutely sure if this is the problem, but I’ve had problems with C# when putting the ID to save, in an auto_increment key.

  • I’ve tried taking out the ID too and did not roll, the problem is from the name.

  • 1

    Nice you want to help, but you used the reply box to comment. You can post comments when you have a little more reputation points. Please use the answer field only to post a solution to the problem the question deals with.

Browser other questions tagged

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