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?– Jéf Bueno
Please put the stack trace complete in question. What type of column
nome
?Id
is not Identity Insert?– Jéf Bueno
@jbueno, the
ToString()
it was just an attempt, because I don’t know what else to do rs.. I’m going to put– Leonardo
It makes no sense to use
ToString
in astring
. The problem is elsewhere. If you try to pass a value hardcoded works?– Jéf Bueno
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.– Leonardo
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 .
– Marco Souza
@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.– Leonardo