Israel

Israel

  • 698
  • 1.3k
  • 217.6k

Object reference not set to an instance of an object.

Sep 9 2014 4:22 PM
Hi!

When I click on my save button I receive this message. But sound that everything it's right? Why this message? "Object reference not set to an instance of an object."


These my codes:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace LunaOffice
{
    public partial class VisualRecursosHumanos : Form
    {
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\lunaoffice\lunaoffice\app_data\office.accdb;");
        
        public VisualRecursosHumanos()
        {
            InitializeComponent();
        }

        OleDbConnection con;
        DataSet ds1;
        OleDbDataAdapter da;

        int MaxRows = 0;
        int inc = 0;

private void Save_Click(object sender, EventArgs e)
        {
            //////////////////////////

            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(da);

            DataRow drow = ds1.Tables["funcionario"].NewRow();// The bug it's here

            drow[0] = txtNomesCadastFunc.Text;
       
            ds1.Tables["funcionario"].Rows.Add(drow);

            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;

con.Open();
        da.Update(ds1, "funcionario");
con.Close();

            MessageBox.Show("Record / Entry Added");
        }

        private void VisualRecursosHumanos_Load(object sender, EventArgs e)
        {
        
            //con = new.OleDbConnection();
            
            ds1 = new DataSet();

            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/lunaoffice/lunaoffice/app_data/office.accdb";

            string sql = "SELECT * from funcionario";
            da = new OleDbDataAdapter(sql, con);

            con.Open();
            da.Fill(ds1, "funcionario");
            //NavigateRecords();
            MaxRows = ds1.Tables["funcionario"].Rows.Count;
            
            con.Close();
            con.Dispose();
        }


        }

Answers (5)