0
Hello,
I have a form with the Datetimepicker - Short (only the date).
And in the database I put DataEntrada DATE
My problem is, I created an insertion method but I don’t know how to pass the Datetimepicker per parameter.
Method of entering records:
public void InserirRegistros(string nome, int codigo, int minimo, int maximo, int qtd, DateTime data)
{
try
{
using (NpgsqlConnection conn = new NpgsqlConnection(ConnString))
{
conn.Open();
string cmdInserir = String.Format("INSERT INTO ESTOQUE(codigo,codigo,minimo,maximo,quantidade,dataEntrada) " +
"VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", nome, codigo, minimo, maximo, qtd, data);
using (NpgsqlCommand cmd = new NpgsqlCommand(cmdInserir, conn))
{
cmd.ExecuteNonQuery();
}
}
}catch(NpgsqlException ex)
{
throw ex;
}catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
Event Click the button:
private void button1_Click(object sender, EventArgs e)
{
DAL acesso = new DAL();
try
{
acesso.InserirRegistros(txtNomeProduto.Text, Convert.ToInt32(txtCodigo.Text), Convert.ToInt32(txtMinimo.Text), Convert.ToInt32(txtMaximo.Text), Convert.ToInt32(txtEntrada.Text), Convert.ToDateTime(dtpData.DataBindings));
}catch(Exception ex)
{
throw ex;
}
finally
{
MessageBox.Show("Cadastrado com sucesso!");
}
}
Error:
System.InvalidCastException ocorrido HResult=0x80004002 Message=Não é possível converter um objeto do tipo 'System.Windows.Forms.ControlBindingsCollection' no tipo 'System.IConvertible'. Source=ProjetoAlpha StackTrace: em ProjetoAlpha.frmProduto.button1_Click(Object sender, EventArgs e) em C:\Users\willian\source\repos\ProjetoAlpha\ProjetoAlpha\frmProduto.cs:linha 29 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
So far I could not, because the field in the table is DATE (2010-01-01) and I am going in the wrong format, how do I fix it? Do you need to change the type of the parameter to string?
Does not work, it picks the full date example: 13/10/2017 07:39:04
– WSS
which error it is returning ?!
– JcSaint
This error: https://answall.com/questions/245784/nullreferenceexception-ao-tenta-gravar-no-banco-de-data
– WSS
But this error has nothing to do with the date, I did not understand the downvote, and the error is of null reference and has nothing to do with the question.
– JcSaint
at first I thought it was because of the format, then I changed and I came to this conclusion too
– WSS