/// <summary>
    /// show the existing poll in edit mode
    /// This is for demo only Never write ur database logic here. Always use classes and SPs to seperate them from codebehind
    /// </summary>
    private void ShowExistingPoll(int PollId)
    {
        PollingController objc    = new PollingController();
        Pollinginfo       objPoll = new Pollinginfo();
        DataSet           dsPoll  = objc.SelectPoll(PollId, Int32.Parse(SageUserModuleID), GetPortalID);

        if (dsPoll.Tables[0].Rows.Count > 0)
        {
            txtQuestion.Text = dsPoll.Tables[0].Rows[0]["Question"].ToString();
            string blockMode = dsPoll.Tables[0].Rows[0]["BlockMode"].ToString();
            if (blockMode == Pollinginfo.BlockMode.COOKIE.ToString())
            {
                rdoCookie.Checked = true;
            }
            else if (blockMode == Pollinginfo.BlockMode.IP_ADDRESS.ToString())
            {
                rdoIP.Checked = true;
            }
            else
            {
                rdoNone.Checked = true;
            }

            ShowChoices(dsPoll.Tables[1]);
            hidRowIndex.Value = dsPoll.Tables[1].Rows.Count.ToString();
            //btnSave.Text = "Update Poll";
        }
        else //can't find the poll.. change to INSERT MODE
        {
            hidPollID.Value = "";
            // btnSave.Text = "Save Poll";
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Pollinginfo objPoll = new Pollinginfo();
            if (Session["PollID"] == null)
            {
                pollID = 0;
            }
            else
            {
                pollID = Convert.ToInt32(Session["PollID"]);

            }

            string blockMode;
            if (rdoCookie.Checked)
                blockMode = Pollinginfo.BlockMode.COOKIE.ToString();
            else if (rdoIP.Checked)
                blockMode = Pollinginfo.BlockMode.IP_ADDRESS.ToString();
            else
                blockMode = Pollinginfo.BlockMode.NONE.ToString();


            if (pollID == 0) //INSERT
            {
                pollID = InsertPoll(txtQuestion.Text.Trim(), blockMode);

                //POLL INSERTED SUCCESSFULLY
                if (pollID > 0)
                {
                    hidPollID.Value = pollID.ToString();
                    ShowMessage(SageMessageTitle.Information.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/Polling/ModuleLocalText", "AddNewPollMessage"), "", SageMessageType.Success);
                }
            }
            else //UPDATE
            {

                UpdatePoll(pollID, txtQuestion.Text.Trim(), blockMode);
                ShowMessage(SageMessageTitle.Information.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/Polling/ModuleLocalText", "UpdatePollSuccess"), "", SageMessageType.Success);

            }

            if (pollID > 0)
            {
                InsertUpdateChoices(pollID, Int32.Parse(SageUserModuleID), GetPortalID); //After inserting/Updateting the poll - call choice insert/update function 
            }
            else
            {
                ShowMessage(SageMessageTitle.Information.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/Polling/ModuleLocalText", "Error"), "", SageMessageType.Error);

            }
            Session["PollID"] = null;
        }
        txtQuestion.Text = string.Empty;
        divform.Attributes.Add("style", "display:none");
        divManipulateData.Attributes.Add("style", "display:block");
        listPolls();

    }
    /// <summary>
    /// The input choices can come in 3 mode -
    /// 1 - INSERT - IF its added as NEW - it will not have a corresponding Choice ID hidden field
    /// 2 - UPDATE - IF a choice text input has corresponding hidden ID present itss in UPDATE mode
    /// 3 - DELETE - IF a choice text input has corresponding hidden ID present but the text value is empty
    /// </summary>
    /// <param name="pollID"></param>
    private void InsertUpdateChoices(int pollID, int UserModuleID, int PortalID)
    {
        Pollinginfo       objPoll = new Pollinginfo();
        PollingController objc    = new PollingController();

        //INSERT/UPDATE THE CHOICES
        //LOOPING THRU ALL THE FORM KEYVALUES TO FIND THE INPUT TEXTS
        //IF A CHOICE HAS CORRESPONDING ID THEN IT HAS TO BE UPDATES/ ELSE INSERT
        foreach (string key in Request.Form)
        {
            if (key.IndexOf("txtChoice") >= 0)
            {
                string hidPollChoiceID = "hidPollChoiceID" + key.Substring(key.Length - 1, 1);
                int    choiceID        = 0;
                string chc             = Request.Form[key];
                if (Session["PollID"] != null)
                {
                    if (Request.Form[hidPollChoiceID] != null && int.TryParse(Request.Form[hidPollChoiceID], out choiceID))
                    {
                        if (Request.Form[key].Trim().Length > 0) //UPDATE if the choice text box is not empty and has a corresponding ID
                        {
                            objPoll.PollChoiceID = choiceID;
                            objPoll.Choice       = Request.Form[key];
                            objPoll.UserModuleID = UserModuleID;
                            objPoll.PortalID     = PortalID;
                            objc.UpdateChoice(objPoll);
                        }
                        else //DELETE the choice if its empty
                        {
                            objc.DeleteChoice(choiceID);
                        }
                    }
                    else if (Request.Form[key].Trim().Length > 0)//New choice - insert only if the text is not empty
                    {
                        objPoll.PollID       = pollID;
                        objPoll.Choice       = Request.Form[key].Trim();
                        objPoll.UserModuleID = UserModuleID;
                        objPoll.PortalID     = PortalID;
                        objc.InsertChoice(objPoll);
                    }
                }
                else
                {
                    if (Request.Form[key].Trim().Length > 0)//New choice - insert only if the text is not empty
                    {
                        objPoll.PollID       = pollID;
                        objPoll.Choice       = Request.Form[key].Trim();
                        objPoll.UserModuleID = UserModuleID;
                        objPoll.PortalID     = PortalID;
                        objc.InsertChoice(objPoll);
                    }
                }
            }
        }
        hidRowIndex.Value = null;
    }
    public int InsertPoll(string question, string blockMode)
    {
        Pollinginfo objInf = new Pollinginfo();

        objInf.Question     = question;
        objInf.blockMode    = blockMode;
        objInf.UserModuleID = Int32.Parse(SageUserModuleID);
        objInf.PortalID     = GetPortalID;
        PollingController objc = new PollingController();

        return(objc.InsertPoll(objInf));
    }
    /// <summary>
    /// Update the poll and return the number of rows affected
    /// </summary>
    /// <param name="pollID"></param>
    /// <param name="question"></param>
    /// <returns>Number of rows affected</returns>
    public void UpdatePoll(int pollID, string question, string blockMode)
    {
        Pollinginfo obj = new Pollinginfo();

        obj.PollID    = pollID;
        obj.Question  = question;
        obj.blockMode = blockMode;

        PollingController objc = new PollingController();

        objc.UpdatePoll(obj);
        // return db.ExecuteNonQuery("usp_poll_update", pollID, question, blockMode, active);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Pollinginfo objPoll = new Pollinginfo();
            if (Session["PollID"] == null)
            {
                pollID = 0;
            }
            else
            {
                pollID = Convert.ToInt32(Session["PollID"]);
            }

            string blockMode;
            if (rdoCookie.Checked)
            {
                blockMode = Pollinginfo.BlockMode.COOKIE.ToString();
            }
            else if (rdoIP.Checked)
            {
                blockMode = Pollinginfo.BlockMode.IP_ADDRESS.ToString();
            }
            else
            {
                blockMode = Pollinginfo.BlockMode.NONE.ToString();
            }


            if (pollID == 0) //INSERT
            {
                pollID = InsertPoll(txtQuestion.Text.Trim(), blockMode);

                //POLL INSERTED SUCCESSFULLY
                if (pollID > 0)
                {
                    hidPollID.Value = pollID.ToString();
                    ShowMessage(SageMessageTitle.Information.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/Polling/ModuleLocalText", "AddNewPollMessage"), "", SageMessageType.Success);
                }
            }
            else //UPDATE
            {
                UpdatePoll(pollID, txtQuestion.Text.Trim(), blockMode);
                ShowMessage(SageMessageTitle.Information.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/Polling/ModuleLocalText", "UpdatePollSuccess"), "", SageMessageType.Success);
            }

            if (pollID > 0)
            {
                InsertUpdateChoices(pollID, Int32.Parse(SageUserModuleID), GetPortalID); //After inserting/Updateting the poll - call choice insert/update function
            }
            else
            {
                ShowMessage(SageMessageTitle.Information.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/Polling/ModuleLocalText", "Error"), "", SageMessageType.Error);
            }
            Session["PollID"] = null;
        }
        txtQuestion.Text = string.Empty;
        divform.Attributes.Add("style", "display:none");
        divManipulateData.Attributes.Add("style", "display:block");
        listPolls();
    }
    /// <summary>
    /// The input choices can come in 3 mode -
    /// 1 - INSERT - IF its added as NEW - it will not have a corresponding Choice ID hidden field
    /// 2 - UPDATE - IF a choice text input has corresponding hidden ID present itss in UPDATE mode
    /// 3 - DELETE - IF a choice text input has corresponding hidden ID present but the text value is empty
    /// </summary>
    /// <param name="pollID"></param>
    private void InsertUpdateChoices(int pollID, int UserModuleID, int PortalID)
    {
        Pollinginfo objPoll = new Pollinginfo();
        PollingController objc = new PollingController();
        //INSERT/UPDATE THE CHOICES 
        //LOOPING THRU ALL THE FORM KEYVALUES TO FIND THE INPUT TEXTS
        //IF A CHOICE HAS CORRESPONDING ID THEN IT HAS TO BE UPDATES/ ELSE INSERT
        foreach (string key in Request.Form)
        {
            if (key.IndexOf("txtChoice") >= 0)
            {
                string hidPollChoiceID = "hidPollChoiceID" + key.Substring(key.Length - 1, 1);
                int choiceID = 0;
                string chc = Request.Form[key];
                if (Session["PollID"] != null)
                {
                    if (Request.Form[hidPollChoiceID] != null && int.TryParse(Request.Form[hidPollChoiceID], out choiceID))
                    {
                        if (Request.Form[key].Trim().Length > 0) //UPDATE if the choice text box is not empty and has a corresponding ID
                        {
                            objPoll.PollChoiceID = choiceID;
                            objPoll.Choice = Request.Form[key];
                            objPoll.UserModuleID = UserModuleID;
                            objPoll.PortalID = PortalID;
                            objc.UpdateChoice(objPoll);

                        }
                        else //DELETE the choice if its empty 
                        {
                            objc.DeleteChoice(choiceID);
                        }
                    }
                    else if (Request.Form[key].Trim().Length > 0)//New choice - insert only if the text is not empty
                    {
                        objPoll.PollID = pollID;
                        objPoll.Choice = Request.Form[key].Trim();
                        objPoll.UserModuleID = UserModuleID;
                        objPoll.PortalID = PortalID;
                        objc.InsertChoice(objPoll);
                    }
                }
                else
                {
                    if (Request.Form[key].Trim().Length > 0)//New choice - insert only if the text is not empty
                    {
                        objPoll.PollID = pollID;
                        objPoll.Choice = Request.Form[key].Trim();
                        objPoll.UserModuleID = UserModuleID;
                        objPoll.PortalID = PortalID;
                        objc.InsertChoice(objPoll);
                    }
                }
            }
        }
        hidRowIndex.Value = null;

    }
    /// <summary>
    /// show the existing poll in edit mode 
    /// This is for demo only Never write ur database logic here. Always use classes and SPs to seperate them from codebehind
    /// </summary>
    private void ShowExistingPoll(int PollId)
    {
        PollingController objc = new PollingController();
        Pollinginfo objPoll = new Pollinginfo();
        DataSet dsPoll = objc.SelectPoll(PollId, Int32.Parse(SageUserModuleID), GetPortalID);

        if (dsPoll.Tables[0].Rows.Count > 0)
        {
            txtQuestion.Text = dsPoll.Tables[0].Rows[0]["Question"].ToString();
            string blockMode = dsPoll.Tables[0].Rows[0]["BlockMode"].ToString();
            if (blockMode == Pollinginfo.BlockMode.COOKIE.ToString())
                rdoCookie.Checked = true;
            else if (blockMode == Pollinginfo.BlockMode.IP_ADDRESS.ToString())
                rdoIP.Checked = true;
            else
                rdoNone.Checked = true;

            ShowChoices(dsPoll.Tables[1]);
            hidRowIndex.Value = dsPoll.Tables[1].Rows.Count.ToString();
            //btnSave.Text = "Update Poll";
        }
        else //can't find the poll.. change to INSERT MODE
        {
            hidPollID.Value = "";
            // btnSave.Text = "Save Poll";
        }
    }
    /// <summary>
    /// Update the poll and return the number of rows affected
    /// </summary>
    /// <param name="pollID"></param>
    /// <param name="question"></param>
    /// <returns>Number of rows affected</returns>
    public void UpdatePoll(int pollID, string question, string blockMode)
    {
        Pollinginfo obj = new Pollinginfo();
        obj.PollID = pollID;
        obj.Question = question;
        obj.blockMode = blockMode;

        PollingController objc = new PollingController();
        objc.UpdatePoll(obj);
        // return db.ExecuteNonQuery("usp_poll_update", pollID, question, blockMode, active);
    }
    public int InsertPoll(string question, string blockMode)
    {
        Pollinginfo objInf = new Pollinginfo();
        objInf.Question = question;
        objInf.blockMode = blockMode;
        objInf.UserModuleID = Int32.Parse(SageUserModuleID);
        objInf.PortalID = GetPortalID;
        PollingController objc = new PollingController();
        return objc.InsertPoll(objInf);


    }