I have a form with two fields:
- 1 ComboBox that I put the type of people (Physique or Juridica);
- 1 MaskTextBox with CPF;
AtfirstIdonotknowwhat'sbestifyousavethevalueoftheIndex
ofthecomboboxoritstext.BecausethenwhenIgosearchingIwillneedtoloadthiscomboboxagain.
Mybanklookslikethis:
CREATETABLECLIENTES(id_clienteSERIALPRIMARYKEYNOTNULL,tipoINT,tiposVARCHAR(10),CPFINT);
ImadeitverysimpletolearnandIlefttipos/tipo
totestbothways.
MyconnectionclassandmethodtoInsert:
classDAL{staticstringserverName="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