jeudi 4 février 2016

SQL - IF statement query

I'm having a bit of a blonde moment this afternoon. When a user uploads an image, I have a list of conditions it has to met (Image Size, Format etc.)

What I would like it to do is show the error and NOT enter the record into the database. At the moment it is showing the error and still entering it. I know it is something simple to do, but have had a complete code block!

Any help gratefully appreciated (Code below)

Cheers! Jack!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CameraWarehouseWebsite.BusinessLayer;

namespace CameraWarehouseWebsite.Admin
{
    public partial class AddNewProducts : System.Web.UI.Page
    {
        //Displays all the product catergories currently stored in the database on page load
        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetCategories();

        }
    }
    //This method is used to "get" all of categories listed in the database
    private void GetCategories()
    {
        ShoppingCart k = new ShoppingCart();
        DataTable dt = k.GetCategories();
        if (dt.Rows.Count > 0)
        {
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataSource = dt;
            ddlCategory.DataBind();
        }
    }

      protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (UploadProductPhoto.PostedFile != null)
        {
            SaveProductPhoto();

            ShoppingCart k = new ShoppingCart()
            {
                CategoryID = Convert.ToInt32(ddlCategory.SelectedValue).ToString(),
                ProductName = txtProductName.Text,
                ProductDescription = txtProductDescription.Text,
                ProductPrice = txtProductPrice.Text,
                ProductImageUrl = "~/ProductImages/" + UploadProductPhoto.FileName,
            };
            k.AddNewProduct();
            ClearText();
        }
    }

    private void ClearText()
    {
        UploadProductPhoto = null;
        txtProductName.Text = string.Empty;
        txtProductPrice.Text = string.Empty;
        txtProductDescription.Text = string.Empty;
    }

    private void SaveProductPhoto()
    {
        if (UploadProductPhoto.PostedFile != null)
        {
            string filename = UploadProductPhoto.PostedFile.FileName.ToString();
            string filextension = System.IO.Path.GetExtension(UploadProductPhoto.FileName);

            if (filename.Length > 96)
            {
                Response.Write("Image name should not exceed 96 characters");
            }
            else if (filextension != ".jpeg" && filextension != ".jpg" && filextension !=".png")
            {
                Response.Write("Invalid file format, please resave as .jpeg, .jpg or .png and try again");
            }
            else if (UploadProductPhoto.PostedFile.ContentLength > 6000000)
            {
                Response.Write("The image size should not be greater than 6MB. Make image smaller and try again");
            }
            else
            {
                UploadProductPhoto.SaveAs(Server.MapPath("~/ProductImages/" + filename));
            }
        }
    }

}
}

Aucun commentaire:

Enregistrer un commentaire