Connecting Tech Pros Worldwide Forums | Help | Site Map

Add new record to database

Marko
Guest
 
Posts: n/a
#1: Nov 29 '06
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!






Steve B.
Guest
 
Posts: n/a
#2: Nov 29 '06

re: Add new record to database


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/....aspx?tabid=63

Hope that helps

"Marko" <markomarko@marko.coma écrit dans le message de news:
ekk1fc$39q$1@sunce.iskon.hr...
Quote:
>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!
>
>
>
>
>

Closed Thread