protected void btnUpload_Click(object sender, EventArgs e)
    {
        productManager objProduct = new productManager();
        ////insert image product
        HttpFileCollection uploadedFiles = Request.Files;

        for (int im = 0; im < uploadedFiles.Count; im++)
        {
            HttpPostedFile userPostedFile = uploadedFiles[im];
            string         name           = userPostedFile.FileName;
            if (name != "null" && name != "")
            {
                string extension = Path.GetExtension(userPostedFile.FileName).ToString();
                string name2     = name.Replace(extension, "");
                string nameBox   = Request.Form["newr" + name2];
                if (nameBox == null)
                {
                    objProduct.productId = 0;
                    objProduct.isactive  = 1;
                    objProduct.InsertProductImageItem();

                    int prodimageID = objProduct.GetmaximageProductId();
                    objProduct.productImagesId = prodimageID;
                    objProduct.actualImageName = userPostedFile.FileName;
                    objProduct.imageName       = UploadImage(userPostedFile, prodimageID);
                    objProduct.imgLabel        = objProduct.imageName;
                    objProduct.UpdateImage();
                }
                else
                {
                }
            }
        }
        BindProduct();
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            productManager objProduct = new productManager();
            ////insert image product
            HttpFileCollection uploadedFiles = Request.Files;
            bool   flg = false;
            string strImagesNameExist = string.Empty;
            string FileNameCount      = string.Empty;

            for (int im = 0; im < uploadedFiles.Count; im++)
            {
                HttpPostedFile userPostedFile = uploadedFiles[im];
                string         name           = userPostedFile.FileName;
                if (name != "null" && name != "")
                {
                    bool   imgFlag   = false;
                    string extension = Path.GetExtension(userPostedFile.FileName).ToString();
                    string name2     = name.Replace(extension, "");
                    string nameBox   = Request.Form["newr" + name2];
                    if (extension.ToLower() == ".png" || extension.ToLower() == ".jpg")
                    {
                        if (nameBox == null)
                        {
                            #region check SKU and ImageName

                            string NameFile = name2;
                            int    prodimageID;

                            string flname = name2.Substring(name2.Length - 1);
                            if (!CommonFunctions.IsValidValue(flname, true, false, false))
                            {
                                name2 = name2.TrimEnd(name2[name2.Length - 1]);
                            }
                            objProduct.sku = name2;
                            int prodId = objProduct.GetSkuCount();
                            int imgcnt = objProduct.GetproductsImageCount(prodId);

                            // for check images exist or not
                            DataTable dtImgname = new DataTable();
                            dtImgname = objProduct.GetProductImageNameByProductId(prodId);  // get the images from productid
                            if (dtImgname.Rows.Count > 0)
                            {
                                for (int ie = 0; ie < dtImgname.Rows.Count; ie++)
                                {
                                    if (Convert.ToString(dtImgname.Rows[ie]["imageName"]) == Convert.ToString(NameFile + ".jpg"))
                                    {
                                        if (strImagesNameExist == "")
                                        {
                                            strImagesNameExist += dtImgname.Rows[ie]["imageName"].ToString();
                                        }
                                        else
                                        {
                                            strImagesNameExist += "," + dtImgname.Rows[ie]["imageName"].ToString();
                                        }
                                        imgFlag = true;
                                        break;
                                    }

                                    // added on 20_12_2016
                                    if (Convert.ToString(dtImgname.Rows[ie]["imageName"]) == Convert.ToString(NameFile + ".png"))
                                    {
                                        if (strImagesNameExist == "")
                                        {
                                            strImagesNameExist += dtImgname.Rows[ie]["imageName"].ToString();
                                        }
                                        else
                                        {
                                            strImagesNameExist += "," + dtImgname.Rows[ie]["imageName"].ToString();
                                        }
                                        imgFlag = true;
                                        break;
                                    }
                                }
                            }

                            if (imgFlag == false)
                            {
                                if (strImagesNameExist == "")
                                {
                                    lblmsg.Visible = false;
                                }

                                objProduct.productId = 0;
                                objProduct.isactive  = 1;
                                //objProduct.imgLabel = NameFile;
                                objProduct.InsertProductImageItem();

                                if (prodId != 0 && imgcnt != 0 && flg == false)                         // if product id and image count both are in the table
                                {
                                    if (imgFlag == false)                                               // if false then updates record other wise display message
                                    {
                                        prodimageID                = objProduct.GetmaximageProductId(); // gat max productimageid
                                        objProduct.productId       = prodId;
                                        objProduct.productImagesId = prodimageID;
                                        objProduct.actualImageName = userPostedFile.FileName;
                                        objProduct.imageName       = UploadImageSKU(userPostedFile, name2, true);
                                        objProduct.isactive        = 1;
                                        objProduct.sortOrder       = 0;
                                        objProduct.imgLabel        = objProduct.imageName;
                                        objProduct.UpdateProductImagetem();
                                    }
                                    else
                                    {
                                        lblmsg.Visible = true;
                                        lblmsgs.Text   = strImagesNameExist + "Images are already exist.";
                                    }
                                }
                                else if (prodId != 0)  // no images avaliable in product images table
                                {
                                    flg                        = true;
                                    prodimageID                = objProduct.GetmaximageProductId(); // gat max productimageid
                                    objProduct.productId       = prodId;
                                    objProduct.productImagesId = prodimageID;
                                    objProduct.actualImageName = userPostedFile.FileName;
                                    objProduct.imageName       = UploadImageSKU(userPostedFile, NameFile, false);
                                    objProduct.isactive        = 1;
                                    objProduct.sortOrder       = 0;
                                    string pr_name = objProduct.GetProductNameByProductid();
                                    objProduct.imgLabel = Server.HtmlEncode(pr_name);
                                    objProduct.UpdateProductImagetem();
                                }
                                else
                                {
                                    prodimageID = objProduct.GetmaximageProductId();   // gat max productimageid
                                    objProduct.productImagesId = prodimageID;
                                    objProduct.actualImageName = userPostedFile.FileName;
                                    objProduct.imageName       = UploadImage(userPostedFile, prodimageID.ToString());
                                    objProduct.imgLabel        = objProduct.imageName;
                                    objProduct.UpdateImage();
                                }
                            }
                            else
                            {
                                //lblmsg.Visible = true;
                                //lblmsgs.Text = strImagesNameExist + "  Images are already exist.";

                                string[] FileMaxId = name.Split('.');
                                string   filesName = UploadImage(userPostedFile, FileMaxId[0].ToString());

                                if (FileNameCount == "")
                                {
                                    FileNameCount = filesName;
                                }
                                else
                                {
                                    FileNameCount += "," + filesName;
                                }

                                lblmsg.Visible = true;
                                lblmsgs.Text   = "image " + FileNameCount + " updates successfully.";
                            }

                            #endregion

                            //int prodimageID = objProduct.GetmaximageProductId();
                            //objProduct.productImagesId = prodimageID;
                            //objProduct.actualImageName = userPostedFile.FileName;
                            ////objProduct.imageName = UploadImage(userPostedFile, prodimageID);
                            ////objProduct.imgLabel = objProduct.imageName;
                            ////objProduct.UpdateImage();
                        }
                        else
                        {
                        }
                    }
                }
            }
            BindProduct();
        }
    }