1
I have this error, when I will record the form data in the database.
Error: System.Nullreferenceexception:
System.NullReferenceException ocorrido HResult=0x80004003 Message=Referência de objeto não definida para uma instância de um objeto. Source=ProjetoAlpha StackTrace: em ProjetoAlpha.frmProduto.btnGravar_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
This is the method to insert:
This is the full class of connection and methods:
using System;
using Npgsql;
using System.Data;
namespace ProjetoAlpha
{
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 DataTable GetTodosRegistros()
{
DataTable dt = new DataTable();
try
{
using (conn = new NpgsqlConnection(ConnString))
{
conn.Open();
string cmdSeleciona = "SELECT * FROM estoque order by id_produto";
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmdSeleciona, conn))
{
da.Fill(dt);
}
}
}
catch (NpgsqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
return dt;
}
public DataTable GetRegistroPorId(int id)
{
DataTable dt = new DataTable();
try
{
using (NpgsqlConnection conn = new NpgsqlConnection(ConnString))
{
conn.Open();
string cmdSeleciona = "SELECT * FROM ESTOQUE WHERE codigo =" + id;
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmdSeleciona, conn))
{
da.Fill(dt);
}
}
}
catch (NpgsqlException ex)
{
throw ex;
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
return dt;
}
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(nome,codigo,minimo,maximo,quantidade,dataEntrada) " +
"VALUES ('{0}','{1}','{2}','{3}','{4}','{5}')", 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();
}
}
}
}
Record button:
private void btnGravar_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), dtpData.Value);
}catch(Exception ex)
{
throw ex;
}
finally
{
MessageBox.Show("Cadastrado com sucesso!");
}
}
I removed the Try, catch and Finally and error in the Insert Data class:
System.ArgumentException ocorrido
HResult=0x80070057
Message=Keyword not supported: user idpostgres;password
Parameter name: keyword
Source=Npgsql
StackTrace:
em Npgsql.NpgsqlConnectionStringBuilder.GetProperty(String keyword)
em Npgsql.NpgsqlConnectionStringBuilder.set_Item(String keyword, Object value)
em System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
em Npgsql.NpgsqlConnectionStringBuilder..ctor(String connectionString)
em Npgsql.NpgsqlConnection.GetPoolAndSettings()
em Npgsql.NpgsqlConnection.set_ConnectionString(String value)
em Npgsql.NpgsqlConnection..ctor(String connectionString)
em ProjetoAlpha.DAL.InserirRegistros(String nome, Int32 codigo, Int32 minimo, Int32 maximo, Int32 qtd, DateTime data) em C:\Users\willian\source\repos\ProjetoAlpha\ProjetoAlpha\DAL.cs:linha 91
em ProjetoAlpha.frmProduto.btnGravar_Click(Object sender, EventArgs e) em C:\Users\willian\source\repos\ProjetoAlpha\ProjetoAlpha\frmProduto.cs:linha 26
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
take out that lot of Try catch there... and another...if it gives error and fall from catch, at the end you still display "Successfully registered" ?
– Rovann Linhalis
Actually it is falling just catch and shows nothing but the error, to be honest I’m following a tutorial on the net that the guy did it also http://www.macoratti.net/13/07/c_pgsql2.htm
– WSS
I’ve learned a lot from his tutorials, but in this one... Nun was cool not... rs
– Rovann Linhalis