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();
}
}
Jignesh TrivediPosted Sep 10, 2014, 12:51 AM
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.
Nitesh KejriwalPosted Sep 10, 2014, 12:39 AM
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";
Ramesh MaruthiPosted Sep 10, 2014, 12:24 AM
DataTable dt = ds1.Tables["funcionario"];
DataRow drow = dt.NewRow();
And make sure what values your getting into dt :)
IsraelPosted Sep 9, 2014, 5:40 PM
Dipankar BiswasPosted Sep 9, 2014, 4:42 PM
http://www.reddit.com/r/learnprogramming/comments/1se1f4/cadonet_getting_error_object_reference_not_set_to/