protected void ValidateSupplementID(object source, ServerValidateEventArgs args)
        {
            string supplementID = args.Value;

            if (Regex.IsMatch(supplementID, @"^\w+\-?\w*$"))
            {
                SportShopEntities context = new SportShopEntities();
                var supplementIDs         =
                    from bs in context.supplement
                    select bs.supplement_id;
                int intSupplementID = int.Parse(supplementID);
                if (!supplementIDs.Any(bs => bs == intSupplementID))
                {
                    args.IsValid = true;
                }
                else
                {
                    CustomValidatorSupplementID.ErrorMessage = "* Съществуващо ID";
                    args.IsValid = false;
                }
            }
            else
            {
                CustomValidatorSupplementID.ErrorMessage = "* Невалидно ID";
                args.IsValid = false;
            }
        }
        protected void ValidateSupplementAvailableInShop(object source, ServerValidateEventArgs args)
        {
            string            sportShopID = args.Value;
            SportShopEntities context     = new SportShopEntities();
            var sportShopIDs =
                from sInfo in context.shop_info
                select sInfo.shop_id;

            if (!sportShopIDs.Any(sInfo => sInfo == sportShopID))
            {
                string supplementID = InpSupplementAvailableInShop.Text;
                if (supplementID == sportShopID)
                {
                    args.IsValid = true;
                }
                else
                {
                    CustomValidatorSupplementAvailableInShop.ErrorMessage = "*  Не съществува магазин с този номер";
                    args.IsValid = false;
                }
            }
            else
            {
                args.IsValid = true;
            }
        }
        protected void ValidateSportShopID(object source, ServerValidateEventArgs args)
        {
            string            sportShopID = args.Value;
            SportShopEntities context     = new SportShopEntities();
            var sportShopIDs =
                from sInfo in context.shop_info
                select sInfo.shop_id;

            if (!sportShopIDs.Any(sInfo => sInfo == sportShopID))
            {
                args.IsValid = true;
            }
            else
            {
                CustomValidatorInpSportShopID.ErrorMessage = "* Съществуващо ID";
                args.IsValid = false;
            }
        }
        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                XDocument doc = new XDocument(
                    new XDeclaration("1.0", "utf-8", string.Empty),
                    new XDocumentType("sport_shop", null, "sport_shop.dtd", null),
                    new XElement("sport_shop",
                                 new XElement("shop_info",
                                              new XAttribute("shop_id", InpSportShopID.Text),
                                              new XElement("shop_name", InpSportShopName.Text),
                                              new XElement("country", InpCountry.Text),
                                              new XElement("city", InpCity.Text),
                                              new XElement("address", InpAddress.Text),
                                              new XElement("phone", InpPhNumber.Text),
                                              new XElement("email", InpEmail.Text),
                                              new XElement("site", InpSite.Text)
                                              ),
                                 new XElement("supplements",
                                              new XElement("supplement",
                                                           new XAttribute("supplement_id", InpSupplementID.Text),
                                                           new XAttribute("type", InpSupplementType.Text),
                                                           new XAttribute("purpose", InpSupplementPurpose.Text),
                                                           new XAttribute("supplement_brand", InpSupplementBrand.Text),
                                                           new XAttribute("available_in_shop", InpSupplementAvailableInShop.Text),
                                                           new XElement("supplement_name", InpSupplementName.Text),
                                                           new XElement("supplement_price", InpSupplementPrice.Text,
                                                                        new XAttribute("currency", InpSupplementCurrency.Text)
                                                                        ),
                                                           new XElement("supplement_rating", InpSupplementRating.Text),
                                                           new XElement("taste", InpTaste.Text),
                                                           new XElement("packing", InpPacking.Text,
                                                                        new XAttribute("measurement_unit", InpMeasurementUnit.Text)
                                                                        ),
                                                           new XElement("servings", InpServings.Text)
                                                           )
                                              ),
                                 new XElement("sport_accessories",
                                              new XElement("boxing_gloves",
                                                           new XAttribute("boxing_gloves_id", InpBoxingGlovesID.Text),
                                                           new XAttribute("available_in_shop", InpBoxingGlovesAvailableInShop.Text),
                                                           new XElement("boxing_gloves_model", InpBoxingGlovesModel.Text),
                                                           new XElement("boxing_gloves_price", InpBoxingGlovesPrice.Text,
                                                                        new XAttribute("currency", InpBoxingGlovesCurrency.Text)
                                                                        ),
                                                           new XElement("boxing_gloves_size", InpBoxingGlovesSize.Text),
                                                           new XElement("boxing_gloves_rating", InpBoxingGlovesRating.Text),
                                                           new XElement("boxing_gloves_material", InpBoxingGlovesMaterial.Text),
                                                           new XElement("origin", InpOrigin.Text)
                                                           ),
                                              new XElement("sport_shoes",
                                                           new XAttribute("sport_shoes_id", InpSportShoesID.Text),
                                                           new XAttribute("available_in_shop", InpSportShoesAvailableInShop.Text),
                                                           new XElement("shoes_model", InpShoesModel.Text),
                                                           new XElement("shoes_price", InpShoesPrice.Text,
                                                                        new XAttribute("currency", InpShoesCurrency.Text)
                                                                        ),
                                                           new XElement("shoes_size", InpShoesSize.Text),
                                                           new XElement("shoes_color", InpShoesColor.Text),
                                                           new XElement("shoes_brand", InpShoesBrand.Text)
                                                           )
                                              )
                                 )
                    );

                SportShopEntities context = new SportShopEntities();
                shop_info         sInfo   = new shop_info();
                sInfo.shop_id   = Request.Form["ctl00$body$InpSportShopID"];
                sInfo.shop_name = Request.Form["ctl00$body$InpSportShopName"];
                sInfo.country   = Request.Form["ctl00$body$InpCountry"];
                sInfo.city      = Request.Form["ctl00$body$InpCity"];
                sInfo.address   = Request.Form["ctl00$body$InpAddress"];
                sInfo.phone     = Request.Form["ctl00$body$InpPhNumber"];
                sInfo.email     = Request.Form["ctl00$body$InpEmail"];
                sInfo.site      = Request.Form["ctl00$body$InpSite"];
                context.shop_info.Add(sInfo);

                supplement sup = new supplement();
                sup.supplement_id     = int.Parse(Request.Form["ctl00$body$InpSupplementID"]);
                sup.available_in_shop = Request.Form["ctl00$body$InpSupplementAvailableInShop"];
                sup.type              = Request.Form["ctl00$body$InpSupplementType"];
                sup.purpose           = Request.Form["ctl00$body$InpSupplementPurpose"];
                sup.supplement_brand  = Request.Form["ctl00$body$InpSupplementBrand"];
                sup.supplement_name   = Request.Form["ctl00$body$InpSupplementName"];
                sup.supplement_price  = decimal.Parse(Request.Form["ctl00$body$InpSupplementPrice"]);
                sup.currency          = Request.Form["ctl00$body$InpSupplementCurrency"];
                sup.supplement_rating = int.Parse(Request.Form["ctl00$body$InpSupplementRating"]);
                sup.taste             = Request.Form["ctl00$body$InpTaste"];
                sup.packing           = decimal.Parse(Request.Form["ctl00$body$InpPacking"]);
                sup.measurement_unit  = Request.Form["ctl00$body$InpMeasurementUnit"];
                sup.servings          = int.Parse(Request.Form["ctl00$body$InpServings"]);
                context.supplement.Add(sup);

                boxing_gloves bGloves = new boxing_gloves();
                bGloves.boxing_gloves_id       = int.Parse(Request.Form["ctl00$body$InpBoxingGlovesID"]);
                bGloves.available_in_shop      = Request.Form["ctl00$body$InpBoxingGlovesAvailableInShop"];
                bGloves.boxing_gloves_model    = Request.Form["ctl00$body$InpBoxingGlovesModel"];
                bGloves.boxing_gloves_price    = decimal.Parse(Request.Form["ctl00$body$InpBoxingGlovesPrice"]);
                bGloves.currency               = Request.Form["ctl00$body$InpBoxingGlovesCurrency"];
                bGloves.boxing_gloves_size     = Request.Form["ctl00$body$InpBoxingGlovesSize"];
                bGloves.boxing_gloves_rating   = int.Parse(Request.Form["ctl00$body$InpBoxingGlovesRating"]);
                bGloves.boxing_gloves_material = Request.Form["ctl00$body$InpBoxingGlovesMaterial"];
                bGloves.origin = Request.Form["ctl00$body$InpOrigin"];
                context.boxing_gloves.Add(bGloves);

                sport_shoes sShoes = new sport_shoes();
                sShoes.sport_shoes_id    = int.Parse(Request.Form["ctl00$body$InpSportShoesID"]);
                sShoes.available_in_shop = Request.Form["ctl00$body$InpSportShoesAvailableInShop"];
                sShoes.shoes_model       = Request.Form["ctl00$body$InpShoesModel"];
                sShoes.shoes_price       = decimal.Parse(Request.Form["ctl00$body$InpShoesPrice"]);
                sShoes.currency          = Request.Form["ctl00$body$InpShoesCurrency"];
                sShoes.shoes_size        = int.Parse(Request.Form["ctl00$body$InpShoesSize"]);
                sShoes.shoes_color       = Request.Form["ctl00$body$InpShoesColor"];
                sShoes.shoes_brand       = Request.Form["ctl00$body$InpShoesBrand"];
                context.sport_shoes.Add(sShoes);

                //creating XML
                doc.Save(@"D:\New folder\" + sInfo.shop_name + sInfo.shop_id + ".xml");

                //adding to DB
                context.SaveChanges();

                DivSuccess.Visible = true;
                DivForm.Visible    = false;
            }
        }
Exemple #5
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] files = Directory.GetFiles(Server.MapPath("~/App_Data"), "*.xml", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    filesNumber++;
                    try
                    {
                        isValid = true;
                        XDocument xDoc = XDocument.Load(file);

                        XmlTextReader       r   = new XmlTextReader(file);
                        XmlValidatingReader val = new XmlValidatingReader(r);

                        val.ValidationType          = ValidationType.DTD;
                        val.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);

                        while (val.Read())
                        {
                            // Can add code here to process the content.
                        }
                        val.Close();

                        if (isValid)
                        {
                            List <shop_info> sInfo = xDoc.Descendants("shop_info").Select(d =>
                                                                                          new shop_info
                            {
                                shop_id   = d.Attribute("shop_id").Value,
                                shop_name = d.Element("shop_name").Value,
                                country   = d.Element("country").Value,
                                city      = d.Element("city").Value,
                                address   = d.Element("address").Value,
                                phone     = d.Element("phone").Value,
                                email     = d.Element("email").Value,
                                site      = d.Element("site").Value
                            }).ToList();
                            // Update Data Here
                            using (SportShopEntities dc = new SportShopEntities())
                            {
                                foreach (var i in sInfo)
                                {
                                    var v = dc.shop_info.Where(a => a.shop_id.Equals(i.shop_id)).FirstOrDefault();
                                    if (v != null)
                                    {
                                        v.shop_name = i.shop_name;
                                        v.country   = i.country;
                                        v.city      = i.city;
                                        v.address   = i.address;
                                        v.phone     = i.phone;
                                        v.email     = i.email;
                                        v.site      = i.site;
                                    }
                                    else
                                    {
                                        dc.shop_info.Add(i);
                                    }
                                }

                                dc.SaveChanges();
                            }

                            List <supplement> supp = xDoc.Descendants("supplement").Select(d =>
                                                                                           new supplement
                            {
                                supplement_id     = int.Parse(d.Attribute("supplement_id").Value),
                                type              = d.Attribute("type").Value,
                                purpose           = d.Attribute("purpose").Value,
                                supplement_brand  = d.Attribute("supplement_brand").Value,
                                available_in_shop = d.Attribute("available_in_shop").Value,
                                supplement_name   = d.Element("supplement_name").Value,
                                supplement_price  = Decimal.Parse(d.Element("supplement_price").Value.Replace('.', ',')),
                                //currency = supplement_price.d.Attribute("currency").Value,
                                currency          = "лев",
                                supplement_rating = int.Parse(d.Element("supplement_rating").Value),
                                packing           = Decimal.Parse(d.Element("packing").Value.Replace('.', ',')),
                                taste             = d.Element("taste").Value,
                                //measurement_unit = d.Attribute("measurement_unit").Value,
                                measurement_unit = "кг",
                                //servings = int.Parse(d.Element("servings").Value)
                            }).ToList();

                            // Update Data Here
                            using (SportShopEntities dc = new SportShopEntities())
                            {
                                foreach (var i in supp)
                                {
                                    var v = dc.supplement.Where(a => a.supplement_id.Equals(i.supplement_id)).FirstOrDefault();
                                    if (v != null)
                                    {
                                        v.type              = i.type;
                                        v.purpose           = i.purpose;
                                        v.supplement_brand  = i.supplement_brand;
                                        v.available_in_shop = i.available_in_shop;
                                        v.supplement_name   = i.supplement_name;
                                        v.supplement_price  = i.supplement_price;
                                        v.currency          = i.currency;
                                        v.supplement_rating = i.supplement_rating;
                                        v.packing           = i.packing;
                                        v.taste             = i.taste;
                                        v.measurement_unit  = i.measurement_unit;
                                        v.servings          = i.servings;
                                    }
                                    else
                                    {
                                        dc.supplement.Add(i);
                                    }
                                }

                                dc.SaveChanges();
                            }

                            List <boxing_gloves> bGloves = xDoc.Descendants("boxing_gloves").Select(d =>
                                                                                                    new boxing_gloves
                            {
                                boxing_gloves_id    = int.Parse(d.Attribute("boxing_gloves_id").Value),
                                available_in_shop   = d.Attribute("available_in_shop").Value,
                                boxing_gloves_model = d.Element("boxing_gloves_model").Value,
                                boxing_gloves_price = Decimal.Parse(d.Element("boxing_gloves_price").Value.Replace('.', ',')),
                                //currency = boxing_gloves_price.d.Attribute("currency").Value,
                                currency               = "лев",
                                boxing_gloves_size     = d.Element("boxing_gloves_size").Value,
                                boxing_gloves_rating   = int.Parse(d.Element("boxing_gloves_rating").Value),
                                boxing_gloves_material = d.Element("boxing_gloves_material").Value,
                                origin = d.Element("origin").Value
                            }).ToList();

                            // Update Data Here
                            using (SportShopEntities dc = new SportShopEntities())
                            {
                                foreach (var i in bGloves)
                                {
                                    var v = dc.boxing_gloves.Where(a => a.boxing_gloves_id.Equals(i.boxing_gloves_id)).FirstOrDefault();
                                    if (v != null)
                                    {
                                        v.available_in_shop      = i.available_in_shop;
                                        v.boxing_gloves_model    = i.boxing_gloves_model;
                                        v.boxing_gloves_price    = i.boxing_gloves_price;
                                        v.currency               = i.currency;
                                        v.boxing_gloves_size     = i.boxing_gloves_size;
                                        v.boxing_gloves_rating   = i.boxing_gloves_rating;
                                        v.boxing_gloves_material = i.boxing_gloves_material;
                                        v.origin = i.origin;
                                    }
                                    else
                                    {
                                        dc.boxing_gloves.Add(i);
                                    }
                                }

                                dc.SaveChanges();
                            }

                            List <sport_shoes> spShoes = xDoc.Descendants("sport_shoes").Select(d =>
                                                                                                new sport_shoes
                            {
                                sport_shoes_id    = int.Parse(d.Attribute("sport_shoes_id").Value),
                                available_in_shop = d.Attribute("available_in_shop").Value,
                                shoes_model       = d.Element("shoes_model").Value,
                                shoes_price       = Decimal.Parse(d.Element("shoes_price").Value.Replace('.', ',')),
                                //currency = boxing_gloves_price.d.Attribute("currency").Value,
                                currency    = "лев",
                                shoes_size  = int.Parse(d.Element("shoes_size").Value),
                                shoes_color = d.Element("shoes_color").Value,
                                shoes_brand = d.Element("shoes_brand").Value,
                            }).ToList();

                            // Update Data Here
                            using (SportShopEntities dc = new SportShopEntities())
                            {
                                foreach (var i in spShoes)
                                {
                                    var v = dc.sport_shoes.Where(a => a.sport_shoes_id.Equals(i.sport_shoes_id)).FirstOrDefault();
                                    if (v != null)
                                    {
                                        v.available_in_shop = i.available_in_shop;
                                        v.shoes_model       = i.shoes_model;
                                        v.shoes_price       = i.shoes_price;
                                        v.currency          = i.currency;
                                        v.shoes_size        = i.shoes_size;
                                        v.shoes_color       = i.shoes_color;
                                        v.shoes_brand       = i.shoes_brand;
                                    }
                                    else
                                    {
                                        dc.sport_shoes.Add(i);
                                    }
                                }

                                dc.SaveChanges();
                            }
                            if (filesNumber < 10)
                            {
                                lblSuccessMessage.Text = lblSuccessMessage.Text + "sport_shop-0" + filesNumber + " е валиден спрямо sport_shop.dtd и успешно е записан в базата данни <br />";
                            }
                            else
                            {
                                lblSuccessMessage.Text = lblSuccessMessage.Text + "sport_shop-" + filesNumber + " е валиден спрямо sport_shop.dtd и успешно е записан в базата данни <br />";
                            }
                        }
                        else
                        {
                            if (filesNumber < 10)
                            {
                                lblErrorMessage.Text = lblErrorMessage.Text + "sport_shop-0" + filesNumber + " не е валиден спрямо sport_shop.dtd и не е записан в базата данни <br />";
                            }
                            else
                            {
                                lblErrorMessage.Text = lblErrorMessage.Text + "sport_shop-" + filesNumber + " не е валиден спрямо sport_shop.dtd и не е записан в базата данни <br />";
                            }
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }