Good evening guys, with a little problem in a basic test program that I'm doing, could anyone help me?
Error: The input string was not in an incorrect format. (FormatException)
in System.Number.StringToNumber (String str, NumberStyles options, NumberBuffer & number, NumberFormatInfo info, Boolean parseDecimal) in System.Number.ParseInt32 (String s, NumberStyles style, NumberFormatInfo info) in System.String.System.IConvertible.ToInt32 (IFormatProvider provider) in System.Convert.ToInt32 (Object value) in MySql.Data.Types.MySqlInt32.MySql.Data.Types.IMySqlValue.WriteValue (MySqlPacket packet, Boolean binary, Object val, Int32 length) in MySql.Data.MySqlClient.MySqlParameter.Serialize (MySqlPacket packet, Boolean binary, MySqlConnectionStringBuilder settings) in MySql.Data.MySqlClient.Statement.SerializeParameter (MySqlParameterCollection parameters, MySqlPacket packet, String parmName, Int32 parameterIndex) in MySql.Data.MySqlClient.Statement.InternalBindParameters (String sql, MySqlParameterCollection parameters, MySqlPacket packet) in MySql.Data.MySqlClient.Statement.BindParameters () in MySql.Data.MySqlClient.PreparableStatement.Execute () in MySql.Data.MySqlClient.MySqlCommand.ExecuteReader (CommandBehavior behavior) in MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery () C: \ Users \ Filipe \ documents \ visual studio 2010 \ Projects \ MiniCrud_Aula_PC \ MiniCrud_Aula_PC \ DAO.cs: line 41 in MiniCrud_Aula_PC.DAO. in MiniCrud_Aula_PC.frm_Principal.Salvar_Cadastro () in C: \ Users \ Filipe \ documents \ visual studio 2010 \ Projects \ MiniCrud_Aula_PC \ MiniCrud_Aula_PC \ frm_Principal.cs: line 239 in MiniCrud_Aula_PC.frm_Principal.bt_Salvar_Click (Object sender, EventArgs e) in C: \ Users \ Filipe \ documents \ visual studio 2010 \ Projects \ MiniCrud_Aula_PC \ MiniCrud_Aula_PC \ frm_Principal.cs: line 62 in System.Windows.Forms.Control.OnClick (EventArgs and) in System.Windows.Forms.Button.OnClick (EventArgs and) in System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent) in System.Windows.Forms.Control.WmMouseUp (Message & m, MouseButtons button, Int32 clicks) in System.Windows.Forms.Control.WndProc (Message & m) in System.Windows.Forms.ButtonBase.WndProc (Message & m) in System.Windows.Forms.Button.WndProc (Message & m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m) in System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m) in System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG & msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32 reason, ApplicationContext context) in System.Windows.Forms.Application.Run (Form mainForm) in MiniCrud_Aula_PC.Program.Main () in C: \ Users \ Filipe \ documents \ visual studio 2010 \ Projects \ MiniCrud_Aula_PC \ MiniCrud_Aula_PC \ Program.cs: line 18 in System.AppDomain._nExecuteAssembly (RuntimeAssembly assembly, String [] args) in System.AppDomain.ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args) in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly () in System.Threading.ThreadHelper.ThreadStart_Context (Object state) in System.Threading.ExecutionContext.RunInternal (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state) in System.Threading.ThreadHelper.ThreadStart ()
private const string insertCarro = "insert into tbl_Carro (marca, modelo, ano, cor, estado, valor) values (@marca, @modelo, @ano, @cor, @estado, @valor);";
private const string countCodigo = "select COUNT(id) from tbl_Carro";
public int cadastrarCarro(Carro c)
{
int codigo = 0;
using (MySqlConnection conectar = new MySqlConnection(conexao))
{
using(MySqlCommand comando = new MySqlCommand(insertCarro, conectar))
{
try
{
conectar.Open();
comando.Parameters.AddWithValue("@marca", MySqlDbType.VarChar).Value = c.Marca;
comando.Parameters.AddWithValue("@modelo", MySqlDbType.VarChar).Value = c.Modelo;
comando.Parameters.AddWithValue("@ano", MySqlDbType.Int32).Value = c.Ano;
comando.Parameters.AddWithValue("@cor", MySqlDbType.VarChar).Value = c.Cor;
comando.Parameters.AddWithValue("@estado", MySqlDbType.VarChar).Value = c.Estado;
comando.Parameters.AddWithValue("@valor", MySqlDbType.Decimal).Value = c.Valor;
comando.ExecuteNonQuery();
comando.CommandText = countCodigo;
codigo = Convert.ToInt32(comando.ExecuteScalar());
}
catch (MySqlException)
{
MessageBox.Show("Ocorreu um erro ao executar a inserção do carro no banco de dados!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conectar.Close();
}
}
}
return codigo;
}
class Carro
{
public int Id { get; set; }
public string Marca { get;set; }
public string Modelo { get; set; }
public int Ano { get; set; }
public string Cor { get; set; }
public string Estado { get; set; }
public double Valor { get; set; }
public Carro()
{
}
public Carro(int _id)
{
Id = _id;
}
public Carro(int _id, string _marca, string _modelo, int _ano, string _cor, string _estado, double _valor)
{
Id = _id;
Marca = _marca;
Modelo = _modelo;
Ano = _ano;
Cor = _cor;
Estado = _estado;
Valor = _valor;
}
public Carro(string _marca, string _modelo, int _ano, string _cor, string _estado, double _valor)
{
Marca = _marca;
Modelo = _modelo;
Ano = _ano;
Cor = _cor;
Estado = _estado;
Valor = _valor;
}
}