/// <summary>
        /// Counts number of input fields for given product template
        /// </summary>
        /// <param name="productTemplateId">Id of the product template to count for</param>
        /// <param name="scope">scope of input fields to consider</param>
        /// <returns>Number of input fields for given product template</returns>
        public static int CountForProductTemplate(int productTemplateId, InputFieldScope scope)
        {
            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand("SELECT COUNT(*) AS TotalRecords FROM ac_InputFields WHERE ProductTemplateId = @productTemplateId");

            database.AddInParameter(selectCommand, "@productTemplateId", System.Data.DbType.Int32, productTemplateId);
            return(CommerceBuilder.Utility.AlwaysConvert.ToInt(database.ExecuteScalar(selectCommand)));
        }
 private static string GetScopeClause(InputFieldScope scope)
 {
     if (scope == InputFieldScope.Customer)
     {
         return(" AND IsMerchantField = 0");
     }
     if (scope == InputFieldScope.Merchant)
     {
         return(" AND IsMerchantField = 1");
     }
     return(string.Empty);
 }
 public static InputFieldCollection LoadForProductTemplate(int productTemplateId, InputFieldScope scope, int maximumRows, int startRowIndex)
 {
     return(LoadForProductTemplate(productTemplateId, scope, maximumRows, startRowIndex, string.Empty));
 }
 public static InputFieldCollection LoadForProductTemplate(int productTemplateId, InputFieldScope scope, string sortExpression)
 {
     return(LoadForProductTemplate(productTemplateId, scope, 0, 0, sortExpression));
 }
 public static InputFieldCollection LoadForProductTemplate(int productTemplateId, InputFieldScope scope)
 {
     return(LoadForProductTemplate(productTemplateId, scope, 0, 0, string.Empty));
 }
        public static InputFieldCollection LoadForProductTemplate(int productTemplateId, InputFieldScope scope, int maximumRows, int startRowIndex, string sortExpression)
        {
            //DEFAULT SORT EXPRESSION
            if (string.IsNullOrEmpty(sortExpression))
            {
                sortExpression = "OrderBy";
            }
            //CREATE THE DYNAMIC SQL TO LOAD OBJECT
            StringBuilder selectQuery = new StringBuilder();

            selectQuery.Append("SELECT");
            if (maximumRows > 0)
            {
                selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
            }
            selectQuery.Append(" " + InputField.GetColumnNames(string.Empty));
            selectQuery.Append(" FROM ac_InputFields");
            selectQuery.Append(" WHERE ProductTemplateId = @productTemplateId");
            selectQuery.Append(GetScopeClause(scope));
            selectQuery.Append(" ORDER BY " + sortExpression);
            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());

            database.AddInParameter(selectCommand, "@productTemplateId", System.Data.DbType.Int32, productTemplateId);
            //EXECUTE THE COMMAND
            InputFieldCollection results = new InputFieldCollection();
            int thisIndex = 0;
            int rowCount  = 0;

            using (IDataReader dr = database.ExecuteReader(selectCommand))
            {
                while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
                {
                    if (thisIndex >= startRowIndex)
                    {
                        InputField inputField = new InputField();
                        InputField.LoadDataReader(inputField, dr);
                        results.Add(inputField);
                        rowCount++;
                    }
                    thisIndex++;
                }
                dr.Close();
            }
            return(results);
        }