Cannot Create New Row in a Table

A

A.Ramezan

Hi everybody,

I cannot create a new record in my DB what is problem ?



oleDbConnection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\Temp\\AlphaControlDB.MDB");

oleDbConnection.Open();

OleDbDataAdapter DA = new OleDbDataAdapter() ;

DA.SelectCommand = new OleDbCommand( "SELECT * FROM Days", oleDbConnection);

OleDbCommandBuilder custCB = new OleDbCommandBuilder(DA);

DataSet DS = new DataSet();

DA.Fill( DS, "DAYS" );



DataRow Row = DS.Tables["Days"].NewRow();

Row["Description"] = "Alvarez";


DS.Tables["Days"].Rows.Add( Row );



//Row.AcceptChanges();

//myTable.AcceptChanges();

DS.AcceptChanges();

DA.Update( DS, "DAYS");

oleDbConnection.Close();



any combination on accept changes do not effect.
 
M

Miha Markic [MVP C#]

Call AcceptChanges after Update. Or better, if you don't have any specific
need, you don't to call it at all.
 
A

A.Ramezan

Dear William,

1.When I switch Acceptchange and Update Lines, an ecception occures,
2. When I remark both of them, it does not add record to my table.

If it is possible, please check it.
thanks.


William Ryan eMVP said:
I see Miha answered this and he's right on. .AcceptChanges right before
update virtually guarantees that no update will occur... this might help
explain it http://www.knowdotnet.com/articles/efficient_pt4.html

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
A.Ramezan said:
Hi everybody,

I cannot create a new record in my DB what is problem ?



oleDbConnection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\Temp\\AlphaControlDB.MDB");

oleDbConnection.Open();

OleDbDataAdapter DA = new OleDbDataAdapter() ;

DA.SelectCommand = new OleDbCommand( "SELECT * FROM Days", oleDbConnection);

OleDbCommandBuilder custCB = new OleDbCommandBuilder(DA);

DataSet DS = new DataSet();

DA.Fill( DS, "DAYS" );



DataRow Row = DS.Tables["Days"].NewRow();

Row["Description"] = "Alvarez";


DS.Tables["Days"].Rows.Add( Row );



//Row.AcceptChanges();

//myTable.AcceptChanges();

DS.AcceptChanges();

DA.Update( DS, "DAYS");

oleDbConnection.Close();



any combination on accept changes do not effect.
 
C

Cor Ligthert

Hi Ramezan,

It is very simple, when you do acceptchanges, there are no updatable
records, and therefore you get probably no error in the update.

However that does not help you, so delete that acceptchanges, as Miha and
Bill said it does nothing in this case (I give you a sample where the
acceptchanges is needed completly bellow) and set your update in a try catch
end try block like this.

try
update.fill(blabla)
catch ex as exception
messagebox.show(ex.tostring)
end try

Now you can see better what is the error.

To give you a sample where by example you can use the *acceptchanges*.

You create by hand a datatable and fill it with three names, now you call
acceptchanges, and bind it in to a textbox and let your user do changes. Now
will be registered what changes the user does. When you had not done the
acceptchanges here before, everything was already in a changed state.

I hope this helps?

Cor
 
M

Mohamed El Ashmawy

I agree with all that was said before.

I don't know the structure of the days table you are using, but I notice
that the only field you are filling in the new row thatyou create is a
"Description" field.
Does the table contain a primary key? If so, then you should supply a
unique value for it in your new row.
Please check that you create the new row and supply in it all the required
fields.
Reading the exception detailsmight help you indntify the exact cause of the
error

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 

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

Top