I can’t find the reason for Formatexcpetion

Asked

Viewed 416 times

2

Folks good night I have a little problem in a basic test program I’m doing someone could 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() Mysql.Data.Mysqlclient.PreparableStatement.Execute() in Mysql.Data.Mysqlclient.MySqlCommand.Executereader(Commandbehavior behavior) in Mysql.Data.Mysqlclient.MySqlCommand.Executenonquery() in Minicrud_aula_pc.DAO.register Return (Car c) in C: Users Filipe Documents visual studio 2010 Projects Minicrud_aula_pc Minicrud_aula_pc DAO.Cs:line 41 in Minicrud_aula_pc.frm_Principal.Salvar_cadastro() na 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) in 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) in System.Windows.Forms.UnsafeNativeMethods.Dispatchmessagew(MSG& msg) em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) in System.Windows.Forms.Application.Threadcontext.Runmessageloopinner(Int32 Reason, Applicationcontext context) in 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;
        }
    }
  • What is the line on which the exception happens?

  • @On the line I execute the command for the first time command.Executenonquery();

  • Post the complete exception (message + stack trace), makes it easier to know the problem. Another piece of information that may also be useful is the definition of the class Carro

  • @carlosfigueira Ready put Stacktrace + the Class

1 answer

3


  • Thanks, I stopped informing the guy and started to inform the value of the parameter with the Addwithvalue and it worked. Thank you and good night :)

Browser other questions tagged

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