0
I have a form with two fields:
- 1 Combobox I put the type of people (Physics or Legal);
- 1 Masktextbox with CPF;
At first I don’t know what’s better if I keep the value of Index of the combobox or its text. Because after that I will need to load this combobox again.
My bank is like this:
CREATE TABLE CLIENTES(
    id_cliente SERIAL PRIMARY KEY NOT NULL,
    tipo INT,
    tipos VARCHAR(10),
    CPF INT
);
I made it very simple to learn and left the tipos/tipo to test the two forms.
My connection class and the method to Insert:
class DAL
    {
        static string serverName = "localhost";
        static string port = "5432";
        static string userName = "postgres";
        static string password = "adm";
        static string databaseName = "dbestoque";
        NpgsqlConnection conn = null;
        string ConnString = null;
        public DAL()
        {
            ConnString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
                                       serverName, port, userName, password, databaseName);
        }
 public void InserirClientes(int cb, int cpf)
        {
            using (conn = new NpgsqlConnection(ConnString))
            {
                conn.Open();
                string cmdInserir = String.Format("INSERT INTO CLIENTES(tipo, cpf) VALUES ('{0}', '{1}')", cb, cpf);
                using (NpgsqlCommand cmd = new NpgsqlCommand(cmdInserir, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
Record button:
private void btnGravar_Click(object sender, EventArgs e)
    {
        DAL n = new DAL();
        try
        {
            n.InserirClientes(cbTipo.SelectedIndex, Convert.ToInt16(mskCPF.Text));
        }catch(Exception ex)
        {
            throw ex;
        }
        finally
        {
            MessageBox.Show("Efetuado");
        }
    }
Error:
System.FormatException ocorrido
  HResult=0x80131537
  Message=A cadeia de caracteres de entrada não estava em um formato correto.
  Source=ProjetoAlpha
  StackTrace:
   em ProjetoAlpha.frmCadastros.btnGravar_Click(Object sender, EventArgs e) em C:\Users\willian\source\repos\ProjetoAlpha\ProjetoAlpha\frmCadastros.cs:linha 28
   em System.Windows.Forms.Control.OnClick(EventArgs e)
   em System.Windows.Forms.Button.OnClick(EventArgs e)
   em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   em System.Windows.Forms.Control.WndProc(Message& m)
   em System.Windows.Forms.ButtonBase.WndProc(Message& m)
   em System.Windows.Forms.Button.WndProc(Message& m)
   em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   em 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)
   em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   em System.Windows.Forms.Application.Run(Form mainForm)
   em ProjetoAlpha.Program.Main() em C:\Users\willian\source\repos\ProjetoAlpha\ProjetoAlpha\Program.cs:linha 19


only use TEXT for very large fields, and which do not require a search, the normal is VARCHAR
– Rovann Linhalis
Thanks for the tip Rovann ! I already changed this in my comment.
– Joao Victor Pereira santos