DCSIMG
Quiz - Ido Samuelson's blog

Quiz

I found this question to be somewhat problematic even for experience developers :-) Let's see how well you know ADO.NET?

Here is a sample DataSet that holds the tables:

 

Here is a code sample that do the following:

adds to the DataSet 4 rows: 2 parent rows and then 2 child rows for the 1st parent.

QuizDataSet ds = new QuizDataSet();

ds.Parent.AddParentRow("Parent A");

ds.Parent.AddParentRow("Parent B");

 

ds.Child.AddChildRow(ds.Parent[0], "Child A");

ds.Child.AddChildRow(ds.Parent[0], "Child B");

The question:

What is the proper way to update the added rows into the database?

Published Wednesday, November 29, 2006 7:17 PM by Ido Samuelson

Comments

# re: Quiz

sqlDataAdapter1.Update(ds, "Parent");

sqlDataAdapter2.Update(ds, "Child");

or

sqlDataAdapter1.Update(ds);

sqlDataAdapter2.Update(ds);

Wednesday, November 29, 2006 10:07 PM by Uri

# re: Quiz

I'm sure this lines of code will exist, however you are missing a very important thing. What you missing is the tricky part :)

Thursday, November 30, 2006 5:04 AM by Ido Samuelson

# re: Quiz

I admit, maybe I have failed to understand your question. I have tested this code with a small test program and it works.

If some error occures while updating the parent table the whole transaction will be rollbacked. The same will happen with the child table.

So I cannot see any potential flaw with this code.

Can you ellaborate?

Thursday, November 30, 2006 2:42 PM by Uri

# re: Quiz

Check two things

1. The PK in the DB for table Child. See to which Parent the childs are connected.

2. Check the IDs on the Dataset tables.

Thursday, November 30, 2006 2:47 PM by Ido Samuelson

# re: Quiz

Hi,

I still don't see the problem. As ids in the DSs weren't specified I have assumed that the ids (Parent.Id & Child.Id) are identities and have been properly declared in the typed DS schema (with their AutoIncrementSeed & AutoIncrementStep), otherwise you wouldn't use such a syntax. Off course you still have to update the parent's table first and the child's table second. The child's records refer to the first record "Parent A" of the parent's table.

Thursday, November 30, 2006 10:37 PM by Uri

# re: Quiz

What you are saying is all true and make sense however in code it is a bit different. Please post your code.

Thursday, November 30, 2006 11:07 PM by Ido Samuelson

# re: Quiz

Hi,

very quick code for proof of concept:

(make sure please that the typed ds's schema contains the AutoIncrementSeed & AutoIncrementStep definitions)

TDS ds = new TDS();

ds.t1.Addt1Row("t1r1");

ds.t1.Addt1Row("t1r2");

ds.t2.Addt2Row(ds.t1[0], "t2r1");

ds.t2.Addt2Row(ds.t1[0], "t2r2");

try

{

sqlConnection1.Open();

SqlTransaction tran = sqlConnection1.BeginTransaction(IsolationLevel.ReadCommitted);

sqlDataAdapter1.InsertCommand.Transaction = tran;

sqlDataAdapter2.InsertCommand.Transaction = tran;

try

{

 sqlDataAdapter1.Update(ds, "t1");

 sqlDataAdapter2.Update(ds, "t2");

 tran.Commit();

}

catch

{

 tran.Rollback();

}

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

finally

{

sqlConnection1.Close();

}

Thursday, November 30, 2006 11:35 PM by Uri

# re: Quiz

Ok,

Now show me the following:

1. The insert SQL statement.

2. the rows on both t1 and t2 from your database.

3. the rows you now have on your dataset.

If you check it carefully you have a problem.

Friday, December 01, 2006 12:06 AM by Ido Samuelson

# re: Quiz

1. The insert command is generated automatically while defining the SqlDataAdapter (I have used the simplest way and generated all the commands after specifying the select command [e.g. select * from t1]). As I have writtern I was trying to provide the quickest way of proof of concept.

Here is a sample from VS.NET generated code:

INSERT INTO t1(data) VALUES (@data)

2. In DS you will have.

t1-> 1, t1r1

    2, t1r2

t2 ->

    1, 1, t2r1

    2, 1, t2r2

The same will be the result in DB!

The next result will increment the identities in DB!

3. Where do you see the problem? All the referential integrities are kept and none are violated. If you applying on the existing rows in DB, it's not a problem, because the identities are managed on the DB level, therefore you don't provide them.

I have tested it either.

I will happily send you all the solution and the SQL code if you are willing to test the results by yourself.

Friday, December 01, 2006 12:42 AM by Uri

# re: Quiz

the way you describe it, you have a problem. Please send me your solution to isamuelson at nana.co.il.

Friday, December 01, 2006 1:06 AM by Ido Samuelson

# re: Quiz

1. I sended you T.zip file. It includes the solution and the script.sql which creates all the necessary tables with its' definitions.

2. You are repeating by telling that I have a problem in my solution but you never have pinpointed it. I really don't see any problem here. As I am the only one who participates in the Quiz :), may be you will post what is exactly the problem in your opinion that I could address the issue.

Friday, December 01, 2006 10:34 AM by Uri

# re: Quiz

I have managed to create the bug with your code.

I sent you an email with the problem.

For everyone else.

What can happen is that the DB will return an ID that already exist in the Dataset and will cause an exception.

NOTICE: turning enforce constrains off on the dataset is not going to help!

Friday, December 01, 2006 2:29 PM by Ido Samuelson

# re: Quiz

Do you get off on these quizes?

.. keep doing it! :D

Saturday, January 06, 2007 12:05 AM by Plakstift

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: