示例#1
0
        private void btnGetProduct_Click(object sender, EventArgs e)
        {
            var st = new Stopwatch();

            st.Start();
            lsProducts.Items.Clear();
            lsProducts.Items.Add("Working");
            BaseURL = txtURLSuppliers.Text + "/api/";
            apiURL  = "ProductObject/" + numProductID.Value.ToString();
            URL     = BaseURL + apiURL;
            Product product = new Product();

            try
            {
                for (int i = 0; i < numericUpDown1.Value; i++)
                {
                    string result = REST.HttpGet(URL); //should get one Supplier
                    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Product));
                    byte[]       byteArray        = Encoding.UTF8.GetBytes(result);
                    MemoryStream ms = new MemoryStream(byteArray);
                    product = (Product)js.ReadObject(ms);
                }

                //string result = REST.HttpGet(URL); //should get one Supplier
                //DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Product));
                //byte[] byteArray = Encoding.UTF8.GetBytes(result);
                //MemoryStream ms = new MemoryStream(byteArray);
                //Product product = (Product)js.ReadObject(ms);

                //Displaying the time
                long milliseconds = st.ElapsedMilliseconds;
                lsProducts.Items.Add($"Load testing : {numericUpDown1.Value} Time to retrieve the information: {milliseconds} milliseconds");

                txtDataOut.Text      = product.ProductID.ToString();
                txtProductName.Text  = product.ProductName.ToString();
                txtSupplierID.Text   = product.SupplierID.ToString();
                txtCategoryId.Text   = product.CategoryID.ToString();
                txtQuantity.Text     = product.QuantityPerUnit.ToString();
                txtUnitPrice.Text    = product.UnitPrice.ToString();
                txtUnitInStock.Text  = product.UnitsInStock.ToString();
                txtUnitOnOrder.Text  = product.UnitsOnOrder.ToString();
                txtReorderLevel.Text = product.ReorderLevel.ToString();
                txtDiscontinued.Text = product.Discontinued.ToString();

                //lsProducts.Items.Clear();
                //lsProducts.Items.Add($"Retrieved Product = {P.ProductID} {P.ProductName} {P.CategoryID} {P.SupplierID} {P.QuantityPerUnit}");
                //MessageBox.Show(result);
            }

            catch (System.Net.WebException ex)
            {
                //can i differentiate between a web exception per se
                //vs a Supplierid not found in the database
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
示例#2
0
        private void button1_Click(object sender, EventArgs e)
        {
            var st = new Stopwatch();

            st.Start();
            lsProducts.Items.Clear();
            lsProducts.Items.Add("Working");
            BaseURL = txtURLSuppliers.Text + "/api/";
            apiURL  = "Products";
            URL     = BaseURL + apiURL;
            try
            {
                for (int i = 0; i < numericUpDown1.Value; i++)
                {
                    string result = REST.HttpGet(URL); //should get one Supplier
                    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(List <Product>));
                    byte[]       byteArray        = Encoding.UTF8.GetBytes(result);
                    MemoryStream ms = new MemoryStream(byteArray);
                    productList = (List <Product>)js.ReadObject(ms);
                }

                //string result = REST.HttpGet(URL); //should get one Supplier
                //DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(List<Product>));
                //byte[] byteArray = Encoding.UTF8.GetBytes(result);
                //MemoryStream ms = new MemoryStream(byteArray);
                //productList = (List<Product>)js.ReadObject(ms);

                long milliseconds = st.ElapsedMilliseconds;
                lsProducts.Items.Add($"Load testing : {numericUpDown1.Value} Time to retrieve the information: {milliseconds} milliseconds");
                lsProducts.Items.Add($"");
                foreach (Product tmp in productList)
                {
                    lsProducts.Items.Add($"Product ID= {tmp.ProductID} Product Name: {tmp.ProductName}");
                }


                //MessageBox.Show(result);
            }

            catch (System.Net.WebException ex)
            {
                //can i differentiate between a web exception per se
                //vs a Supplierid not found in the database
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }