protected void ButtonConfirm_Click(object sender, EventArgs e)
        {
            Advertisement            AdvertObj = new Advertisement();
            Advertisement_Management aDao      = new Advertisement_Management();

            AdvertObj = aDao.getAdvByID(Session["AdvertID"].ToString());
            //initialise imagelink and getvalue
            string imagelink = "";
            string getvalue  = "";

            for (int i = 0; i < CheckBoxList2.Items.Count; i++)
            {
                if (CheckBoxList2.Items[i].Selected)
                {
                    getvalue += CheckBoxList2.Items[i].Text + ",";
                    getvalue  = getvalue.TrimEnd();
                }
            }

            //if uploaded file then save
            if (FileUpload1.HasFile)
            {
                string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/Images/") + FileUpload1.FileName);
            }

            if (Literal1.Text == "") // if no file is uploaded
            {
                string previousimagepath = AdvertObj.Item.ToString();
                imagelink = previousimagepath;
            }
            else
            {
                imagelink = "Images/" + Literal1.Text;
            }

            string newItemType = AdvertObj.ItemType;

            if (AdvertObj.ItemType == "image")      // If file is image,
            {
                newItemType = AdvertObj.ItemType;   // Item type will be image
            }
            else if (AdvertObj.ItemType == "video") // If file is video,
            {
                newItemType = AdvertObj.ItemType;   // Item type will be video
            }
            string NewAdvertName = adNameTB.Text.ToString();
            int    NewCompanyID  = Convert.ToInt32(DropDownListCompany.SelectedValue);
            int    NewDuration   = Convert.ToInt32(videoDurationTB.Text);

            string startdate = startDateTB.Text.ToString();
            string enddate   = endDateTB.Text.ToString();

            string        lastUpdBy = Session["userID"].ToString();
            string        lastUpdOn = DateTime.Now.ToString("MM/dd/yyyy h:mm tt");
            SqlConnection sqlcn     = new SqlConnection(Reference.Constr);

            aDao.AdvertUpdate(Session["AdvertID"].ToString(), imagelink, newItemType, NewCompanyID, NewAdvertName, NewDuration, startdate, enddate, lastUpdBy, lastUpdOn);
            //SqlCommand cmd = new SqlCommand("update [AdvertisementAudience] set AgeID=@newAgeID,GenderID=@newGenderID where AdvID=@paraAdvID " +
            // "if @@rowcount=0 insert into [AdvertisementLocation] (AdvID,AgeID,GenderID) values (@newAdvID,@newAgeID,@newGenderID)", sqlcn);
            // sqlcn.Open();
            aDao.AdvertAudienceDeleteExisting(Session["AdvertID"].ToString());
            aDao.AdvertCategoryDeleteExisting(Session["AdvertID"].ToString());
            aDao.AdvertLocationDeleteExisting(Session["AdvertID"].ToString());
            SqlConnection sqlcon   = new SqlConnection(Reference.Constr);
            string        sqlquery = "Insert into [AdvertisementCategory](AdvID,CategoryID) values(@AdvID,@CategoryID)";
            SqlCommand    sqlcom   = new SqlCommand(sqlquery, sqlcon);

            sqlcon.Open();
            string str = adCategoryTB.Text;

            string[] splitstr = str.Split(',');
            //int id = GetMaxIDAdvertisement();


            foreach (string s in splitstr)
            {
                //trim the string, i.e. remove the space if any
                string _s = s;
                _s = _s.Trim();
                sqlcom.Parameters.AddWithValue("@AdvID", Session["AdvertID"]);
                sqlcom.Parameters.AddWithValue("@CategoryID", _s);
                sqlcom.ExecuteNonQuery();
                sqlcom.Parameters.Clear();
            }


            sqlcon.Close();


            SqlConnection sqlconnn  = new SqlConnection(Reference.Constr);
            string        sqlqueryy = "Insert into [AdvertisementLocation](AdvID,BillboardID) values(@AdvID,@BillboardID)";
            SqlCommand    sqlcommm  = new SqlCommand(sqlqueryy, sqlconnn);

            sqlconnn.Open();

            //int AdvId = GetMaxIDAdvertisement();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row   = GridView1.Rows[i];
                bool        chkbx = ((CheckBox)row.FindControl("CheckBoxSelector")).Checked;
                if (chkbx)
                {
                    Label bblabel = (Label)GridView1.Rows[i].FindControl("lb_BillboardID");
                    sqlcommm.Parameters.AddWithValue("@BillboardID", Convert.ToInt32(bblabel.Text));
                    sqlcommm.Parameters.AddWithValue("@AdvID", Session["AdvertID"]);
                    sqlcommm.ExecuteNonQuery();
                    sqlcommm.Parameters.Clear();
                }
            }
            sqlconnn.Close();



            SqlConnection sqlcnAudience = new SqlConnection(Reference.Constr);
            string        sqlque        = "Insert into [AdvertisementAudience](AdvID,AgeID,GenderID) values(@AdvID,@AgeID,@GenderID)";
            SqlCommand    sqlcm         = new SqlCommand(sqlque, sqlcnAudience);

            sqlcnAudience.Open();

            //int ID_audience = GetMaxIDAdvertisement();


            for (int i = 0; i < CheckBoxList2.Items.Count; i++)
            {
                if (CheckBoxList2.Items[i].Selected == true)
                {
                    string stri = string.Empty;
                    stri = CheckBoxList2.Items[i].ToString();

                    if (stri.Contains("Male") & stri.Contains("Child"))
                    {
                        sqlcm.Parameters.AddWithValue("@GenderID", "M");
                        sqlcm.Parameters.AddWithValue("@AgeID", "1");
                    }
                    else if (stri.Contains("Male") & stri.Contains("Young Adult"))
                    {
                        sqlcm.Parameters.AddWithValue("@GenderID", "M");
                        sqlcm.Parameters.AddWithValue("@AgeID", "2");
                    }
                    else if (stri.Contains("Male") & stri.Contains("Age 31-65"))
                    {
                        sqlcm.Parameters.AddWithValue("@GenderID", "M");
                        sqlcm.Parameters.AddWithValue("@AgeID", "3");
                    }
                    else if (stri.Contains("Male") & stri.Contains("Senior"))
                    {
                        sqlcm.Parameters.AddWithValue("@GenderID", "M");
                        sqlcm.Parameters.AddWithValue("@AgeID", "4");
                    }
                    else if (stri.Contains("Female") & stri.Contains("Child"))
                    {
                        sqlcm.Parameters.AddWithValue("@GenderID", "F");
                        sqlcm.Parameters.AddWithValue("@AgeID", "1");
                    }
                    else if (stri.Contains("Female") & stri.Contains("Young Adult"))
                    {
                        sqlcm.Parameters.AddWithValue("@GenderID", "F");
                        sqlcm.Parameters.AddWithValue("@AgeID", "2");
                    }
                    else if (stri.Contains("Female") & stri.Contains("Age 31-65"))
                    {
                        sqlcm.Parameters.AddWithValue("@GenderID", "F");
                        sqlcm.Parameters.AddWithValue("@AgeID", "3");
                    }
                    else if (stri.Contains("Female") & stri.Contains("Senior"))
                    {
                        sqlcm.Parameters.AddWithValue("@GenderID", "F");
                        sqlcm.Parameters.AddWithValue("@AgeID", "4");
                    }


                    sqlcm.Parameters.AddWithValue("@AdvID", Session["AdvertID"]);
                    sqlcm.ExecuteNonQuery();
                    sqlcm.Parameters.Clear();
                }
            }

            sqlcnAudience.Close();
            adNameTB.Text = string.Empty;
            DropDownListCompany.SelectedIndex = 0;
            startDateTB.Text        = string.Empty;
            endDateTB.Text          = string.Empty;
            videoDurationTB.Text    = string.Empty;
            adCategoryTB.Text       = string.Empty;
            billboardDisplayTB.Text = string.Empty;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                CheckBoxList1.Items[i].Selected = false;
            }
            for (int i = 0; i > CheckBoxList2.Items.Count; i++)
            {
                CheckBoxList2.Items[i].Selected = false;
            }
            //alertWarning.Visible = false;
            //alertSuccess.Visible = true;
            Session["AdvertUpdate"] = 2;
            Response.Redirect("AdvertList.aspx");
        }
        protected void ButtonConfirm_Click(object sender, EventArgs e)
        {
            Advertisement            AdvertObj = new Advertisement();
            Advertisement_Management aDao      = new Advertisement_Management();

            AdvertObj = aDao.getAdvByID(Session["AdvertID"].ToString());
            //initialise imagelink and getvalue
            string imagelink = "";
            string getvalue  = "";

            for (int i = 0; i < CheckBoxList2.Items.Count; i++)
            {
                if (CheckBoxList2.Items[i].Selected)
                {
                    getvalue += CheckBoxList2.Items[i].Text + ",";
                    getvalue  = getvalue.TrimEnd();
                }
            }

            //if uploaded file then save
            if (FileUpload1.HasFile)
            {
                string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/Images/") + FileUpload1.FileName);
            }

            //if any field missing give warning!
            if (Literal1.Text == "" || startDateTB.Text == "" ||
                endDateTB.Text == "" || adCategoryTB.Text == "" || billboardDisplayTB.Text == "" || getvalue == "")
            {
                alertWarning.Visible = true;

                warningLocation.Text = "Please ensure you have filled in all required fields";
            }
            //if never agree to terms and conditions,display warning
            else if (CheckBox1.Checked == false)
            {
                alertWarning.Visible = true;

                warningLocation.Text = "Please agree with T&C";
            }

            if (startDateTB.Text == "" || endDateTB.Text == "")
            {
                warningLocation.Visible = true;
            }

            else
            {
                imagelink = "Images/" + Literal1.Text;
                string newItemType   = Literal2.Text;
                string NewAdvertName = adNameTB.Text.ToString();
                int    NewCompanyID  = Convert.ToInt32(DropDownListCompany.SelectedValue);
                int    NewDuration   = Convert.ToInt32(videoDurationTB.Text);

                string startdate = startDateTB.Text.ToString();
                string enddate   = endDateTB.Text.ToString();

                string        lastUpdBy = Session["userID"].ToString();
                string        lastUpdOn = DateTime.Now.ToString("MM/dd/yyyy h:mm tt");
                SqlConnection sqlcn     = new SqlConnection(Reference.Constr);

                aDao.AdvertUpdate(Session["AdvertID"].ToString(), imagelink, newItemType, NewCompanyID, NewAdvertName, NewDuration, startdate, enddate, lastUpdBy, lastUpdOn);
                //SqlCommand cmd = new SqlCommand("update [AdvertisementAudience] set AgeID=@newAgeID,GenderID=@newGenderID where AdvID=@paraAdvID " +
                // "if @@rowcount=0 insert into [AdvertisementLocation] (AdvID,AgeID,GenderID) values (@newAdvID,@newAgeID,@newGenderID)", sqlcn);
                // sqlcn.Open();
                aDao.AdvertAudienceDeleteExisting(Session["AdvertID"].ToString());
                aDao.AdvertCategoryDeleteExisting(Session["AdvertID"].ToString());
                aDao.AdvertLocationDeleteExisting(Session["AdvertID"].ToString());
                SqlConnection sqlcon   = new SqlConnection(Reference.Constr);
                string        sqlquery = "Insert into [AdvertisementCategory](AdvID,CategoryID) values(@AdvID,@CategoryID)";
                SqlCommand    sqlcom   = new SqlCommand(sqlquery, sqlcon);
                sqlcon.Open();
                string   str      = adCategoryTB.Text;
                string[] splitstr = str.Split(',');
                //int id = GetMaxIDAdvertisement();


                foreach (string s in splitstr)
                {
                    //trim the string, i.e. remove the space if any
                    string _s = s;
                    _s = _s.Trim();
                    sqlcom.Parameters.AddWithValue("@AdvID", Session["AdvertID"]);
                    sqlcom.Parameters.AddWithValue("@CategoryID", _s);
                    sqlcom.ExecuteNonQuery();
                    sqlcom.Parameters.Clear();
                }


                sqlcon.Close();


                SqlConnection sqlconnn  = new SqlConnection(Reference.Constr);
                string        sqlqueryy = "Insert into [AdvertisementLocation](AdvID,BillboardID) values(@AdvID,@BillboardID)";
                SqlCommand    sqlcommm  = new SqlCommand(sqlqueryy, sqlconnn);
                sqlconnn.Open();

                //int AdvId = GetMaxIDAdvertisement();

                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    GridViewRow row   = GridView1.Rows[i];
                    bool        chkbx = ((CheckBox)row.FindControl("CheckBoxSelector")).Checked;
                    if (chkbx)
                    {
                        Label bblabel = (Label)GridView1.Rows[i].FindControl("lb_BillboardID");
                        sqlcommm.Parameters.AddWithValue("@BillboardID", Convert.ToInt32(bblabel.Text));
                        sqlcommm.Parameters.AddWithValue("@AdvID", Session["AdvertID"]);
                        sqlcommm.ExecuteNonQuery();
                        sqlcommm.Parameters.Clear();
                    }
                }
                sqlconnn.Close();



                SqlConnection sqlcnAudience = new SqlConnection(Reference.Constr);
                string        sqlque        = "Insert into [AdvertisementAudience](AdvID,AgeID,GenderID) values(@AdvID,@AgeID,@GenderID)";
                SqlCommand    sqlcm         = new SqlCommand(sqlque, sqlcnAudience);
                sqlcnAudience.Open();

                //int ID_audience = GetMaxIDAdvertisement();


                for (int i = 0; i < CheckBoxList2.Items.Count; i++)
                {
                    if (CheckBoxList2.Items[i].Selected == true)
                    {
                        string stri = string.Empty;
                        stri = CheckBoxList2.Items[i].ToString();

                        if (stri.Contains("Male") & stri.Contains("Child"))
                        {
                            sqlcm.Parameters.AddWithValue("@GenderID", "M");
                            sqlcm.Parameters.AddWithValue("@AgeID", "1");
                        }
                        else if (stri.Contains("Male") & stri.Contains("Young Adult"))
                        {
                            sqlcm.Parameters.AddWithValue("@GenderID", "M");
                            sqlcm.Parameters.AddWithValue("@AgeID", "2");
                        }
                        else if (stri.Contains("Male") & stri.Contains("Age 31-65"))
                        {
                            sqlcm.Parameters.AddWithValue("@GenderID", "M");
                            sqlcm.Parameters.AddWithValue("@AgeID", "3");
                        }
                        else if (stri.Contains("Male") & stri.Contains("Senior"))
                        {
                            sqlcm.Parameters.AddWithValue("@GenderID", "M");
                            sqlcm.Parameters.AddWithValue("@AgeID", "4");
                        }
                        else if (stri.Contains("Female") & stri.Contains("Child"))
                        {
                            sqlcm.Parameters.AddWithValue("@GenderID", "F");
                            sqlcm.Parameters.AddWithValue("@AgeID", "1");
                        }
                        else if (stri.Contains("Female") & stri.Contains("Young Adult"))
                        {
                            sqlcm.Parameters.AddWithValue("@GenderID", "F");
                            sqlcm.Parameters.AddWithValue("@AgeID", "2");
                        }
                        else if (stri.Contains("Female") & stri.Contains("Age 31-65"))
                        {
                            sqlcm.Parameters.AddWithValue("@GenderID", "F");
                            sqlcm.Parameters.AddWithValue("@AgeID", "3");
                        }
                        else if (stri.Contains("Female") & stri.Contains("Senior"))
                        {
                            sqlcm.Parameters.AddWithValue("@GenderID", "F");
                            sqlcm.Parameters.AddWithValue("@AgeID", "4");
                        }


                        sqlcm.Parameters.AddWithValue("@AdvID", Session["AdvertID"]);
                        sqlcm.ExecuteNonQuery();
                        sqlcm.Parameters.Clear();
                    }
                }

                sqlcnAudience.Close();
                adNameTB.Text = string.Empty;
                DropDownListCompany.SelectedIndex = 0;
                startDateTB.Text        = string.Empty;
                endDateTB.Text          = string.Empty;
                videoDurationTB.Text    = string.Empty;
                adCategoryTB.Text       = string.Empty;
                billboardDisplayTB.Text = string.Empty;
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = false;
                }
                for (int i = 0; i > CheckBoxList2.Items.Count; i++)
                {
                    CheckBoxList2.Items[i].Selected = false;
                }
                alertWarning.Visible    = false;
                alertSuccess.Visible    = true;
                Session["AdvertUpdate"] = 2;
                Response.Redirect("AdvertList.aspx");
            }

            //ScriptManager.RegisterStartupScript(this, this.GetType(),
            //"alert",
            //"alert('Advertisement Successfully Updated');window.location ='AdListing.aspx';",
            // true);
            //Response.Write("<script language='javascript'>window.alert('Advertisement Successfully Updated');window.location='AdListingUpdate.aspx';</script>");
            //string adNamenew = AdvertObj.Name;
            //Response.Write("<script language='javascript'>alert('Successfully Updated Advertisement');</script>");
            //Server.Transfer("AdListing.aspx", true);

            // Response.Redirect(Request.RawUrl);
            // alertSuccess.Visible = true;
            // Response.Redirect("AdListing.aspx?showSuccessMessage = 1");
        }