I'm having this error, when I'm going to write form data to the bank.
Error: System.NullReferenceException:
System.NullReferenceExceptionocorridoHResult=0x80004003Message=Referênciadeobjetonãodefinidaparaumainstânciadeumobjeto.Source=ProjetoAlphaStackTrace:emProjetoAlpha.frmProduto.btnGravar_Click(Objectsender,EventArgse)emC:\Users\willian\source\repos\ProjetoAlpha\ProjetoAlpha\frmProduto.cs:linha29emSystem.Windows.Forms.Control.OnClick(EventArgse)emSystem.Windows.Forms.Button.OnClick(EventArgse)emSystem.Windows.Forms.Button.OnMouseUp(MouseEventArgsmevent)emSystem.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtonsbutton,Int32clicks)emSystem.Windows.Forms.Control.WndProc(Message&m)emSystem.Windows.Forms.ButtonBase.WndProc(Message&m)emSystem.Windows.Forms.Button.WndProc(Message&m)emSystem.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)emSystem.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)emSystem.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)emSystem.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)emSystem.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtrdwComponentID,Int32reason,Int32pvLoopData)emSystem.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32reason,ApplicationContextcontext)emSystem.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32reason,ApplicationContextcontext)emSystem.Windows.Forms.Application.Run(FormmainForm)emProjetoAlpha.Program.Main()emC:\Users\willian\source\repos\ProjetoAlpha\ProjetoAlpha\Program.cs:linha19
Thisisthemethodtoinsert:
ThisismytableinPostGresql:
Thisisthecompleteclassofconnectionandmethods:
usingSystem;usingNpgsql;usingSystem.Data;namespaceProjetoAlpha{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 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();
}
}
}
}
Save 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 gave 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