Beispiel #1
0
        /// <summary>
        /// Fetches credits packages by specified arguments.
        /// It returns an empty array if there are no credits packages in DB by specified arguments.
        /// If these arguments are null it returns all credits packages from DB.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The name.</param>
        /// <param name="quantity">The quantity.</param>
        /// <param name="price">The price.</param>
        /// <param name="sortColumn">The sort column.</param>
        /// <returns></returns>
        private static CreditsPackage[] Fetch(int? id, string name, int? quantity, decimal? price, eSortColumn sortColumn)
        {
            using (SqlConnection conn = Config.DB.Open())
            {
                SqlDataReader reader = (SqlDataReader)
                    SqlHelper.GetDB().ExecuteReader( "FetchCreditsPackages", id, name, quantity, price, sortColumn);

                List <CreditsPackage> lCreditsPackage = new List<CreditsPackage>();

                while (reader.Read())
                {
                    CreditsPackage creditsPackage = new CreditsPackage();

                    creditsPackage.id = (int) reader["ID"];
                    creditsPackage.name = (string) reader["Name"];
                    creditsPackage.quantity = (int) reader["Quantity"];
                    creditsPackage.price = (decimal) reader["Price"];

                    lCreditsPackage.Add(creditsPackage);
                }

                return lCreditsPackage.ToArray();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!HasWriteAccess)
                return;

            #region validation

            string name = txtName.Text.Trim();
            int quantity = 0;
            decimal price = 0;

            if (name.Length == 0)
            {
                MessageBox1.Show(Lang.TransA("Please enter name"), Misc.MessageType.Error);
                return;
            }

            if (!Int32.TryParse(txtQuantity.Text.Trim(), out quantity))
            {
                MessageBox1.Show(Lang.TransA("Please enter valid quantity"), Misc.MessageType.Error);
                return;
            }

            if (quantity < 0)
            {
                MessageBox1.Show(Lang.TransA("The quantity can't be negative!"), Misc.MessageType.Error);
                return;
            }

            if (!Decimal.TryParse(txtPrice.Text.Trim(), out price))
            {
                MessageBox1.Show(Lang.TransA("Please enter valid price"), Misc.MessageType.Error);
                return;
            }

            if (price < 0)
            {
                MessageBox1.Show(Lang.TransA("The price can't be negative!"), Misc.MessageType.Error);
                return;
            }

            #endregion

            CreditsPackage creditsPackage;
            
            if (CurrentPackageID.HasValue)
            {
                creditsPackage = CreditsPackage.Fetch(CurrentPackageID.Value);                
            }
            else
            {
                creditsPackage = new CreditsPackage();
            } 

            creditsPackage.Name = name;
            creditsPackage.Quantity = quantity;
            creditsPackage.Price = price;

            creditsPackage.Save();

            loadPackages();

            mvCreditsPackages.SetActiveView(viewCreditsPackages);
        }