Exemplo n.º 1
0
        private void btnSavePicture_Click(object sender, EventArgs e)
        {
            //Store picture as an array of bytes. To do this, create a fileStream object and a binReader obj.
            //Set fileStream to target image in picbox. Then read bytes into array with binReader.

            //After array is successfully filled, gather other information about product.
            //Fill product object with information and then attempt to insert into database.
            try
            {

                //Declare product object
                Product aProduct = new Product();

                //Declare array of bytes
                byte[] pic = null;

                //Declare file reading objects
                FileStream fileStream = new FileStream(fileLocation, FileMode.Open, FileAccess.Read);
                BinaryReader binReader = new BinaryReader(fileStream);

                //Read file with binary reader into pic array
                pic = binReader.ReadBytes((int)fileStream.Length);

                //Read input from form to object as well as pic array
                aProduct.ProductName = txtProductName.Text;
                aProduct.ProductDescription = txtProductDescription.Text;
                aProduct.Price = (float)Convert.ToDouble(txtPrice.Text);
                aProduct.ProductPicture = pic;

                command = new SqlCommand(commandStatement, connection);

                //Add params to command object
                command.Parameters.AddWithValue("@ProductName", aProduct.ProductName);
                command.Parameters.AddWithValue("@ProductDescription", aProduct.ProductDescription);
                command.Parameters.AddWithValue("@Price", aProduct.Price);
                command.Parameters.AddWithValue("@ProductPicture", aProduct.ProductPicture);

                //Open connection
                connection.Open();

                //Attempt insert into database
                command.ExecuteNonQuery();

                //Close connection
                connection.Close();

                MessageBox.Show("New product successfully saved to database");

            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save product to database! " + ex.Message);
                connection.Close();
            }
        }
Exemplo n.º 2
0
        private void getAssignedProducts()
        {
            try
            {
                //Gather form input
                int PersonID = Convert.ToInt32(txtPersonID.Text);

                //Select statement selecting only the products associated with the person's assigned gifts
                String commandStatement = "SELECT  Products.ProductName, Products.Price " +
                                    "FROM   AssignedGifts INNER JOIN " +
                                    "Products ON AssignedGifts.ProductID = Products.ProductID " +
                                    "WHERE  (AssignedGifts.PersonID = @PersonID)";

                //Add parameters
                SqlCommand command = new SqlCommand(commandStatement, connection);
                command.Parameters.AddWithValue("@PersonID", PersonID);

                //Read database
                connection.Open();
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    Product aProd = new Product();

                    aProd.ProductName = reader["ProductName"].ToString();
                    aProd.Price = (float)Convert.ToDouble(reader["Price"]);

                    //Put the person's gifts into the datagrid and add their prices to the list
                    curUserPrices.Add(aProd.Price);
                    dataGridAssignedProducts.Rows.Add(aProd.ProductName, aProd.Price.ToString("c"));
                }

                connection.Close();
            }

            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }