Goodevening everybody,
i am developing an application with two tables. Those tables have parent - child control. What i want to do is press the button of add new row and a new row is added to the parent and child. So far so good. The problem is when i wish to fill the gaps that the autoicrement property leaves after deletion of some row.
The code i use is:
Code:// The connection string string ConnString = Settings.Default.axisConnectionString; string Query = " INSERT INTO dbo.owner" + " (ID, NAME, SURNAME)" + " VALUES (" + inside_row_index.ToString().Trim() + ",' ',' ')"; // Create my connection SqlConnection insertconn = new SqlConnection(ConnString); // Create my command SqlCommand insertcommand = insertconn.CreateCommand(); insertcommand.CommandText = Query; try { // Connect to database insertconn.Open(); // Create the transaction mytran = insertconn.BeginTransaction(System.Data.IsolationLevel.RepeatableRead); // Execute query insertcommand.ExecuteNonQuery(); // Before creating the child save the parent matrix // and refresh the grid axisDataSet.ownerDataTable newParentRecord = (axisDataSet.ownerDataTable)axisDataSet.owner.GetChanges(); if (newParentRecord != null) { // Update the grid Validate(); ownerTableAdapter.Update(axisDataSet); axisDataSet.AcceptChanges(); ownerDataGridView.Refresh(); // Child add row // Return the obtained id //new_child_row["ID"] = inside_row_index; //axisDataSet.xyz.Rows.Add(new_child_row); mytran.Commit(); insertconn.Close(); } else { throw new Exception("Could not save or create the new row."); } } catch (SqlException a) { MessageBox.Show(a.Message, "Exciting.", MessageBoxButtons.OK, MessageBoxIcon.Error); mytran.Rollback(); return; } catch (Exception a) { MessageBox.Show(a.Message, "Exciting.", MessageBoxButtons.OK, MessageBoxIcon.Error); mytran.Rollback(); return; }
I tried to use the sql statement set_identity on and off but error occured. After deleting that it seemed that it worked but when i try to use the new row to create a new child row an error concening the foreign key arises. This indicates that the row is not inserted into database.
So i updated before creating new row for the child. Still the same. Then i called the GetChanges and saw that no changes are happening?
Do you have any ideas because i seem to have run out!!!
This is the code i use to create the new rows
Any idea is welcomeCode:DataRow new_row = axisDataSet.owner.NewRow(); DataRow new_child_row = axisDataSet.xyz.NewRow();
ioigoume





Reply With Quote