how to insert one row at a time from each  table exits in dataset

Posted on 2009-02-13
Last Modified: 2013-12-17
I am reading one XML file and storing data in 5 different datable, now these are related to each other.
here is my req.
I want to insert one row of each table a time, so that one complete node can be inserted in to the database.
and also I want to make sure that if some records contains error then all the records before this records must be committed or permanently stored into the database.
right now i am using transaction and looping of each records some thing like shown below.
I want to know is there is any proper or good way to do this.
_transaction = _connection.BeginTransaction();

        try {

           // InsertAudit(sourceID);


            foreach(DataRow PArow in _dataset.Tables["ProductCategory"].Rows) {

                if (CheckRecords(PArow,"ProductCategory") == false) {



                foreach (DataRow Productrow in _dataset.Tables["Product"].Rows) {

                    if (PArow["OID"].ToString() == Productrow["PrductCatID"].ToString()){

                        if (CheckRecords(Productrow, "Product") == false) {



                        foreach (DataRow IssueProductrow in _dataset.Tables["IssueProduct"].Rows) {

                            if (Productrow["OID"].ToString() == IssueProductrow["ProductID"].ToString()) {

                                if (CheckRecords(IssueProductrow, "IssueProduct") == false) {









            foreach (DataRow Issuerow in _dataset.Tables["Issue"].Rows) {

                if (CheckRecords(Issuerow, "Issue") == false) {


                    foreach (DataRow IssueProductXIssuerow in _dataset.Tables["IssueProductXIssue"].Rows) {

                        if (Issuerow["OID"].ToString() == IssueProductXIssuerow["IssueID"].ToString()) {







            foreach (DataRow rowArticle in _dataset.Tables["Article"].Rows) {

                if (Convert.ToInt16(rowArticle["Inserted"]) == 1) {

                    CheckRecords(rowArticle, "Article");



                    foreach (DataRow rowAV in _dataset.Tables["ArticleVersion"].Rows) {

                        if (rowArticle["OID"].ToString() == rowAV["ArticleID"].ToString()) {

                            CheckRecords(rowAV, "ArticleVersion");



                            foreach (DataRow rowPA in _dataset.Tables["ProjectAudience"].Rows) {

                                if (rowAV["OID"].ToString() == rowPA["ArticleVersionID"].ToString()) {

                                    CheckRecords(rowPA, "ProjectAudience");




                            foreach (DataRow rowAC in _dataset.Tables["ArticleContent"].Rows) {

                                if (rowAV["OID"].ToString() == rowAC["ArticleVersionID"].ToString()) {

                                    CheckRecords(rowAC, "ArticleContent");




                            foreach (DataRow rowImages in _dataset.Tables["Images"].Rows) {

                                if (rowAV["OID"].ToString() == rowImages["ArticleVersionID"].ToString()) {

                                    CheckRecords(rowImages, "Images");









        } catch(Exception e) {



Open in new window

Question by:ziorrinfotech
    LVL 27

    Accepted Solution

    One problem that is apparent from the code above is that you have an all or nothing situation here:

    One exception anywhere, will cause your whole transaction to roll back.
    Is this what you want?

    You are also trying to do too much in one method.
    For example, I would suggest you move the handling of Product to another method, and then again, from there call the method that handles Issue Product.

    You are also repeating the same code all the time. you probably can write just one method that accepts a table name and the condition that needs to be passed to CheckRecords and call that instead

    Just a few suggestions as to what would be the proper and good way to do this in my opinion


    Author Comment

    no I want to insert one row of each table at a time.

    how can i do this

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now