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 name column does not accept null value, but in the Console.WriteLine itself the values are displayed, 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 most recent compatible connector.
Does anyone know what could be going wrong? SELECT * FROM robos
is working fine.
Follow 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 line 201 is: dbcmd.ExecuteNonQuery();