Polls and slide shows...

Dec 9, 2010 at 11:39 PM

I did a search on the Poll widget, and downloaded it and followed the instructions to install it but whe I go to add a simple Poll to the test blog site, I get the dreaded "Ooops! An unexpected error has occurred." Has anyone gotten this error and been able to solve it? If so, how did you solve it? Any help would be appreciated.

Also I did a search on Slide shows and saw info for a a couple of the - the Silverlight one and a Picas one. Which is better? Or, more apt;y, which is easier to install and use?

Once again any and all help is greatly appreciated.

Coleen

Dec 10, 2010 at 6:40 AM

i installed Poll widget into the new Blogengine.net RC 2.0, there are couple of things i had to do to make it workable, firstly you need to make sure poll.cs file is tagged by [Serializable] attribute, because after deploying you might get into the serialization problem, this will resolve the issue, most importantly the Poll widget is created with old blogengine.net platform, you have to replace $ sign in widget.ascx with BlogEngine.$, there will total of 13 ($ sign) instances to be replaced with BlogEngine.$. Hope this resolve your problem.

Cheers :)

 

Dec 10, 2010 at 4:23 PM

Thanks Coolrah :-)

I do have some questions though...when I open the .cs file, there aren't any $ instances in the code! I'm obviously missing something. Can you please tell me where to look?  Are you referring to the file for the Poll widget? I did find some $ instances in the file under widgets/poll/widget.ascx - are those the instances you are referring to? I don't want to start changing code that is not the correct code to change.  Thanks for your help.

 

Dec 10, 2010 at 5:33 PM

Okay, I tried replacing all the $ in the widget.aspx file (under the poll sub-directory of the widget directory)  with the BlogEngine$ and it no shows a little more in the widget editor, but still does not work correctly. What I get now is a box with the question and a message to choose an answer and a roll-over to Show Results (which does not work) and the choose answer doesn't show the answers available to choose from...

Any other suggestions?

Coordinator
Dec 10, 2010 at 5:46 PM

My suggestion would be to copy-and-paste all of the code you have now here.  And what version of BE are you using?

Dec 10, 2010 at 8:50 PM

Thanks Ben. The version of the Blog Engine is the most recent, 1.6.1 - I just downloaded and installed it in October. The code for the Poll widget .cs (which I have not changed at all) is here:

/*#############################################################
Developed by: Fatih Sever | www.fatihsever.com

Don't hesitate to use and modify but please provide credits.
#############################################################*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.IO;
using BlogEngine.Core;
using System.Data;
using System.Globalization;
using BlogEngine.Core.Providers;
using System.Configuration;
using System.Data.Common;
using System.Configuration.Provider;

namespace Controls
{
    #region Answer Object
    public class Answer
    {
        #region Constructor

        public Answer()
        {
            id = Guid.NewGuid();
        }

        #endregion

        #region Fields & Properties

        private Guid id;
        public Guid Id
        {
            get { return id; }
            set { id = value; }
        }

        private string text;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        private int vote;
        public int Vote
        {
            get { return vote; }
            set { vote = value; }
        }

        #endregion

    }
    #endregion

    #region Poll Object
    public class Poll
    {
        #region Constructor

        public Poll()
        {
            id = Guid.NewGuid();
            answers = new List<Answer>();
        }

        #endregion

        #region Fields & Properties

        private Guid id;
        public Guid Id
        {
            get { return id; }
            set { id = value; }
        }

        private string question;
        public string Question
        {
            get { return question; }
            set { question = value; }
        }

        private List<Answer> answers;
        public List<Answer> Answers
        {
            get { return answers; }
            set { answers = value; }
        }

        private bool activated;
        public bool Activated
        {
            get { return activated; }
            set { activated = value; }
        }

        private DateTime dateCreated;
        public DateTime DateCreated
        {
            get { return dateCreated; }
            set { dateCreated = value; }
        }

        private int totalVote;
        public int TotalVote
        {
            get { return totalVote; }
            set { totalVote = value; }
        }

        #endregion
    }

    #endregion

    #region Polls Control

    public static class Polls
    {
        #region Fields

        private static string folder = HttpContext.Current.Server.MapPath(BlogSettings.Instance.StorageLocation);
        private static BlogProviderSection section = (BlogProviderSection)ConfigurationManager.GetSection("BlogEngine/blogProvider");
        private static string connString = ConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString;
        private static DbProviderFactory provider = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["BlogEngine"].ProviderName);
        private static string parmPrefix = section.Providers["DbBlogProvider"].Parameters["parmPrefix"] != null ? section.Providers["DbBlogProvider"].Parameters["parmPrefix"].ToString() : "@";
        private static string tablePrefix = section.Providers["DbBlogProvider"].Parameters["tablePrefix"] != null ? section.Providers["DbBlogProvider"].Parameters["tablePrefix"].ToString() : "be_";

        #endregion

        #region Methods

        private static void CreateXml(Poll poll, List<Poll> polls, bool IsDelete)
        {
            using (XmlTextWriter pollWriter = new XmlTextWriter(folder + "polls.xml", System.Text.Encoding.UTF8))
            {
                pollWriter.Formatting = Formatting.Indented;
                pollWriter.Indentation = 4;
                pollWriter.WriteStartDocument(true);
                pollWriter.WriteStartElement("polls");

                foreach (Poll pol in polls)
                {
                    pollWriter.WriteStartElement("poll");
                    pollWriter.WriteAttributeString("id", pol.Id.ToString());
                    if (!IsDelete)
                    {
                        if (!poll.Activated)
                        {
                            pollWriter.WriteAttributeString("activated", pol.Activated.ToString());
                        }
                        else
                        {
                            if (pol.Id != poll.Id)
                            {
                                pollWriter.WriteAttributeString("activated", "False");
                            }
                            else
                            {
                                pollWriter.WriteAttributeString("activated", "True");
                            }
                        }
                    }
                    else
                    {
                        pollWriter.WriteAttributeString("activated", pol.Activated.ToString());
                    }
                    pollWriter.WriteAttributeString("datecreated", pol.DateCreated.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));

                    pollWriter.WriteStartElement("question");
                    pollWriter.WriteValue(pol.Question);
                    pollWriter.WriteEndElement();

                    pollWriter.WriteStartElement("answers");

                    foreach (Answer ans in pol.Answers)
                    {
                        pollWriter.WriteStartElement("answer");
                        pollWriter.WriteAttributeString("id", ans.Id.ToString());
                        pollWriter.WriteAttributeString("vote", ans.Vote.ToString());
                        pollWriter.WriteValue(ans.Text);
                        pollWriter.WriteEndElement();
                    }

                    pollWriter.WriteEndElement();
                    pollWriter.WriteEndElement();
                }

                pollWriter.WriteEndElement();
            }
        }

        public static bool InsertPoll(Poll poll)
        {
            if (section.DefaultProvider == "XmlBlogProvider")
            {
                List<Poll> polls = new List<Poll>();

                if (File.Exists(folder + "polls.xml"))
                {
                    polls = FillPolls();
                }
                polls.Add(poll);

                CreateXml(poll, polls, false);

                return true;
            }
            else if (section.DefaultProvider == "DbBlogProvider")
            {
                try
                {
                    using (DbConnection conn = provider.CreateConnection())
                    {
                        conn.ConnectionString = connString;
                        conn.Open();

                        using (DbCommand cmd = conn.CreateCommand())
                        {
                            string sqlQuery;

                            if (poll.Activated)
                            {
                                sqlQuery = "UPDATE " + tablePrefix + "Polls SET Activated=@false;" +
                                    "INSERT INTO " + tablePrefix + "Polls" +
                                      "(PollID,Question,Activated,DateCreated) " +
                                      "VALUES(@pollId,@question,@activated,@dateCreated)";
                            }
                            else
                            {
                                sqlQuery = "INSERT INTO " + tablePrefix + "Polls" +
                                           "(PollID,Question,Activated,DateCreated) " +
                                           "VALUES(@pollId,@question,@activated,@dateCreated)";
                            }

                            if (parmPrefix != "@")
                            {
                                sqlQuery = sqlQuery.Replace("@", parmPrefix);
                            }
                            cmd.CommandText = sqlQuery;
                            cmd.CommandType = CommandType.Text;

                            DbParameter pID = provider.CreateParameter();
                            pID.ParameterName = parmPrefix + "pollId";
                            pID.Value = poll.Id;
                            cmd.Parameters.Add(pID);

                            DbParameter pQuestion = provider.CreateParameter();
                            pQuestion.ParameterName = parmPrefix + "question";
                            pQuestion.Value = poll.Question;
                            cmd.Parameters.Add(pQuestion);

                            DbParameter pActivated = provider.CreateParameter();
                            pActivated.ParameterName = parmPrefix + "activated";
                            pActivated.Value = poll.Activated;
                            cmd.Parameters.Add(pActivated);

                            DbParameter pDate = provider.CreateParameter();
                            pDate.ParameterName = parmPrefix + "dateCreated";
                            pDate.Value = poll.DateCreated;
                            cmd.Parameters.Add(pDate);

                            DbParameter pFalse = provider.CreateParameter();
                            pFalse.ParameterName = parmPrefix + "false";
                            pFalse.Value = false;
                            cmd.Parameters.Add(pFalse);

                            cmd.ExecuteNonQuery();


                            // Answers    

                            foreach (Answer answer in poll.Answers)
                            {
                                sqlQuery = "INSERT INTO " + tablePrefix + "PollAnswers" +
                                       "(AnswerID,PollID,Answer,Vote) " +
                                       "VALUES(@answerId,@aPollId,@answer,@vote)";

                                if (parmPrefix != "@")
                                {
                                    sqlQuery = sqlQuery.Replace("@", parmPrefix);
                                }
                                cmd.CommandText = sqlQuery;
                                cmd.CommandType = CommandType.Text;

                                DbParameter aID = provider.CreateParameter();
                                aID.ParameterName = parmPrefix + "answerId";
                                aID.Value = answer.Id;
                                cmd.Parameters.Add(aID);

                                DbParameter aPID = provider.CreateParameter();
                                aPID.ParameterName = parmPrefix + "aPollId";
                                aPID.Value = poll.Id;
                                cmd.Parameters.Add(aPID);

                                DbParameter aAnswer = provider.CreateParameter();
                                aAnswer.ParameterName = parmPrefix + "answer";
                                aAnswer.Value = answer.Text;
                                cmd.Parameters.Add(aAnswer);

                                DbParameter aVote = provider.CreateParameter();
                                aVote.ParameterName = parmPrefix + "vote";
                                aVote.Value = answer.Vote;
                                cmd.Parameters.Add(aVote);

                                cmd.ExecuteNonQuery();

                                cmd.Parameters.Clear();
                            }
                        }
                        conn.Close();
                    }

                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                throw new System.Configuration.Provider.ProviderException("Unable to load default BlogProvider");
            }
        }

        public static bool UpdatePoll(Poll poll, string pollId)
        {
            try
            {
                if (section.DefaultProvider == "XmlBlogProvider")
                {

                    List<Poll> polls = FillPolls();

                    foreach (Poll pol in polls.ToArray())
                    {
                        if (pol.Id == new Guid(pollId))
                        {
                            polls.Remove(pol);
                        }
                    }

                    polls.Add(poll);

                    CreateXml(poll, polls, false);

                    return true;

                }
                else if (section.DefaultProvider == "DbBlogProvider")
                {
                    if (DeletePoll(pollId))
                    {
                        InsertPoll(poll);
                    }

                    return true;
                }
                else
                {
                    throw new System.Configuration.Provider.ProviderException("Unable to load default BlogProvider");
                }
            }
            catch
            {
                return false;
            }
        }

        public static bool DeletePoll(string pollId)
        {
            if (section.DefaultProvider == "XmlBlogProvider")
            {
                List<Poll> polls = FillPolls();

                foreach (Poll pol in polls.ToArray())
                {
                    if (pol.Id == new Guid(pollId))
                    {
                        polls.Remove(pol);
                    }
                }

                CreateXml(null, polls, true);
            }
            else if (section.DefaultProvider == "DbBlogProvider")
            {
                using (DbConnection conn = provider.CreateConnection())
                {
                    conn.ConnectionString = connString;
                    conn.Open();

                    using (DbCommand cmd = conn.CreateCommand())
                    {
                        string sqlQuery = "DELETE FROM " + tablePrefix + "PollAnswers WHERE PollID = @pollId;" +
                                     "DELETE FROM " + tablePrefix + "Polls WHERE PollID = @pollId;";

                        if (parmPrefix != "@")
                        {
                            sqlQuery = sqlQuery.Replace("@", parmPrefix);
                        }
                        cmd.CommandText = sqlQuery;
                        cmd.CommandType = CommandType.Text;

                        DbParameter pID = provider.CreateParameter();
                        pID.ParameterName = parmPrefix + "pollId";
                        pID.Value = new Guid(pollId);
                        cmd.Parameters.Add(pID);

                        cmd.ExecuteNonQuery();
                    }

                    conn.Close();
                }
            }
            else
            {
                throw new System.Configuration.Provider.ProviderException("Unable to load default BlogProvider");
            }

            return true;
        }

        public static Poll SelectPoll()
        {
            Poll poll = new Poll(); ;

            if (section.DefaultProvider == "XmlBlogProvider")
            {
                if (!File.Exists(folder + "polls.xml"))
                {
                    return null;
                }

                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(folder + "polls.xml");
                }
                catch
                {
                    return null;
                }

                foreach (XmlNode node in doc.SelectNodes("polls/poll"))
                {
                    if (node == null)
                    {
                        return null;
                    }

                    bool IsActivated = Convert.ToBoolean(node.Attributes["activated"].InnerText);

                    if (IsActivated)
                    {
                        poll.Id = new Guid(node.Attributes["id"].InnerText);
                        poll.Activated = IsActivated;
                        poll.DateCreated = DateTime.Parse(node.Attributes["datecreated"].InnerText, System.Globalization.CultureInfo.InvariantCulture);
                        poll.Question = node.ChildNodes[0].InnerText;

                        Answer answer = null;

                        foreach (XmlNode node2 in node.SelectNodes("answers/answer"))
                        {
                            answer = new Answer();

                            answer.Id = new Guid(node2.Attributes["id"].InnerText);
                            answer.Vote = Convert.ToInt32(node2.Attributes["vote"].InnerText);
                            answer.Text = node2.ChildNodes[0].InnerText;

                            poll.TotalVote += Convert.ToInt32(node2.Attributes["vote"].InnerText);

                            poll.Answers.Add(answer);
                        }

                        return poll;
                    }
                }

                return null;
            }
            else if (section.DefaultProvider == "DbBlogProvider")
            {
                try
                {
                    using (DbConnection conn = provider.CreateConnection())
                    {
                        conn.ConnectionString = connString;

                        using (DbCommand cmd = conn.CreateCommand())
                        {
                            string sqlQuery = "SELECT * FROM " + tablePrefix + "Polls " +
                                        "WHERE Activated = @activated";

                            if (parmPrefix != "@")
                            {
                                sqlQuery = sqlQuery.Replace("@", parmPrefix);
                            }
                            cmd.CommandText = sqlQuery;
                            cmd.CommandType = CommandType.Text;

                            DbParameter dpActivated = provider.CreateParameter();
                            dpActivated.ParameterName = parmPrefix + "activated";
                            dpActivated.Value = true;
                            cmd.Parameters.Add(dpActivated);

                            conn.Open();

                            using (DbDataReader rdr = cmd.ExecuteReader())
                            {
                                if (rdr.HasRows)
                                {
                                    rdr.Read();

                                    poll.Id = rdr.GetGuid(0);
                                    if (!rdr.IsDBNull(1))
                                    {
                                        poll.Question = rdr.GetString(1);
                                    }
                                    if (!rdr.IsDBNull(2))
                                    {
                                        poll.Activated = rdr.GetBoolean(2);
                                    }
                                    if (!rdr.IsDBNull(3))
                                    {
                                        poll.DateCreated = rdr.GetDateTime(3);
                                    }
                                }
                                else
                                {
                                    return null;
                                }
                            }


                            // Answers    

                            sqlQuery = "SELECT * FROM " + tablePrefix + "PollAnswers " +
                                        "WHERE PollID = @pollId";

                            if (parmPrefix != "@")
                            {
                                sqlQuery = sqlQuery.Replace("@", parmPrefix);
                            }
                            cmd.CommandText = sqlQuery;
                            cmd.CommandType = CommandType.Text;

                            DbParameter dpID = provider.CreateParameter();
                            dpID.ParameterName = parmPrefix + "pollId";
                            dpID.Value = poll.Id;
                            cmd.Parameters.Add(dpID);

                            using (DbDataReader rdr = cmd.ExecuteReader())
                            {
                                Answer answer = null;

                                while (rdr.Read())
                                {
                                    answer = new Answer();

                                    answer.Id = rdr.GetGuid(0);
                                    if (!rdr.IsDBNull(2))
                                    {
                                        answer.Text = rdr.GetString(2);
                                    }
                                    if (!rdr.IsDBNull(3))
                                    {
                                        answer.Vote = rdr.GetInt32(3);
                                        poll.TotalVote += rdr.GetInt32(3);
                                    }

                                    poll.Answers.Add(answer);
                                }
                            }
                        }
                        conn.Close();
                    }
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                throw new System.Configuration.Provider.ProviderException("Unable to load default BlogProvider");
            }

            return poll;
        }

        public static Poll SelectPoll(string pollId)
        {
            Poll poll = new Poll();

            if (section.DefaultProvider == "XmlBlogProvider")
            {
                if (!File.Exists(folder + "polls.xml"))
                {
                    return null;
                }

                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(folder + "polls.xml");
                }
                catch
                {
                    return null;
                }

                foreach (XmlNode node in doc.SelectNodes("polls/poll"))
                {
                    if (node == null)
                    {
                        return null;
                    }

                    if (node.Attributes["id"].InnerText.Trim() == pollId)
                    {
                        poll.Id = new Guid(node.Attributes["id"].InnerText);
                        poll.Activated = Convert.ToBoolean(node.Attributes["activated"].InnerText);
                        poll.DateCreated = DateTime.Parse(node.Attributes["datecreated"].InnerText, System.Globalization.CultureInfo.InvariantCulture);
                        poll.Question = node.ChildNodes[0].InnerText;

                        Answer answer = null;

                        foreach (XmlNode node2 in node.SelectNodes("answers/answer"))
                        {
                            answer = new Answer();

                            answer.Id = new Guid(node2.Attributes["id"].InnerText);
                            answer.Vote = Convert.ToInt32(node2.Attributes["vote"].InnerText);
                            answer.Text = node2.ChildNodes[0].InnerText;

                            Convert.ToInt32(node2.Attributes["vote"].InnerText);

                            poll.Answers.Add(answer);
                        }
                    }
                }
            }
            else if (section.DefaultProvider == "DbBlogProvider")
            {
                using (DbConnection conn = provider.CreateConnection())
                {
                    conn.ConnectionString = connString;

                    using (DbCommand cmd = conn.CreateCommand())
                    {

                        string sqlQuery = "SELECT PollID,Question,Activated,DateCreated " +
                                   "FROM " + tablePrefix + "Polls " +
                                   "WHERE PollID = @pollId";

                        if (parmPrefix != "@")
                        {
                            sqlQuery = sqlQuery.Replace("@", parmPrefix);
                        }
                        cmd.CommandText = sqlQuery;
                        cmd.CommandType = CommandType.Text;

                        DbParameter dpID = provider.CreateParameter();
                        dpID.ParameterName = parmPrefix + "pollId";
                        dpID.Value = new Guid(pollId);
                        cmd.Parameters.Add(dpID);

                        conn.Open();

                        using (DbDataReader rdr = cmd.ExecuteReader())
                        {
                            if (rdr.HasRows)
                            {
                                rdr.Read();

                                poll.Id = rdr.GetGuid(0);
                                if (!rdr.IsDBNull(1))
                                {
                                    poll.Question = rdr.GetString(1);
                                }
                                if (!rdr.IsDBNull(2))
                                {
                                    poll.Activated = rdr.GetBoolean(2);
                                }
                                if (!rdr.IsDBNull(3))
                                {
                                    poll.DateCreated = rdr.GetDateTime(3);
                                }
                            }
                            else
                            {
                                return null;
                            }
                        }


                        // Answers    

                        sqlQuery = "SELECT AnswerID,PollID,Answer,Vote " +
                                    "FROM " + tablePrefix + "PollAnswers " +
                                    "WHERE PollID = @aPollId";

                        if (parmPrefix != "@")
                        {
                            sqlQuery = sqlQuery.Replace("@", parmPrefix);
                        }
                        cmd.CommandText = sqlQuery;
                        cmd.CommandType = CommandType.Text;

                        DbParameter dpAID = provider.CreateParameter();
                        dpAID.ParameterName = parmPrefix + "aPollId";
                        dpAID.Value = poll.Id;
                        cmd.Parameters.Add(dpAID);

                        using (DbDataReader rdr = cmd.ExecuteReader())
                        {
                            Answer answer = null;

                            while (rdr.Read())
                            {
                                answer = new Answer();

                                answer.Id = rdr.GetGuid(0);
                                if (!rdr.IsDBNull(2))
                                {
                                    answer.Text = rdr.GetString(2);
                                }
                                if (!rdr.IsDBNull(3))
                                {
                                    answer.Vote = rdr.GetInt32(3);
                                }

                                poll.Answers.Add(answer);
                            }
                        }
                    }
                    conn.Close();
                }
            }
            else
            {
                throw new System.Configuration.Provider.ProviderException("Unable to load default BlogProvider");
            }

            return poll;
        }

        public static List<Poll> FillPolls()
        {
            List<Poll> polls = new List<Poll>();

            if (section.DefaultProvider == "XmlBlogProvider")
            {
                if (!File.Exists(folder + "polls.xml"))
                {
                    return null;
                }

                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(folder + "polls.xml");
                }
                catch
                {
                    return null;
                }

                Poll poll = null;

                foreach (XmlNode node in doc.SelectNodes("polls/poll"))
                {
                    if (node == null)
                    {
                        return null;
                    }

                    poll = new Poll();

                    poll.Id = new Guid(node.Attributes["id"].InnerText);
                    poll.Activated = Convert.ToBoolean(node.Attributes["activated"].InnerText);
                    poll.DateCreated = DateTime.Parse(node.Attributes["datecreated"].InnerText, System.Globalization.CultureInfo.InvariantCulture);
                    poll.Question = node.ChildNodes[0].InnerText;

                    Answer answer = null;

                    foreach (XmlNode node2 in node.SelectNodes("answers/answer"))
                    {
                        answer = new Answer();

                        answer.Id = new Guid(node2.Attributes["id"].InnerText);
                        answer.Vote = Convert.ToInt32(node2.Attributes["vote"].InnerText);
                        answer.Text = node2.ChildNodes[0].InnerText;

                        poll.TotalVote += Convert.ToInt32(node2.Attributes["vote"].InnerText);

                        poll.Answers.Add(answer);
                    }

                    polls.Add(poll);
                }
            }
            else if (section.DefaultProvider == "DbBlogProvider")
            {
                try
                {
                    using (DbConnection conn = provider.CreateConnection())
                    {
                        conn.ConnectionString = connString;

                        using (DbCommand cmd = conn.CreateCommand())
                        {

                            string sqlQuery = "SELECT * FROM " + tablePrefix + "Polls";
                            cmd.CommandText = sqlQuery;
                            cmd.CommandType = CommandType.Text;

                            conn.Open();

                            using (DbDataReader rdr = cmd.ExecuteReader())
                            {
                                Poll poll = null;

                                while (rdr.Read())
                                {
                                    if (rdr.HasRows)
                                    {
                                        poll = new Poll();

                                        poll.Id = rdr.GetGuid(0);
                                        if (!rdr.IsDBNull(1))
                                        {
                                            poll.Question = rdr.GetString(1);
                                        }
                                        if (!rdr.IsDBNull(2))
                                        {
                                            poll.Activated = rdr.GetBoolean(2);
                                        }
                                        if (!rdr.IsDBNull(3))
                                        {
                                            poll.DateCreated = rdr.GetDateTime(3);
                                        }

                                        // Answers    
                                        using (DbConnection conn2 = provider.CreateConnection())
                                        {
                                            conn2.ConnectionString = connString;

                                            using (DbCommand cmd2 = conn2.CreateCommand())
                                            {
                                                string sqlQuery2 = "SELECT * FROM " + tablePrefix + "PollAnswers " +
                                                            "WHERE PollID = @pollId";

                                                if (parmPrefix != "@")
                                                {
                                                    sqlQuery2 = sqlQuery2.Replace("@", parmPrefix);
                                                }
                                                cmd2.CommandText = sqlQuery2;
                                                cmd2.CommandType = CommandType.Text;

                                                DbParameter dpID = provider.CreateParameter();
                                                dpID.ParameterName = parmPrefix + "pollId";
                                                dpID.Value = poll.Id;
                                                cmd2.Parameters.Add(dpID);

                                                conn2.Open();

                                                using (DbDataReader rdr2 = cmd2.ExecuteReader())
                                                {
                                                    Answer answer = null;

                                                    while (rdr2.Read())
                                                    {
                                                        answer = new Answer();

                                                        answer.Id = rdr2.GetGuid(0);
                                                        if (!rdr2.IsDBNull(2))
                                                        {
                                                            answer.Text = rdr2.GetString(2);
                                                        }
                                                        if (!rdr2.IsDBNull(3))
                                                        {
                                                            answer.Vote = rdr2.GetInt32(3);
                                                            poll.TotalVote += rdr2.GetInt32(3);
                                                        }

                                                        poll.Answers.Add(answer);
                                                    }
                                                }

                                                conn2.Close();
                                            }

                                            polls.Add(poll);
                                        }
                                    }
                                    else
                                    {
                                        return null;
                                    }
                                }
                            }

                            conn.Close();
                        }
                    }
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                throw new System.Configuration.Provider.ProviderException("Unable to load default BlogProvider");
            }

            return polls;
        }

        #endregion
    }
    #endregion
}

The code for the widget/pools/widget.ascx is here - and the only change I have made is to the code is to replace the $ with the BlogEngine.$ :

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="widget.ascx.cs" Inherits="widgets_Poll_widget" %>
<div id="PollForm" style="height: 100%;">
    <div id="Question_div" runat="server" class="pollForm-Question">
    </div>
    <asp:RadioButtonList ID="AnswersRbl" runat="server" CssClass="pollForm-AnswersContainer">
    </asp:RadioButtonList>
    <br />
    <asp:Button ID="Vote_bt" runat="server" Text="Vote" OnClientClick="return beginVote()"
        CssClass="pollForm-VoteButton" />
    <div id="AjaxLoader" class="pollForm-AjaxLoader" style="display: none">
    </div>
    <span id="SelectError" style="display: none;" class="pollForm-AnswerNotSelectedError">Please choose
        an answer.</span> <span onclick="showResult();" style="cursor: pointer; float: right"
            class="pollForm-ResultsLink">Show Results</span>
    <br style="clear: both" />
</div>
<div id="PollResult" style="display: none">
    <span id="VotedBefore" style="display: none;" class="pollResult-CurrentlyVoted">You already
        voted this poll.</span> <span id="VoteSaved" style="display: none;" class="pollResult-VoteSaved">
            Your vote has been registered.</span>
    <div id="QuestionResult_div" runat="server" class="pollResult-Question">
    </div>
    <asp:Repeater ID="Results_rpt" runat="server" OnItemDataBound="Results_rpt_ItemDataBound">
        <ItemTemplate>
            <div class="pollResult-AnswersText">
                <%# DataBinder.Eval(Container.DataItem, "Text") %></div>
            <div class="pollResult-AnswersProgressBarContainer">
                <div id="Progress_div" runat="server" style="float: left;" class="pollResult-AnswersProgressBar">
                    &nbsp;
                </div>
                <div id="Percent_div" runat="server" class="pollResult-AnswersPercentage">
                </div>
            </div>
        </ItemTemplate>
    </asp:Repeater>
    <br />
    <div class="pollResult-TotalVotes" style="float: left">
        Total Vote:
        <asp:Label ID="TotalVote_lb" runat="server" Text=""></asp:Label></div>
    <span id="ShowPoll" onclick="showPoll();" style="cursor: pointer; float: right" class="pollResult-PollLink">
        Show Poll</span>
    <br style="clear: both" />
</div>

<script type="text/javascript">
  function getElementsByName_iefix(tag, name) 
  {    
       var elem = document.getElementsByTagName(tag);
       var arr = new Array();
       for(i = 0,iarr = 0; i < elem.length; i++) {
            att = elem[i].getAttribute("name");
            if(att == name) {
                 arr[iarr] = elem[i];
                 iarr++;
            }
       }
       return arr;
  }
    
  function beginVote()
  {
    BlogEngine.$('SelectError').style.display = 'none';
    var clientId= '<%=AnswersRbl.ClientID %>';
    var clientName= clientId.replace(/_/g,"BlogEngine.$");    
    var answerRadioButtons = getElementsByName_iefix("input", clientName);
    var rblDCSelection = "";

    for (var i in answerRadioButtons)
    {
        if (answerRadioButtons[i].checked)
        {
           rblDCSelection = answerRadioButtons[i].value;
           break;
        }
    }     
    
    if(rblDCSelection == "")
    {
        BlogEngine.$('SelectError').style.display = 'block';
        return false;
    }
    
    BlogEngine.$('AjaxLoader').style.display = 'block';
    BlogEngine.$('<%=Vote_bt.ClientID %>').style.display = 'none';
    
    var arg = rblDCSelection;
    var context = 'poll';
    <%=Page.ClientScript.GetCallbackEventReference(this, "arg", "endVote", "context") %>;
    
    return false;
  }
  
  function endVote(arg, context)
  {
     if(arg=="true")
     {
       BlogEngine.$('ShowPoll').style.display = 'none';
       BlogEngine.$('VoteSaved').style.display = 'block';
       showResult();
     }
     else
     {
       BlogEngine.$('VotedBefore').style.display = 'block';
       showResult();
     }
  }
  
  function showResult()
  {         
    BlogEngine.$('PollForm').style.display = 'none';
    BlogEngine.$('PollResult').style.display = 'block';
    BlogEngine.$('<%=Vote_bt.ClientID %>').style.display = 'block';
    BlogEngine.$('AjaxLoader').style.display = 'none';
  }  
  
  function showPoll()
  {
    BlogEngine.$('PollForm').style.display = 'block';
    BlogEngine.$('PollResult').style.display = 'none';
  }  
</script>

Thanks for any/all help - it is much appreciated.

Coleen

Coordinator
Dec 10, 2010 at 10:28 PM

I have a ZIP download of the Poll widget from several months back... probably the same code you have.

The Poll.cs file should be put into the App_Code\Controls folder.  In the Widgets\Polls folder, there should be 4 files -- widget.ascx, widget.ascx.cs, edit.ascx and edit.ascx.cs.

Then it looks like there is a CSS file, and an achivepoll.aspx and archivepoll.aspx.cs file.  Maybe others too.  Do you have all these files setup on your blog and in the correct locations?

Just asking since it looks like you posted only 2 of the several files here, and not sure if you have all the files in the correct locations.

Dec 10, 2010 at 11:09 PM

Thanks Ben. I followed the instructions in the ReadMe.txt file exactly:

REQUIREMENTS
------------

BlogEngine.NET 1.4 (XML or DataBase)


INSTALLATION
------------

1.  Copy "Poll.cs" file to "App_Code/Controls" folder which is in BlogEngine main directory.

2.  Copy "Poll" folder to "widgets" folder which is in your blog's main directory.

3.  If you use database for BlogEngine, you should configure your database with appropriate script 
    in the "Database Setup" folder.

4.  Copy "poll.css" and "progress.gif" files in "Sample Css" to your themes's folder ('Standard',etc.)
    in "themes" folder which is in your blog's main directory. Open "site.master" file in your text
    editor and add the following code just above the </head> tag.

    <link href="poll.css" rel="stylesheet" type="text/css" />
        

5.  If you want to use polls archive, copy "archivepoll.aspx" and "archivepoll.aspx.cs" files in "Archive"
    folder to BlogEngine main directory. Use some link like that to access archive:
    http://www.yourblog.com/archivepoll.aspx

I don't use an SQL database so I did not do any configuration for that. Under the Widget/Poll file I do have edit.ascx, edit.ascx.cs, widget.ascx, and widget.ascx.cs. I didn't list any of the other files because we weren't discussing any changes to them (unless I misunderstood what coolrah was referring to - which could happen ;-) but I just downloaded the most recent code from the link here: http://www.fatihsever.com/post/BlogEngineNET-Poll-Widget.aspx so maybe there is another file that is more recent?

Do you want me to post the rest of the code? I haven't touched any of it except the above that I mentioned. Thanks very much for your help.

Coleen

Coordinator
Dec 11, 2010 at 3:10 AM
Edited Dec 11, 2010 at 3:11 AM

You probably have the files in the right place then ... since you read the ReadMe file :)

I did find what problem with the widget.ascx file you posted.  When you were changing $ to BlogEngine.$, you made one too many changes.  You have this line of code in widget.ascx:

var clientName= clientId.replace(/_/g,"BlogEngine.$");

That should be what it originally was:

var clientName= clientId.replace(/_/g,"$");

The other $ to BlogEngine.$ changes are good.  I tested the Poll with 1.6.1 and was able to create a Poll, but then was getting "Please choose an answer" when I would try to vote.  This was because of that problem with the one-too many $ to BlogEngine.$ changes.  Fixing it as shown above allowed me to vote.

You are able to create a Poll?  You see the poll question and answers to choose from?  When you click on "Show Results", nothing happens?  Have you tried another browser?  If you're using IE, when you click on Show Results, do you see an Error in the IE status bar (left side)?  If you have Firefox available, try Show Results, and if it doesn't work, look in the Error Console (Tools -> Error Console) and look to see if anything shows up.

If you're still stuck, I can add some debugging statements to the widget.ascx file that you can use, to try and see where it might be breaking down.

Dec 11, 2010 at 12:36 PM
Edited Dec 11, 2010 at 12:58 PM

Yes I was able to create the poll, but as you said, I couldn't vote on it (it never displayed the options that I had set to vote on) but since I made the change back (thank you for that!) it still doesn't show the question or the options to vote, but now it does allow me to click on the show results and shows 0 Votes.  Funny - it's kind of hard to vote when the question won' display ;-)

I double-checked that I did follow the instructions and yes, everything is where it's supposed to be.  This is what I get:

 

Photobucket

So I am not sure what the problem is. Thanks very much for your help.

Coleen

 

 

Coordinator
Dec 11, 2010 at 8:29 PM

If you click Edit on the Poll you created, there'll be a dropdown list "Select a poll to edit or delete".  Is the poll you created in that dropdown list?  And if so, if you select that poll from the dropdown list, does the bottom grid then get populated with your poll question & answers?

Also, check the App_Data folder.  There should be a polls.xml file.  If you open that file up, you should see your poll question and answers.

Just trying to determine if the poll is being saved.

When you create the poll, are you clicking the Add Poll button at the bottom after you have entered the question and the answers?

Dec 11, 2010 at 11:27 PM
Edited Dec 11, 2010 at 11:37 PM

Is the poll you created in that dropdown list? No - it was at first, but when I changed the  var clientName= clientId.replace(/_/g,"BlogEngine.$"); back to var clientName= clientId.replace(/_/g,"$"); for some reason it stopped showing up in the dropdown list.

Also, check the App_Data folder.  There should be a polls.xml file.  If you open that file up, you should see your poll question and answers.  I did check that earlier and no, it is not populating the xml file and I was going to post about that even before you asked about it - I've just been tied up this afternoon...

The first time I tried clicking the Add Poll button at the bottom the whole poll disappeared, and it opened it to enter a new one - but I hadn't tried it again since I made the change for the var clientName code. So I tried it this time, and it worked - however,  now when I click on the answer (just a simple radio button yes or no) it now gices a red error message:

do you celebrate Christmas?

Yes

No

Please choose an answer.

So one problem solved very easily (just a misunderstanding of how the polls editor works - I didn't realize you had to "add" the poll, I thought that was to add another one to the sidebar) but other than it not recognizing the the selected answer, it now seems to be working - any suggestions on why it isn't "reading" the answer? Thank you very much for your help!

Coordinator
Dec 11, 2010 at 11:50 PM

The red "Please choose an answer" is what I too was seeing, until I changed that line of JS back to what it originally was.  It wasn't recognizing that I had selected an answer.  In widget.ascx is that one line of code from before, plus more lines of code above and below it.  Below, I've pasted some code (3 lines).  The first line below is the line we changed back.  The 2nd line is there from before, and the 3rd line is a new debugging line I added.  Try adding that 3rd line of code into your widget.ascx file (the alert(...) line), and then try voting.  Also, make sure the 1st of line below matches what you have ... that's the line we restored back to the original value.

var clientName= clientId.replace(/_/g,"$");
var answerRadioButtons = getElementsByName_iefix("input", clientName);
alert('answerRadioButtons.length = ' + answerRadioButtons.length);
When you vote, you should see a message box that says:

answerRadioButtons.length = 2

(or some number greater than 0).

Also, maybe you can paste here some of the HTML from your poll when you viewing it in the browser.  If you do a View Source in your browser, search (Find) the part where it has your poll answers.  It'll look something like:

<table id="ctl00_afa70b116e74462fa6d5876412c399cb_AnswersRbl" class="pollForm-AnswersContainer" border="0">
	<tr>
		<td><input id="ctl00_afa70b116e74462fa6d5876412c399cb_AnswersRbl_0" type="radio"
		name="ctl00$afa70b116e74462fa6d5876412c399cb$AnswersRbl" value="ac39d818-51b8-4d6e-a545-5c0eea453d2c" />
		<label for="ctl00_afa70b116e74462fa6d5876412c399cb_AnswersRbl_0">Yes</label></td>
	</tr><tr>
		<td><input id="ctl00_afa70b116e74462fa6d5876412c399cb_AnswersRbl_1" type="radio"
		name="ctl00$afa70b116e74462fa6d5876412c399cb$AnswersRbl" value="036fc47c-9162-4abc-b2dd-6887fd662cdd" />
		<label for="ctl00_afa70b116e74462fa6d5876412c399cb_AnswersRbl_1">No</label></td>
	</tr>
</table>

Dec 12, 2010 at 12:59 AM

Thanks so much Ben! Unfortunately the message box comes up with 0!  answerRadioButtons.length = 0

Here's the code from the source selection:

<div id="PollForm" style="height: 100%;">
    <div id="958b1e1d05df4c5dbec8620b3f2854d4_Question_div" class="pollForm-Question">do you celebrate Christmas?</div>
    <table id="958b1e1d05df4c5dbec8620b3f2854d4_AnswersRbl" class="pollForm-AnswersContainer">
	<tbody><tr>
		<td><input id="958b1e1d05df4c5dbec8620b3f2854d4_AnswersRbl_0" 
name="ctl00$958b1e1d05df4c5dbec8620b3f2854d4$AnswersRbl"
value="e1bd3fee-b3d3-4dfb-b2c5-c21a32e6ff87" type="radio">
<
label for="958b1e1d05df4c5dbec8620b3f2854d4_AnswersRbl_0">Yes</label>
</td> </tr><tr> <td><input id="958b1e1d05df4c5dbec8620b3f2854d4_AnswersRbl_1"
name="ctl00$958b1e1d05df4c5dbec8620b3f2854d4$AnswersRbl"
value="c7b45706-2414-4b65-9185-6b129e010888" checked="checked"
type="radio"><label for="958b1e1d05df4c5dbec8620b3f2854d4_AnswersRbl_1">No</label>
</
td> </tr> </tbody></table> <br> <input name="ctl00$958b1e1d05df4c5dbec8620b3f2854d4$Vote_bt" value="Vote" onclick="return beginVote();"
id="958b1e1d05df4c5dbec8620b3f2854d4_Vote_bt" class="pollForm-VoteButton" type="submit"> <div id="AjaxLoader" class="pollForm-AjaxLoader" style="display: none;"> </div> <span id="SelectError" style="display: block;" class="pollForm-AnswerNotSelectedError">Please choose an answer.</span> <span onclick="showResult();" style="cursor: pointer; float: right;"
class
="pollForm-ResultsLink">Show Results</span> <br style="clear: both;"> </div>
Thank you so very much for your help!

Coleen

Coordinator
Dec 12, 2010 at 1:59 AM

Thanks, that's sort of getting us somewhere.  One other thing if you could paste here.  When doing a View Source, there will be the beginVote() JS function in there.  It starts off like:

function beginVote()
{
   BlogEngine.$('SelectError').style.display = 'none';
   var clientId= 'ctl00_afa70b116e74462fa6d5876412c399cb_AnswersRbl';

If you could paste here what your line looks like that has "var clientId = ...........".  I'm looking to see what it looks like in the browser, when the page is up (via View Source).  It's a different value than what you have in widget.ascx since the clientId is calculated at runtime.  So I'd like to see what the runtime value is when you do a View Source.  Thanks....

Coordinator
Dec 12, 2010 at 2:06 AM

Also when you're checking the View Source, see how many "function beginVote()" exist in the View Source.

Do you have multiple polls on the page at once?  If you have multiple, I think that could cause a problem.

Dec 12, 2010 at 11:37 AM

Okay, sorry, I obviously did miss quite a bit of the source code. Here is the rest of it (it starts of right where the other left off - I double-checked to make sure I didn't miss any this time)

No I am not trying to post multiple Polls at the same time - just one little sample poll to see if I could get it to work...thanks again for your help!

<script type="text/javascript">
  function getElementsByName_iefix(tag, name) 
  {    
       var elem = document.getElementsByTagName(tag);
       var arr = new Array();
       for(i = 0,iarr = 0; i < elem.length; i++) {
            att = elem[i].getAttribute("name");
            if(att == name) {
                 arr[iarr] = elem[i];
                 iarr++;
            }
       }
       return arr;
  }
    
  function beginVote()
  {
    BlogEngine.$('SelectError').style.display = 'none';
    var clientId= '958b1e1d05df4c5dbec8620b3f2854d4_AnswersRbl';
    var clientName= clientId.replace(/_/g,"$");   
    var answerRadioButtons = getElementsByName_iefix("input", clientName);
    alert('answerRadioButtons.length = ' + answerRadioButtons.length);
    var rblDCSelection = "";


    for (var i in answerRadioButtons)
    {
        if (answerRadioButtons[i].checked)
        {
           rblDCSelection = answerRadioButtons[i].value;
           break;
        }
    }     
    
    if(rblDCSelection == "")
    {
        BlogEngine.$('SelectError').style.display = 'block';
        return false;
    }
    
    BlogEngine.$('AjaxLoader').style.display = 'block';
    BlogEngine.$('958b1e1d05df4c5dbec8620b3f2854d4_Vote_bt').style.display = 'none';
    
    var arg = rblDCSelection;
    var context = 'poll';
    WebForm_DoCallback('ctl00$958b1e1d05df4c5dbec8620b3f2854d4',arg,endVote,context,null,false);
    
    return false;
  }
  
  function endVote(arg, context)
  {
     if(arg=="true")
     {
       BlogEngine.$('ShowPoll').style.display = 'none';
       BlogEngine.$('VoteSaved').style.display = 'block';
       showResult();
     }
     else
     {
       BlogEngine.$('VotedBefore').style.display = 'block';
       showResult();
     }
  }
  
  function showResult()
  {         
    BlogEngine.$('PollForm').style.display = 'none';
    BlogEngine.$('PollResult').style.display = 'block';
    BlogEngine.$('958b1e1d05df4c5dbec8620b3f2854d4_Vote_bt').style.display = 'block';
    BlogEngine.$('AjaxLoader').style.display = 'none';
  }  
  
  function showPoll()
  {
    BlogEngine.$('PollForm').style.display = 'block';
    BlogEngine.$('PollResult').style.display = 'none';
  }  
</script>

</div></div>

Coordinator
Dec 12, 2010 at 6:24 PM

Thanks, the latest code you posted shows the problem.  My previous request didn't ask for this ... so no problem about not pasting it.  There's two possible solutions.  Try the first one first, if it doesn't work, then try the 2nd one.  If it still doesn't work (it should work though), then if you could paste here the latest HTML again from View Source ... both the part that you pasted yesterday and today.

Solution # 1:  In widget.ascx is:

var clientId= '<%=AnswersRbl.ClientID %>';

... change this to ...

var clientId= '<%=AnswersRbl.UniqueID %>';

Solution # 2:  In widget.ascx, within "getElementsByName_iefix" is this line:

if(att == name) {

... change this to ...

if(att && name.toLowerCase().indexOf(att.toLowerCase()) != -1) {

Dec 12, 2010 at 11:46 PM

Thank you Ben! It lloks like the first solution does work! This is what I get now:

do you celebrate Christmas?
Yes
 
0.0%
No
 
100.0%

Total Vote: 1
Show Pol

 


Show Pol

Jan 25, 2011 at 8:31 PM

Hi, 

I can't get that far when trying to add the poll widget.

when I click on Add

Add Poll

I get Page == null

page == null

 

 

Any hints?

Thanks!

Coordinator
Jan 26, 2011 at 6:53 AM

I'm not sure the reason why Page is null, but after you get the error while adding the Poll widget, if you refresh the browser page, do you then see the Poll widget ... or do you continue to get this same error?

This code gets called when adding the Poll, and after the Poll has been added ... every time a person visits your blog.  So just wondering if it's a problem all the time, or only when you are adding the poll.

Jan 26, 2011 at 7:53 AM

hiiiiiiiiiiiii

Jan 26, 2011 at 4:05 PM

I can refresh the page without getting an error but the poll widget is not added 

 

Coordinator
Jan 27, 2011 at 8:04 AM

I'm not sure.  Are you running BE as a normal web site project, or have you converted it to a WAP project?  Have you integrated BE in with another website ... or is it running as a standalone application?

Just trying to think of anything that could possibly cause a problem ....

Jan 27, 2011 at 4:00 PM
Edited Jan 27, 2011 at 4:43 PM

I've downloaded the VS2010 solution, didn't convert to WAP, running on Asp.net 4, slightly updated the config file to use SQL server, and I've made my own theme (not very much different from the original version I used though)

I appreciate your help, I agree the error is not very self explanatory

 

Dec 22, 2011 at 6:45 PM
Edited Dec 22, 2011 at 6:47 PM
BenAmada wrote:

Thanks, the latest code you posted shows the problem.  My previous request didn't ask for this ... so no problem about not pasting it.  There's two possible solutions.  Try the first one first, if it doesn't work, then try the 2nd one.  If it still doesn't work (it should work though), then if you could paste here the latest HTML again from View Source ... both the part that you pasted yesterday and today.

Solution # 1:  In widget.ascx is:

var clientId= '<%=AnswersRbl.ClientID %>';

... change this to ...

var clientId= '<%=AnswersRbl.UniqueID %>';

Solution # 2:  In widget.ascx, within "getElementsByName_iefix" is this line:

if(att == name) {

... change this to ...

if(att && name.toLowerCase().indexOf(att.toLowerCase()) != -1) {

I tried your solution #1 to modify my poll so that when a user voted, they would then see the bar graphs (results).. this kinda worked.. but not completely.. they now see the graphs, but if its a brand new poll and you click the vote button once, then try it again with a new user, it shows them count of 1 instead of 2, the tabulation is decreased by one..

 

any thoughts on how to fix that part?

(the only place i changed this was the very beginning of the  function beginVote()  ) 

Thanks