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;
MessageBox.Show("Record / Entry Added");
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();
}
}

Dipankar BiswasRank #992 · 2K reputationPosted Sep 9, 2014, 12:42 PM EDT
http://www.reddit.com/r/learnprogramming/comments/1se1f4/cadonet_getting_error_object_reference_not_set_to/
IsraelRank #1,223 · 1.5K reputationPosted Sep 9, 2014, 1:40 PM EDT
Ramesh MaruthiRank #937 · 2.2K reputationPosted Sep 9, 2014, 8:24 PM EDT
DataTable dt = ds1.Tables["funcionario"];
DataRow drow = dt.NewRow();
And make sure what values your getting into dt :)
Nitesh KejriwalRank #253 · 9.9K reputationPosted Sep 9, 2014, 8:39 PM EDT
da.Fill(ds1, "funcionario");
After the above line, ds1 is not populated with any data but is set to NULL. If this is not true, also check ds1 has a table named "funcionario" using the designer. If not, you can try
ds1.Tables[0].TableName = "funcionario";
Jignesh TrivediRank #16 · 50.3K reputationPosted Sep 9, 2014, 8:51 PM EDT
Hi,
This error is occurred when you trying to access property or method of null object....
First of all check whether ds1 is null if not then check it has table called "funcionario"?
hope this will help you.