Example #1
0
    //Add & Update the Item
    public void InsertItem()
    {
        bool   fileSaved   = false;
        string getCategory = "";

        for (int k = 0; k < chkLstCategory.Items.Count; k++)     //Get all the category to which item will be inserted
        {
            if (chkLstCategory.Items[k].Selected == true)
            {
                getCategory += Convert.ToString(chkLstCategory.Items[k].Value) + ",";
            }
        }
        if (cmbCity.SelectedValue == "")
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "msg", "alert('Select City')", true);
        }
        else if (getCategory == "")
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "msg", "alert('Select Category')", true);
        }
        else
        {
            try
            {
                itemBLLObj.itemName        = txtItemName.Text;
                itemBLLObj.itemDescription = txtItemDescription.Text;
                itemBLLObj.cityId          = Convert.ToInt32(Convert.ToString(cmbCity.SelectedValue));
                itemBLLObj.itemActive      = 1;


                //for (int k = 0; k < chkLstCategory.Items.Count; k++)     //Get all the category to which item will be inserted
                //{
                //    if (chkLstCategory.Items[k].Selected == true)
                //    {
                //        getCategory += Convert.ToString(chkLstCategory.Items[k].Value) + ",";
                //    }
                //}

                int    id         = getCategory.Length;
                string categotyId = getCategory.Substring(0, id - 1);

                if (Convert.ToString(ViewState["EditImage"]) != "1")
                {
                    itemBLLObj.itemId     = System.Guid.NewGuid().ToString(); //Creates a GUID as new Item Id
                    itemBLLObj.categoryId = categotyId;
                    string thisDir = Server.MapPath("~");

                    if (!System.IO.Directory.Exists(thisDir + "\\Item_Resource\\" + itemBLLObj.itemId))  //Creates a folder named that of GIUD(itemId)
                    {
                        System.IO.Directory.CreateDirectory(thisDir + "\\Item_Resource\\" + itemBLLObj.itemId);
                    }
                    System.Random round    = new Random();
                    string        rndImage = Convert.ToString(round.Next(1, 9999));

                    if (uplSelectImage.HasFile)
                    {
                        itemBLLObj.itemImage = uplSelectImage.FileName;
                        imageOk = cc.CheckImageExtension(itemBLLObj.itemImage);  //check for correct image extension
                    }

                    if (imageOk == true)
                    {
                        try
                        {
                            uplSelectImage.PostedFile.SaveAs(path + "\\" + itemBLLObj.itemId + "\\" + rndImage + itemBLLObj.itemImage);
                            itemBLLObj.itemImage = rndImage + itemBLLObj.itemImage;
                            fileSaved            = true;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }

                    status = itemBLLObj.BLLInsertItem(itemBLLObj);

                    if (status > 0)
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "msg", "alert('Item Inserted Successfully.')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "msg", "alert('Item Not Inserted.')", true);
                    }
                }
                else   //Update the Item
                {
                    itemBLLObj.itemId          = Convert.ToString(ViewState["itemId"]);
                    itemBLLObj.itemDescription = txtItemDescription.Text;
                    itemBLLObj.itemName        = txtItemName.Text;
                    itemBLLObj.categoryId      = categotyId;
                    itemBLLObj.cityId          = Convert.ToInt32(cmbCity.SelectedValue);

                    System.Random round    = new Random();
                    string        rndImage = Convert.ToString(round.Next(1, 9999));

                    if (uplSelectImage.HasFile)
                    {
                        itemBLLObj.itemImage = uplSelectImage.FileName;
                        imageOk = cc.CheckImageExtension(itemBLLObj.itemImage);  //check for correct image extension
                    }

                    if (imageOk == true)
                    {
                        try
                        {
                            uplSelectImage.PostedFile.SaveAs(path + "\\" + itemBLLObj.itemId + "\\" + rndImage + itemBLLObj.itemImage);
                            itemBLLObj.itemImage = rndImage + itemBLLObj.itemImage;
                            fileSaved            = true;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    status = itemBLLObj.BLLUpdateItem(itemBLLObj);
                    if (status > 0)
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "msg", "alert('Item Updated Successfully.')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, typeof(Page), "msg", "alert('Item Not Updated.')", true);
                    }
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
    }