Marcel,
A first thing I would like to mention has to do with the use of Commands
(in your case OleDbCommand):
- It is much safer to work with command-parameters instead of building
your SQL-string. So create an OleDbCommand and set the
CommandTextProperty to: "INSERT INTO FailureControl (ControlDate,
ControlVersion) VALUES(?, ?)"
then add the OleDbParameters to the command. Check
http://msdn.microsoft.com/library/de...classtopic.asp
for more information.
Then, to receive the ID of the last inserted row, the best solution
depends on the database-system you are using. Post some more information
please. I then will try to solve you whole problem.
Greetz,
Dries
Marcel Hug wrote:[color=blue]
> Hi all !
> I have a table in my database, which has 3 attributes. IDFailureControl,
> ControlDate and ControlVersion.
>
> In the following function I test, if the date of today allready exists.
> Then I would like to write the new ControlDate or Version into the
> database. First i update the dataset, then i create a Insertcommand and
> call the update-methode. All datas are in the database, but....
>
> 1.) If I only use the InsertCommand, without updating the dataset, the
> datas would not be inserted to the database....?
>
> 2.) At the end I try to get the new created IDFailureControl
> (autoIncrement), but it does not exist. Why ? how can I get this ID ?
>
>
> here my code:
>
> public int WriteControlDate()
> {
> DateTime currentDate = DateTime.Today;
> DataSet currentDateDatas = new DataSet();
> int iVersion = 0;
> int iIndex = 0;
>
> try
> {
> this.dbConnection.Open();
>
> this.dbAdapter.SelectCommand = new OleDbCommand("SELECT *
> FROM FailureControl ORDER BY IDFailureControl", this.dbConnection);
> this.dbAdapter.Fill(currentDateDatas);
>
> if(currentDateDatas.Tables.Count != 0)
> {
> int iLastRowIndex = currentDateDatas.Tables[0].Rows.Count
> - 1;
> DataRow lastRow =
> currentDateDatas.Tables[0].Rows[iLastRowIndex];
> DateTime lastControlDate = (DateTime)
> lastRow["ControlDate"];
>
> if(lastControlDate.Equals(currentDate) == true)
> {
> iVersion = (int) lastRow["ControlVersion"];
> iVersion++;
> }
> }
>
> DataRow newRow = currentDateDatas.Tables[0].NewRow();
> newRow["ControlDate"] = currentDate;
> newRow["ControlVersion"] = iVersion;
> currentDateDatas.Tables[0].Rows.Add(newRow);
>
> this.dbAdapter.InsertCommand = new OleDbCommand("INSERT INTO
> FailureControl (ControlDate, ControlVersion) VALUES('" + currentDate +
> "', " + iVersion +")", this.dbConnection);
> this.dbAdapter.Update(currentDateDatas);
>
> int iNewIndex = currentDateDatas.Tables[0].Rows.Count - 1;
> newRow = currentDateDatas.Tables[0].Rows[iNewIndex];
> iIndex = (int) newRow["IDFailureControl"];
> }
> finally
> {
> this.dbConnection.Close();
> }
>
> return iIndex;
> }
>
> Thanks and regards[/color]