I’m trying to update the database using a method were the parameters of the method will be used rather then using an SQL statement. I did this so a user can enter information into the database.
System.Data.OleDb.OleDbConnection con;
DataSet dsl;
System.Data.OleDb.OleDbDataAdapter da;
public String updateDatabase(int id, String Fname, String Lname, int age, string job)
{
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Owner\\Documents\\CIS3052.mdb";
dsl = new DataSet();
da = new System.Data.OleDb.OleDbDataAdapter();
con.Open();
DataRow dRow = dsl.Tables["Employee"].NewRow();
dRow["ID"] = id;
dRow["Fname"] = Fname;
dRow["Lname"] = Lname;
dRow["Age"] = age;
dRow["Job"] = job;
dsl.Tables["Employee"].Rows.Add(dRow);
da.Update(dsl, "Employee");
return null;
}
The problem I have is in this line:
DataRow dRow = dsl.Tables["Employee"].NewRow();
It is telling me:
Object reference not set to an instance of an object.
I tried using da.Fill() but I cannot find a way to use that without using a SQL statement.
Thanks.
Because
dsl.Tables["Employee"]dose not exist. Once you filldsl.Tables["Employee"]withda.Fill()then it become available.Once you called
You are trying to access the “Employee” table in dataset, but dataset does not know about
Employeetable until you tell it (by callingda.Fill()).IMO there is no way to call
DataRow dRow = dsl.Tables["Employee"].NewRow();without filling it.