public Dbcart(int _idcart)
        {
            this.Idcart = _idcart;
            SqlConnection cnn = new SqlConnection(HttpContext.Current.Application["strCnn"].ToString());
            cnn.Open();

            string sql;

            sql = "select * from tcart where id=@id";

            DataTable dtcart = simplestecommerce.helpDb.getDataTableByOpenCnn(cnn,sql,new SqlParameter("id", _idcart));
            
            if ( dtcart.Rows.Count<1) {
                cnn.Close();
                simplestecommerce.problema.redirect("no order with this ID in database");
            }

            DataRow drcart = dtcart.Rows[0];

            this.Idcarrier = (int)drcart["idcarrier"];
            this.Status = (int)drcart["idorderstatus"];
            this.Data = DateTime.Parse(drcart["data"].ToString());
            this.Promemoria = drcart["promemoria"].ToString();
            this.Dbuser.Id = drcart["idloggeduser"].ToString();
            this.Note = drcart["note"].ToString();

            if (drcart["idloggeduser"] == System.DBNull.Value)
            {

                this.Dbuser.Address = drcart["guestaddress"].ToString();
                this.Dbuser.City = drcart["guestcity"].ToString();
                this.Dbuser.Email = drcart["guestemail"].ToString();
                this.Dbuser.Firstname = drcart["guestfirstname"].ToString();
                this.Dbuser.Fiscalcode = drcart["guestfiscalcode"].ToString();
                this.Dbuser.Idregion = (int)drcart["guestidregion"];
                this.Dbuser.Nameoffirm = drcart["guestnameoffirm"].ToString();
                this.Dbuser.Postalcode = drcart["guestpostalcode"].ToString();
                this.Dbuser.Secondname = drcart["guestsecondname"].ToString();
                this.Dbuser.Subject = (int)drcart["guestsubject"];
                this.Dbuser.Telephone = drcart["guesttelephone"].ToString();
                this.Dbuser.Vatnumber = drcart["guestvatnumber"].ToString();


            }
            else
            {
                DataTable dtloggeduser = simplestecommerce.helpDb.getDataTableByOpenCnn(
                    cnn,
                    "select * from tutenti where ut_id=@idloggeduser",
                    new SqlParameter("idloggeduser", (string)drcart["idloggeduser"])
                    );

                if (dtloggeduser.Rows.Count < 1)
                {
                    cnn.Close();
                    simplestecommerce.problema.redirect("user with ID " + simplestecommerce.sicurezza.xss.getreplacedencoded((string)drcart["idloggeduser"]) + " no longer exists in db");
                }

                DataRow drloggeduser = dtloggeduser.Rows[0];

                this.Dbuser.Address = drloggeduser["ut_address"].ToString();
                this.Dbuser.City = drloggeduser["ut_city"].ToString();
                this.Dbuser.Email = drloggeduser["ut_email"].ToString();
                this.Dbuser.Firstname = drloggeduser["ut_firstname"].ToString();
                this.Dbuser.Fiscalcode = drloggeduser["ut_fiscalcode"].ToString();
                this.Dbuser.Idregion = (int)drloggeduser["ut_idregion"];
                this.Dbuser.Nameoffirm = drloggeduser["ut_nameoffirm"].ToString();
                this.Dbuser.Postalcode = drloggeduser["ut_postalcode"].ToString();
                this.Dbuser.Secondname = drloggeduser["ut_secondname"].ToString();
                this.Dbuser.Subject = (int)drloggeduser["ut_subject"];
                this.Dbuser.Telephone = drloggeduser["ut_telephone"].ToString();
                this.Dbuser.Vatnumber = drloggeduser["ut_vatnumber"].ToString();


            }


            //shipping data
            this.Dbuser.Spaddress = drcart["spaddress"].ToString();
            this.Dbuser.Spcity = drcart["spcity"].ToString();
            this.Dbuser.Spfirstname = drcart["spfirstname"].ToString();
            this.Dbuser.Spidregion = (int)drcart["spidregion"];
            this.Dbuser.Sppostalcode = drcart["sppostalcode"].ToString();
            this.Dbuser.Spsecondname = drcart["spsecondname"].ToString();

            this.Subtotal = (double)drcart["subtotal"];
            this.Coupononsubtotal = (double)drcart["coupononsubtotal"];
            this.Tax = (double)drcart["tax"];
            this.Shippingcost = (double)drcart["shippingcost"];
            this.Taxonshippingcost = (double)drcart["taxonshippingcost"];
            this.Couponaftertax = (double)drcart["couponaftertaxes"];
            this.Tot = (double)drcart["tot"];
            this.Taxtype = (int)drcart["taxtype"];
            this.Userlanguage = drcart["userlanguage"].ToString();


            // cartitems

            DataTable dtcartitem = simplestecommerce.helpDb.getDataTableByOpenCnn(
                cnn,
                "select * from tcartitem where idcart=@idcart",
                new SqlParameter ("idcart", this.Idcart)
                );

            if (dtcartitem.Rows.Count<1) {
                cnn.Close();
                simplestecommerce.problema.redirect ("no cartitems in db with current idcart");
            }
            
            foreach ( DataRow rowcartitem in dtcartitem.Rows) {

                DataTable dtcartvariation = simplestecommerce.helpDb.getDataTableByOpenCnn(
                cnn,
                "select * from tcartvariation where idcartitem=@idcartitem",
                new SqlParameter ("idcartitem", (int)rowcartitem["id"])
                );


                List<Dbcartvariation> workcartvariations = new List<Dbcartvariation>();

                foreach ( DataRow rowcartvariation in dtcartvariation.Rows) {

                    Dbcartvariation workcartvariation = new Dbcartvariation(rowcartvariation["strvariation"].ToString(), rowcartvariation["stroption"].ToString());
                    workcartvariations.Add (workcartvariation);

                }
            
                lista.Add ( new simplestecommerce.Dbcartitem (
                rowcartitem["name"].ToString(),
                rowcartitem["code"].ToString(),
                (double)rowcartitem["finalprice"],
                (int)rowcartitem["quantity"],
                (double)rowcartitem["totaldiscount"],
                (string)rowcartitem["preview"],
                workcartvariations
                ));
                 
                
                
            
            
            }
            
            
            
            
            cnn.Close();


        }
        public Dbcart(int _idcart)
        {
            this.Idcart = _idcart;
            SqlConnection cnn = new SqlConnection(HttpContext.Current.Application["strCnn"].ToString());

            cnn.Open();

            string sql;

            sql = "select * from tcart where id=@id";

            DataTable dtcart = simplestecommerce.helpDb.getDataTableByOpenCnn(cnn, sql, new SqlParameter("id", _idcart));

            if (dtcart.Rows.Count < 1)
            {
                cnn.Close();
                simplestecommerce.problema.redirect("no order with this ID in database");
            }

            DataRow drcart = dtcart.Rows[0];

            this.Idcarrier  = (int)drcart["idcarrier"];
            this.Status     = (int)drcart["idorderstatus"];
            this.Data       = DateTime.Parse(drcart["data"].ToString());
            this.Promemoria = drcart["promemoria"].ToString();
            this.Dbuser.Id  = drcart["idloggeduser"].ToString();
            this.Note       = drcart["note"].ToString();

            if (drcart["idloggeduser"] == System.DBNull.Value)
            {
                this.Dbuser.Address    = drcart["guestaddress"].ToString();
                this.Dbuser.City       = drcart["guestcity"].ToString();
                this.Dbuser.Email      = drcart["guestemail"].ToString();
                this.Dbuser.Firstname  = drcart["guestfirstname"].ToString();
                this.Dbuser.Fiscalcode = drcart["guestfiscalcode"].ToString();
                this.Dbuser.Idregion   = (int)drcart["guestidregion"];
                this.Dbuser.Nameoffirm = drcart["guestnameoffirm"].ToString();
                this.Dbuser.Postalcode = drcart["guestpostalcode"].ToString();
                this.Dbuser.Secondname = drcart["guestsecondname"].ToString();
                this.Dbuser.Subject    = (int)drcart["guestsubject"];
                this.Dbuser.Telephone  = drcart["guesttelephone"].ToString();
                this.Dbuser.Vatnumber  = drcart["guestvatnumber"].ToString();
            }
            else
            {
                DataTable dtloggeduser = simplestecommerce.helpDb.getDataTableByOpenCnn(
                    cnn,
                    "select * from tutenti where ut_id=@idloggeduser",
                    new SqlParameter("idloggeduser", (string)drcart["idloggeduser"])
                    );

                if (dtloggeduser.Rows.Count < 1)
                {
                    cnn.Close();
                    simplestecommerce.problema.redirect("user with ID " + simplestecommerce.sicurezza.xss.getreplacedencoded((string)drcart["idloggeduser"]) + " no longer exists in db");
                }

                DataRow drloggeduser = dtloggeduser.Rows[0];

                this.Dbuser.Address    = drloggeduser["ut_address"].ToString();
                this.Dbuser.City       = drloggeduser["ut_city"].ToString();
                this.Dbuser.Email      = drloggeduser["ut_email"].ToString();
                this.Dbuser.Firstname  = drloggeduser["ut_firstname"].ToString();
                this.Dbuser.Fiscalcode = drloggeduser["ut_fiscalcode"].ToString();
                this.Dbuser.Idregion   = (int)drloggeduser["ut_idregion"];
                this.Dbuser.Nameoffirm = drloggeduser["ut_nameoffirm"].ToString();
                this.Dbuser.Postalcode = drloggeduser["ut_postalcode"].ToString();
                this.Dbuser.Secondname = drloggeduser["ut_secondname"].ToString();
                this.Dbuser.Subject    = (int)drloggeduser["ut_subject"];
                this.Dbuser.Telephone  = drloggeduser["ut_telephone"].ToString();
                this.Dbuser.Vatnumber  = drloggeduser["ut_vatnumber"].ToString();
            }


            //shipping data
            this.Dbuser.Spaddress    = drcart["spaddress"].ToString();
            this.Dbuser.Spcity       = drcart["spcity"].ToString();
            this.Dbuser.Spfirstname  = drcart["spfirstname"].ToString();
            this.Dbuser.Spidregion   = (int)drcart["spidregion"];
            this.Dbuser.Sppostalcode = drcart["sppostalcode"].ToString();
            this.Dbuser.Spsecondname = drcart["spsecondname"].ToString();

            this.Subtotal         = (double)drcart["subtotal"];
            this.Coupononsubtotal = (double)drcart["coupononsubtotal"];
            this.Tax               = (double)drcart["tax"];
            this.Shippingcost      = (double)drcart["shippingcost"];
            this.Taxonshippingcost = (double)drcart["taxonshippingcost"];
            this.Couponaftertax    = (double)drcart["couponaftertaxes"];
            this.Tot               = (double)drcart["tot"];
            this.Taxtype           = (int)drcart["taxtype"];
            this.Userlanguage      = drcart["userlanguage"].ToString();


            // cartitems

            DataTable dtcartitem = simplestecommerce.helpDb.getDataTableByOpenCnn(
                cnn,
                "select * from tcartitem where idcart=@idcart",
                new SqlParameter("idcart", this.Idcart)
                );

            if (dtcartitem.Rows.Count < 1)
            {
                cnn.Close();
                simplestecommerce.problema.redirect("no cartitems in db with current idcart");
            }

            foreach (DataRow rowcartitem in dtcartitem.Rows)
            {
                DataTable dtcartvariation = simplestecommerce.helpDb.getDataTableByOpenCnn(
                    cnn,
                    "select * from tcartvariation where idcartitem=@idcartitem",
                    new SqlParameter("idcartitem", (int)rowcartitem["id"])
                    );


                List <Dbcartvariation> workcartvariations = new List <Dbcartvariation>();

                foreach (DataRow rowcartvariation in dtcartvariation.Rows)
                {
                    Dbcartvariation workcartvariation = new Dbcartvariation(rowcartvariation["strvariation"].ToString(), rowcartvariation["stroption"].ToString());
                    workcartvariations.Add(workcartvariation);
                }

                lista.Add(new simplestecommerce.Dbcartitem(
                              rowcartitem["name"].ToString(),
                              rowcartitem["code"].ToString(),
                              (double)rowcartitem["finalprice"],
                              (int)rowcartitem["quantity"],
                              (double)rowcartitem["totaldiscount"],
                              (string)rowcartitem["preview"],
                              workcartvariations
                              ));
            }



            cnn.Close();
        }