PC Review


Reply
Thread Tools Rate Thread

Adding New record - ADO.NET

 
 
Darryn Ross
Guest
Posts: n/a
 
      22nd Jul 2003
Hi,

I am having trouble saving my new record to my access database, the dataset
gets updated fine but the new record is not being added to the database for
which it is looking at. Here is my code, any help would be appreciated.

DtlsConnection.ConnectionString = strConnectionPath ;
DtlsConnection.Open() ;

SelectDtlsCommand.CommandText = "Select * From tble" ;
SelectDtlsCommand.Connection = DtlsConnection ;
DtlsAdapter.SelectCommand = SelectDtlsCommand ;
DtlsAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
DtlsAdapter.Fill(DtlsDs, "tble") ;

DataRow tblRow ;
DataTable tbl ;
tbl = DtlsDs.Tables["tble"] ;
tblRow = tbl.NewRow() ;

try {
tblRow["Code"] = txtCode.Text ;
tblRow["Name"] = txtName.Text ;
tblRow["Type"] = txtType.Text ;
tbl.Rows.Add(tblRow) ;
DtlsDs.AcceptChanges() ;
}
catch(Exception e) {
MessageBox.Show(e.Message, "Error Saving Data", MessageBoxButtons.OK,
MessageBoxIcon.Error) ;
}


Kind Regards
Darryn Ross


 
Reply With Quote
 
 
 
 
Stephen Muecke
Guest
Posts: n/a
 
      22nd Jul 2003
Darryn

After you have added the new row, you need to call the Update method of the
DataAdaptor
Do not call AcceptChanges (this marks the row as 'unchanged' so when Update
is called, there are no 'new' rows to write back to the database!)
The Update method, if successful will in effect call AcceptChanges anyway

Stephen


"Darryn Ross" <(E-Mail Removed)> wrote in message
news:%23j$pVo%(E-Mail Removed)...
> Hi,
>
> I am having trouble saving my new record to my access database, the

dataset
> gets updated fine but the new record is not being added to the database

for
> which it is looking at. Here is my code, any help would be appreciated.
>
> DtlsConnection.ConnectionString = strConnectionPath ;
> DtlsConnection.Open() ;
>
> SelectDtlsCommand.CommandText = "Select * From tble" ;
> SelectDtlsCommand.Connection = DtlsConnection ;
> DtlsAdapter.SelectCommand = SelectDtlsCommand ;
> DtlsAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
> DtlsAdapter.Fill(DtlsDs, "tble") ;
>
> DataRow tblRow ;
> DataTable tbl ;
> tbl = DtlsDs.Tables["tble"] ;
> tblRow = tbl.NewRow() ;
>
> try {
> tblRow["Code"] = txtCode.Text ;
> tblRow["Name"] = txtName.Text ;
> tblRow["Type"] = txtType.Text ;
> tbl.Rows.Add(tblRow) ;
> DtlsDs.AcceptChanges() ;
> }
> catch(Exception e) {
> MessageBox.Show(e.Message, "Error Saving Data", MessageBoxButtons.OK,
> MessageBoxIcon.Error) ;
> }
>
>
> Kind Regards
> Darryn Ross
>
>



 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      22nd Jul 2003
>I am having trouble saving my new record to my access database, the dataset
>gets updated fine but the new record is not being added to the database for
>which it is looking at. Here is my code, any help would be appreciated.
>

You're basically saving your new record to the data set - which is an
in-memory copy of your data. You don't ever write it back to the
database!

Check out the DataAdapter's "Update" method - that will then write
back the data to the database. You can have it run your updates
automagically, by means of providing insert, update, and delete
commands (SQL queries, or preferably stored procedures), or you can
"roll you own" if you want to.

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: adding a new record and saving it, moving to next, previous record using a form in Access 2007 John W. Vinson Microsoft Access 1 24th Jun 2010 10:03 PM
adding a new record at subform should update an existing record (not create new record) Mark Kubicki Microsoft Access Form Coding 1 16th Jan 2009 08:34 AM
How does one protect against subform adding record before main form has record? Chips Microsoft Access Forms 3 2nd May 2004 04:19 PM
Deleting a record follow by Adding a record will result in getting wrong Index Key problem =?Utf-8?B?TGlzYSBKb25lcw==?= Microsoft VB .NET 0 30th Apr 2004 06:56 PM
form / subform - problem adding new record! - can't save main record Bob Microsoft Access Forms 2 7th Oct 2003 06:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:04 AM.