private void updateHash()
 {
     using (var ctx = new DiscountDBContext())
     {
         string contentForHash = calcStringForHash();
         ctx.HashTs.DeleteObject(ctx.HashTs.First());
         HashT hash = new HashT { Hash = calcMD5(contentForHash) };
         ctx.HashTs.AddObject(hash);
         Version vers = new Version { VersionOfCatalog = ctx.Versions.First().VersionOfCatalog + 0.01 };
         ctx.Versions.DeleteObject(ctx.Versions.First());
         ctx.Versions.AddObject(vers);
         ctx.SaveChanges();
     }
 }
        private void parseSite()
        {
            HttpWebRequest req;
            HttpWebResponse resp;
            StreamReader sr;
            string allContent;
            req = (HttpWebRequest)WebRequest.Create("http://tomall.ru/allmarket/okey/");
            resp = (HttpWebResponse)req.GetResponse();
            sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("windows-1251"));
            allContent = sr.ReadToEnd();
            sr.Close();
            cleanTableProducts();

            using (var ctx = new DiscountDBContext())
            {
                for (int i = 10; i < 30; i++)
                {
                    int startInd = allContent.IndexOf("all" + i.ToString()) + 7;
                    int endInd = allContent.IndexOf("];", startInd);
                    string content = allContent.Remove(endInd).Remove(0, startInd);
                    //textBox1.Text = content;
                    List<string> productsList = new List<string>();
                    for (int index = 0; index < content.Length - 1; )
                    {

                        int startInd_ = content.IndexOf("[", index) + 1;
                        int endInd_ = content.IndexOf("]", startInd_);
                        //textBox1.Text += startInd_.ToString() + " " + endInd_.ToString() + " ";
                        index = endInd_;
                        string oneLine = content.Remove(endInd_).Remove(0, startInd_);
                        productsList.Add(oneLine);
                    }
                    foreach (string line in productsList)
                    {
                        int index = 0;
                        int startInd_ = 0;
                        int endInd_ = line.IndexOf(",", index);
                        string id = line.Remove(endInd_);
                        /*
                        var r = from c in ctx.Products
                                where c.ProductID == id
                                select c;
                        if (r.Count() > 0)
                            continue;
                        */
                        index = endInd_;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string productName = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf(",", index) + 2;
                        endInd_ = line.IndexOf(",", startInd_);
                        string oldPrice = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string newPrice = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf(",", index) + 2;
                        endInd_ = line.IndexOf(",", startInd_);
                        string discount = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string storeName = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf(",", index) + 2;
                        endInd_ = line.IndexOf(",", startInd_);
                        string storeID = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string startDate = line.Remove(endInd_).Remove(0, startInd_);

                        index = endInd_ + 1;
                        startInd_ = line.IndexOf("'", index) + 1;
                        endInd_ = line.IndexOf("'", startInd_);
                        string endDate = line.Remove(endInd_).Remove(0, startInd_);

                        string imageURLFolder = Convert.ToInt32(id.Remove(3)).ToString();
                        string imageURLFile = Convert.ToInt32(id.Remove(0, 3)).ToString();
                        string imageURL = @"http://tomall.ru/allmarket/foto/" + imageURLFolder + @"/" + imageURLFile + ".jpg";

                        Product p = new Product
                        {
                            ProductID = id,
                            StoreID = Convert.ToInt32(storeID),
                            ProductName = productName,
                            ProductsTypeID = i,
                            Discount = Convert.ToInt32(discount),
                            OldPrice = oldPrice,
                            NewPrice = newPrice,
                            StartDate = startDate,
                            EndDate = endDate,
                            ImageURL = imageURL
                        };

                        ctx.Products.AddObject(p);
                        ctx.SaveChanges();
                    }
                }
                updateHash();
            }
        }