Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. System.NullReferenceException ocorrido
  2. HResult=0x80004003
  3. Message=Referência de objeto não definida para uma instância de um objeto.
  4. Source=ProjetoAlpha
  5. StackTrace:
  6. em ProjetoAlpha.frmProduto.btnGravar_Click(Object sender, EventArgs e) em C:UserswilliansourcereposProjetoAlphaProjetoAlphafrmProduto.cs:linha 29
  7. em System.Windows.Forms.Control.OnClick(EventArgs e)
  8. em System.Windows.Forms.Button.OnClick(EventArgs e)
  9. em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  10. em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  11. em System.Windows.Forms.Control.WndProc(Message& m)
  12. em System.Windows.Forms.ButtonBase.WndProc(Message& m)
  13. em System.Windows.Forms.Button.WndProc(Message& m)
  14. em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  15. em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  16. em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  17. em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  18. em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
  19. em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  20. em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  21. em System.Windows.Forms.Application.Run(Form mainForm)
  22. em ProjetoAlpha.Program.Main() em C:UserswilliansourcereposProjetoAlphaProjetoAlphaProgram.cs:linha 19
  23.  
  24. using System;
  25. using Npgsql;
  26. using System.Data;
  27.  
  28. namespace ProjetoAlpha
  29. {
  30. class DAL
  31. {
  32. static string serverName = "localhost";
  33. static string port = "5432";
  34. static string userName = "postgres";
  35. static string password = "adm";
  36. static string databaseName = "dbestoque";
  37. NpgsqlConnection conn = null;
  38. string ConnString = null;
  39.  
  40. public DAL()
  41. {
  42. ConnString = String.Format("Server={0};Port={1};User Id{2};Password={3};Database={4};",
  43. serverName, port, userName, password, databaseName);
  44.  
  45. }
  46.  
  47. public DataTable GetTodosRegistros()
  48. {
  49. DataTable dt = new DataTable();
  50.  
  51. try
  52. {
  53. using (conn = new NpgsqlConnection(ConnString))
  54. {
  55. conn.Open();
  56. string cmdSeleciona = "SELECT * FROM estoque order by id_produto";
  57.  
  58. using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmdSeleciona, conn))
  59. {
  60. da.Fill(dt);
  61. }
  62. }
  63. }
  64. catch (NpgsqlException ex)
  65. {
  66. throw ex;
  67. }
  68. finally
  69. {
  70. conn.Close();
  71. }
  72.  
  73. return dt;
  74. }
  75.  
  76.  
  77. public DataTable GetRegistroPorId(int id)
  78. {
  79. DataTable dt = new DataTable();
  80.  
  81. try
  82. {
  83. using (NpgsqlConnection conn = new NpgsqlConnection(ConnString))
  84. {
  85. conn.Open();
  86. string cmdSeleciona = "SELECT * FROM ESTOQUE WHERE codigo =" + id;
  87.  
  88. using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmdSeleciona, conn))
  89. {
  90. da.Fill(dt);
  91. }
  92. }
  93. }
  94. catch (NpgsqlException ex)
  95. {
  96. throw ex;
  97. }
  98. catch(Exception ex)
  99. {
  100. throw ex;
  101. }
  102. finally
  103. {
  104. conn.Close();
  105. }
  106.  
  107. return dt;
  108. }
  109.  
  110. public void InserirRegistros(string nome, int codigo, int minimo, int maximo, int qtd, DateTime data)
  111. {
  112. try
  113. {
  114. using (NpgsqlConnection conn = new NpgsqlConnection(ConnString))
  115. {
  116. conn.Open();
  117.  
  118. string cmdInserir = String.Format("INSERT INTO ESTOQUE(nome,codigo,minimo,maximo,quantidade,dataEntrada) " +
  119. "VALUES ('{0}','{1}','{2}','{3}','{4}','{5}')", nome, codigo, minimo, maximo, qtd, data);
  120.  
  121. using (NpgsqlCommand cmd = new NpgsqlCommand(cmdInserir, conn))
  122. {
  123. cmd.ExecuteNonQuery();
  124. }
  125. }
  126. }catch(NpgsqlException ex)
  127. {
  128. throw ex;
  129. }catch(Exception ex)
  130. {
  131. throw ex;
  132. }
  133. finally
  134. {
  135. conn.Close();
  136. }
  137. }
  138. }
  139. }
  140.  
  141. private void btnGravar_Click(object sender, EventArgs e)
  142. {
  143. DAL acesso = new DAL();
  144.  
  145. try
  146. {
  147. acesso.InserirRegistros(txtNomeProduto.Text, Convert.ToInt32(txtCodigo.Text), Convert.ToInt32(txtMinimo.Text), Convert.ToInt32(txtMaximo.Text), Convert.ToInt32(txtEntrada.Text), dtpData.Value);
  148. }catch(Exception ex)
  149. {
  150. throw ex;
  151. }
  152. finally
  153. {
  154. MessageBox.Show("Cadastrado com sucesso!");
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement