Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:55:31+00:00 2026-05-27T15:55:31+00:00

I dont know how to handle with DataSet very well (used to work with

  • 0

I dont know how to handle with DataSet very well (used to work with RecordSet in VB) so I made a method that looks very messy. Wondering which is the correct way to use DataSet.

Problems I would like to solve:

  • Is really necessary use 2 instances of DataAdapter, DataSet..?
  • Correct way to commit changes (working when add rows and not when update)
  • What should I dispose in finally blocks?

Thanks for attention.

Code is long because there are some loopings and consistences but what is confusing me is Editing or Insert rows using DataSet.

public bool SaveData()
{
    bool resp = false;
    this.pObs = null;
    bool editing = false;

    //StringBuilder stringBuilder;    
    string sqlQuery = "SELECT * FROM BooksTemp";
    string sqlQuery2 = "SELECT * FROM Categories;";    

    using (SqlConnection sqlConnection = new SqlConnection(connectionString))
    {
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery, connectionString);
        SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

        SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter(sqlQuery2, connectionString);
        SqlCommandBuilder sqlCommandBuilder2 = new SqlCommandBuilder(sqlDataAdapter2);
        DataSet dataSet2 = new DataSet();

        DataSet dataSet = new DataSet();
        DataTable dataTable = null;
        DataRow Row = null;

        try
        {
            sqlDataAdapter.Fill(dataSet, "BooksTemp");
            dataTable = dataSet.Tables["BooksTemp"];

            sqlDataAdapter2.Fill(dataSet2, "Categories");
            DataTable tableCategorias = dataSet2.Tables["Categories"];

            int i = 0;
            foreach (Ebook ebook in pListEbooks)
            {
                editing = false;

                #region loop_categories

                /* Loop all book categories before save in temporary book table*/
                string codCategorias = null;
                if (ebook.categories != null)
                {
                    bool aux;
                    string catID;

                    try
                    {
                        foreach (Categorias categoria in ebook.categories)
                        {
                            aux = false;
                            /* Search the categorie in DB */
                            if(tableCategorias.Select("CDD = '" + categoria.code + "'").Length > 0)
                                aux = true;

                            /* Include categorie in DB */
                            if (!aux)
                            {
                                /* Generate an ID */
                                catID = Strings.Codify();

                                //tableCategorias.Rows.Find(catID) didnt work
                                while (tableCategorias.Select("ID = '" + catID + "'").Length > 0)
                                {
                                    catID = Strings.Codifica();
                                }
                                Row = tableCategorias.NewRow();
                                Row.BeginEdit();

                                Row["ID"] = catID;
                                Row["Nome"] = categoria.description;

                                tableCategorias.Rows.Add(Row);
                                sqlDataAdapter2.Update(tableCategorias);
                            }
                        }
                    }
                    catch { }
                    finally 
                    {
                        // Shoud I dispose or close something here?
                    }
                }

                #endregion

                /* Verify if the book already have changes in DB */                
                if (dataTable.Select("DistribuidorLivroID = '" + ebook.id + "'").Length == 1)
                {
                    /* Edit row with new ebook changes */
                    editing = true;
                    Row = dataTable.Rows[i];
                    Row.BeginEdit();
                    Row["UpdatedOn"] = DateTime.Now;
                }
                else
                {
                    /* Add new row with ebook changes */
                    Row = dataTable.NewRow();
                    Row.BeginEdit();
                    Row["CreatedOn"] = DateTime.Now;
                }
                Row["DistribuidorLivroID"] = ebook.id;

                if (ebook.price != null)
                    Row["Price"] = ebook.price;
                if (ebook.sorting_title != null)
                    Row["Title"] = ebook.title;
                if (ebook.cover_image != null)
                    Row["Foto"] = ebook.cover_image;

                if (!editing)
                    dataTable.Rows.Add(Row);
                // else
                //     Row.AcceptChanges();

                // Commiting only when I add new row and not when I edit a row
                sqlDataAdapter.Update(dataTable);
                i++;
            }
        }
        catch { }
        finally
        {
            // What should I dispose here?            
        }
    }
    return resp;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:55:32+00:00Added an answer on May 27, 2026 at 3:55 pm

    I suggest you to use typed Datasets. These will solve all your problems and will improve the code quality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a method like this class PersonCollection { [Contracts.CanReturnNull] //dont know if something
I dont know how to handle new BigDecimal(0E30) . Its value is 0 but
I am developing an Android application and don't know hot to handle null values
I have a problem with a script and I don't know how to handle
I dont know if this is possible. In SSIS Derived column transformation, can you
I dont know how to debug structures like: fun1 <- function(obj){ a<-c(obj,4) c(a,5) }
I dont know how many values will be present from Cell A1 to A65555.
I dont know what happend but I cant register any dlls anymore. It seems
I dont know if my sessions are too big. Is there a way to
I dont know where my error is in this mysql query $sql = INSERT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.