Add new record to database

M

Marko

I am trying to add new record in database. There is no compile error but
program is not adding any record to database. Hot to fix it? This is my
code:

OleDbDataAdapter odbAdapt;
DataSet dsObject= new DataSet();
DataTable dtObject;
string SQL;
string ConnStr;
int BrojSlogova;

ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=c:/dbase/Proba.mdb";
SQL = "SELECT * FROM Names"
OleDbConnection odbconn = new OleDbConnection();
odbconn.ConnectionString = ConnStr;
odbconn.Open();
odbAdapt = new OleDbDataAdapter (SQL, ConnStr);
dsObject = new DataSet();
odbAdapt.Fill(dsObject);
dtObject = dsObject.Tables[0];

// Then add new record

DataRow WorkRow;
WorkRow= dtObject.NewRow();
WorkRow["FirstName"] = "George";
WorkRow["LastName"] = "Bush";
dtObject.Rows.Add(WorkRow);

This doesn't work!
 
S

Steve B.

Firstly, if you don't have an insert method set in the DataAdapter, you
won't be able to use the DataAdapter to update (meaning insert, update or
delete) the db.
Secondly, when you add a row to a datatable, you only add it to the
datatable object which is a memory representation of relationnal datas (not
specific to a platform). You have to use the Update method of the
DataAdapter and passed the modified datatable in order to apply updates to
the DB.
Thirdly, since .Net 2.0, TableAdapters are more accurates than DataAdapters.

In order to help understanding ADO.Net concepts (especially disconnect
working), I suggest you to follow this tutorial which is a very good one :
http://www.asp.net/learn/dataaccess/default.aspx?tabid=63

Hope that helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top