Пример #1
0
        protected void AddProductBtnClick(object sender, EventArgs e)
        {
            string productId     = DropDownListProduct.SelectedValue;
            string newSqlCommand = "SELECT [Name], [Amount], [Calories], [Sugar], [Carbs] FROM [Product] WHERE Id = " + productId;
            //SqlDataSource3.SelectCommand = newSqlCommand;
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionStrin‌​g);

            con.Open();

            SqlCommand findOneProductByIdCmd = con.CreateCommand();

            findOneProductByIdCmd.CommandText = newSqlCommand;
            findOneProductByIdCmd.CommandType = CommandType.Text;

            SqlDataReader reader = findOneProductByIdCmd.ExecuteReader(CommandBehavior.KeyInfo);

            reader.Read();

            Product newProduct = new Product();

            newProduct.Id   = long.Parse(productId);
            newProduct.Name = reader.GetString(0);

            newProduct.Calories = reader.GetDecimal(2);
            newProduct.Sugar    = reader.GetDecimal(3);
            newProduct.Carbs    = reader.GetDecimal(4);

            int newAmount = Int32.Parse(DropDownListAmount.SelectedValue);

            newProduct.Amount = newAmount;

            newProduct.CalculateTotal();

            if (ViewState["ProductsList"] != null)
            {
                ProductsList = (List <Product>)ViewState["ProductsList"];
            }

            ProductsList.Add(newProduct);

            ProductTableGV.DataSource = ProductsList;
            ProductTableGV.DataBind();

            reader.Close();
            con.Close();

            ViewState["ProductsList"] = ProductsList;
        }
Пример #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Calendar1.Visible         = false;
                ProductTableGV.DataSource = ProductsList;
                ProductTableGV.DataBind();
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionStrin‌​g);
                con.Open();
                string     selectFirstCategoryIdCommand = "select TOP 1 Id from [myDb].[dbo].[Category]";
                SqlCommand com             = new SqlCommand(selectFirstCategoryIdCommand, con);
                int        firstCategoryId = Convert.ToInt32(com.ExecuteScalar().ToString());
                con.Close();
                string newSqlCommand = "SELECT * FROM [Product] WHERE CategoryId = " + firstCategoryId;
                SqlDataSource2.SelectCommand = newSqlCommand;

                LabelProductName.Visible     = false;
                LabelProductCalories.Visible = false;
            }
        }