1
Vote

LoadDataSetWithoutAcceptChanges option

description

Suggested code to add to file EL v.6 DAAB .../Data/database.cs

Explanation:
        //AcceptChanges() : Commits all the changes made to this DataSet since it was loaded or since the last time AcceptChanges was called.
        //AcceptChangesDuringFill: Gets or sets a value indicating whether AcceptChanges is called on a DataRow after it is added to the DataTable during any of the Fill operations.
        //If false, AcceptChanges is not called, and the newly added rows are treated as inserted rows: rowstate = added instead of unchanged.
Below code is needed in our webapplication to "Get the voter's data and answers to resume a session"

        //additions by Fryslan Webservices - SurveyProject.org


        /// <summary>
        /// Comment Step one:
        /// </summary>
        /// <param name="storedProcedureName">1</param>
        /// <param name="dataSet">2</param>
        /// <param name="tableNames">3</param>
        /// <param name="parameterValues">4</param>
        public virtual void LoadDataSetWithoutAcceptChanges(string storedProcedureName,
                                        DataSet dataSet,
                                        string[] tableNames,
                                        params object[] parameterValues)
        {
            using (DbCommand command = GetStoredProcCommand(storedProcedureName, parameterValues))
            {
                LoadDataSetWithoutAcceptChanges(command, dataSet, tableNames);
            }
        }


        /// <summary>
        /// Comment step 2
        /// </summary>
        /// <param name="command">1</param>
        /// <param name="dataSet">2</param>
        /// <param name="tableNames">3</param>
        public virtual void LoadDataSetWithoutAcceptChanges(DbCommand command,
                                DataSet dataSet,
                                string[] tableNames)
        {
            using (var wrapper = GetOpenConnection())
            {
                PrepareCommand(command, wrapper.Connection);
                DoLoadDataSetWithoutAcceptChanges(command, dataSet, tableNames);
            }
        }

        /// <summary>
        /// Method adjusted: AcceptChangesDuringFill added to set Rowstate to added
        /// </summary>
        /// <param name="command">command</param>
        /// <param name="dataSet">dataset</param>
        /// <param name="tableNames">tablenames</param>

        //Step Three:
        void DoLoadDataSetWithoutAcceptChanges(IDbCommand command,
                           DataSet dataSet,
                           string[] tableNames)
        {
            if (tableNames == null) throw new ArgumentNullException("tableNames");
            if (tableNames.Length == 0)
            {
                //throw new ArgumentException(Resources.ExceptionTableNameArrayEmpty, "tableNames");
                throw new ArgumentException("The table name array used to map results to user-specified table names cannot be empty.", "tableNames");
            }
            for (int i = 0; i < tableNames.Length; i++)
            {
                //if (string.IsNullOrEmpty(tableNames[i])) throw new ArgumentException(Resources.ExceptionNullOrEmptyString, string.Concat("tableNames[", i, "]"));
                if (string.IsNullOrEmpty(tableNames[i])) throw new ArgumentException("The value can not be null or an empty string.", string.Concat("tableNames[", i, "]"));
            }

            using (DbDataAdapter adapter = GetDataAdapter(UpdateBehavior.Standard))
            {
                ((IDbDataAdapter)adapter).SelectCommand = command;

                string systemCreatedTableNameRoot = "Table";
                for (int i = 0; i < tableNames.Length; i++)
                {
                    string systemCreatedTableName = (i == 0)
                                                        ? systemCreatedTableNameRoot
                                                        : systemCreatedTableNameRoot + i;

                    adapter.TableMappings.Add(systemCreatedTableName, tableNames[i]);
                }

                //added line to avoid AcceptChanges on the dataset and to force rowstate to "added" instead of "unchanged"; "added" will enforce the insert command
                adapter.AcceptChangesDuringFill = false;

                adapter.Fill(dataSet);
            }
        }

comments