Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chris A 11 posts 32 karma points
    Sep 20, 2010 @ 00:21
    Chris A
    0

    Cryptic script manager error when using poll in 4.5.2

    Hi, just passing on a bit of information about using the poll package under 4.5.2.  It looks as if answers to the poll need to be initialised with 0 in the answers property.  If you don't do this, submitting the poll will fail with what looks like a javascript error raised by the asp.net ajax script manager, but what is actually a type conversion error.  I hope this helps.

  • Alex Campbell 47 posts 69 karma points
    Feb 14, 2011 @ 19:22
    Alex Campbell
    0

    Hi Chris

    Did you manage to get this to work? I'm trying to adjust the source code to sort this issue out but struggling, do you have any pearls of wisdom?

    Thanks,

    Alex

  • Chris A 11 posts 32 karma points
    Feb 14, 2011 @ 21:54
    Chris A
    0

    Hi Alex, yes, I did, by playing around with the source code - well by "playing" I mean messing about with the source code like a bull in a china shop.

    As I remember (no guarantees !) the lines of code causing a problem were those in poll.ascx.cs in methods SelectAwnser() (sic) and RenderResults() which test for properties != DBNull.Value

    For example:


    if (childDoc.getProperty("votes").Value != DBNull.Value) ...

    I replaced those tests with a less efficient one, but which works, which casts the "votes" property value to a string and tests its length. If length > 0 I've cast the string to an integer variable (making the assumption the "votes" value is going to be a number) and then used that variable further on down in the method. Basically what this does is ensure that wherever the methods expect an integer, they are going to get one, even if it's just 0, rather than a null, or zero-length string.

     


    int myvotes = 0;


    if (childDoc.getProperty("votes").Value.ToString() != ""
    )

    {

    myvotes = Convert.ToInt32(childDoc.getProperty("votes").Value);

     

     ....

    If it's going to useful to you I can send the complete modified code  - I've also pulled out the (AFAIK) redundant SQL related code.

    Cheers !

    Chris.

  • Alex Campbell 47 posts 69 karma points
    Feb 14, 2011 @ 23:19
    Alex Campbell
    0

    Star!

    Thanks Chris! It's all working nicely and saved me my own bull impression!

    For anyone else who needs it I'll post the two methods belows.

    Cheers,

    Alex

  • Alex Campbell 47 posts 69 karma points
    Feb 14, 2011 @ 23:19
    Alex Campbell
    0

    private

     

    void SelectAwnser()

    {

    System.Threading.

    Thread.Sleep(100);

     

    Document answerDoc = new Document(Convert.ToInt32(Questions.SelectedValue));

     

    int currentvotes = 0;

     

    if (answerDoc.getProperty("votes").Value.ToString() != "")

    {

    currentvotes =

    Convert.ToInt32(answerDoc.getProperty("votes").Value);

    }

     

    //if (answerDoc.getProperty("votes").Value != DBNull.Value)

     

    //{

     

    // currentvotes = (int)answerDoc.getProperty("votes").Value;

     

    //}

    currentvotes++;

    answerDoc.getProperty(

    "votes").Value = currentvotes;

     

    //insert into table

     

    try

    {

     

    if (Membership.GetUser(HttpContext.Current.User.Identity.Name) != null)

    {

     

    int memberid = (int)Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey;

     

    int pollid = answerDoc.Parent.Id;

     

    SqlConnection conn = new SqlConnection(umbraco.GlobalSettings.DbDSN);

     

    string select = "insert into tblPoll(memberid,pollid,answer) values(@memberid,@pollid,@answer)";

     

    SqlCommand comm = new SqlCommand(select, conn);

    comm.Parameters.AddWithValue(

    "@memberid", memberid);

    comm.Parameters.AddWithValue(

    "@pollid", pollid);

    comm.Parameters.AddWithValue(

    "@answer", answerDoc.Text);

    conn.Open();

    comm.ExecuteNonQuery();

    conn.Close();

    }

    }

     

    catch { }

     

    HttpCookie poll = new HttpCookie("poll" + answerDoc.Parent.Id);

    poll.Value =

    "true";

    poll.Expires =

    DateTime.Now.AddMonths(12);

    Response.Cookies.Add(poll);

     

    RenderResults();

    }

     

    private void RenderResults()

    {

    pnlResults.Visible =

    true;

    pnlQuestion.Visible =

    false;

     

    Document pollDoc = GetPollDoc();

    lblQuestion.Text = pollDoc.Text;

     

    if (_hidequestion)

    lblQuestion.Visible =

    false;

     

    if (pollDoc.getProperty("info").Value.ToString().Length > 0)

    {

    lblInfo.Text = pollDoc.getProperty(

    "info").Value.ToString();

    }

     

    int totalvotes = 0;

     

    int mostvotes = 0;

     

    int count = 0;

     

    foreach (Document childDoc in pollDoc.Children)

    {

     

    if (childDoc.Published)

    {

     

    if (childDoc.getProperty("votes").Value.ToString() != "")

     

    //if (childDoc.getProperty("votes").Value != DBNull.Value)

    {

     

    //totalvotes = Convert.ToInt32(childDoc.getProperty("votes").Value);

    totalvotes +=

    Convert.ToInt32(childDoc.getProperty("votes").Value);

     

    if (Convert.ToInt32(childDoc.getProperty("votes").Value) > mostvotes)

    {

    mostvotes =

    Convert.ToInt32(childDoc.getProperty("votes").Value);

    }

     

    }

    count++;

    }

    }

     

    Answer[] awnsers = new Answer[count];

     

    int i = 0;

     

    foreach (Document childDoc in pollDoc.Children)

    {

     

    if (childDoc.Published)

    {

     

    if (childDoc.getProperty("votes").Value != DBNull.Value)

    {

     

    double width;

     

    if (WidthTotalVotes)

    {

    width = CalcPercentage(

    Convert.ToInt32(childDoc.getProperty("votes").Value), totalvotes, 0);

    }

     

    else

    {

    width = CalcPercentage(

    Convert.ToInt32(childDoc.getProperty("votes").Value), mostvotes, 0);

    }

    awnsers[i] =

    new Answer(childDoc.Text, Convert.ToInt32(childDoc.getProperty("votes").Value), CalcPercentage(Convert.ToInt32(childDoc.getProperty("votes").Value), totalvotes,1), width);

     

    }

     

    else

    {

    awnsers[i] =

    new Answer(childDoc.Text, 0,0,0);

    }

    i++;

    }

    }

     

    if (SortResults)

    {

     

    Array.Sort(awnsers, 0, awnsers.Length, new AnswerComparer());

    }

    Literal3.Text =

    string.Empty;

     

    bool first = false;

     

    for (int j = 0; j < awnsers.Length; j++)

    {

     

    if (awnsers[j].votes == mostvotes)

    {

    first =

    true;

    }

     

    else

    {

    first =

    false;

    }

    Literal3.Text += RenderAwnser(awnsers[j].ToString(), awnsers[j].percentage, awnsers[j].votespercentage, first);

     

    }

     

    }

  • Tony Bolton 83 posts 109 karma points
    Feb 15, 2011 @ 00:10
    Tony Bolton
    0

    Cheers chaps - literally the moment I needed to download this to start using, you guys go and fix a bug before I get started!

    Thanks for posting the solution, that probably saved me a couple of hours of aggro! :)

  • Tony Bolton 83 posts 109 karma points
    Feb 15, 2011 @ 01:07
    Tony Bolton
    0

    Just expanding on Alex and Chris' excellent work, I've instead opted for parsing the value which also seems to sort out the problem :

            private void SelectAwnser()
    {
    System.Threading.Thread.Sleep(100);

    Document answerDoc = new Document(Convert.ToInt32(Questions.SelectedValue));
    int currentvotes = 0;
    if (answerDoc.getProperty("votes").Value == DBNull.Value || Int32.TryParse(answerDoc.getProperty("votes").Value.ToString(),out currentvotes) == false)
    {
    currentvotes = 0;
    }

    currentvotes++;
    answerDoc.getProperty("votes").Value = currentvotes;

    //insert into table
    try
    {
    if (Membership.GetUser(HttpContext.Current.User.Identity.Name) != null)
    {
    int memberid = (int)Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey;
    int pollid = answerDoc.Parent.Id;

    SqlConnection conn = new SqlConnection(umbraco.GlobalSettings.DbDSN);
    string select = "insert into tblPoll(memberid,pollid,answer) values(@memberid,@pollid,@answer)";

    SqlCommand comm = new SqlCommand(select, conn);

    comm.Parameters.AddWithValue("@memberid", memberid);
    comm.Parameters.AddWithValue("@pollid", pollid);
    comm.Parameters.AddWithValue("@answer", answerDoc.Text);
    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
    }
    }
    catch { }

    HttpCookie poll = new HttpCookie("poll" + answerDoc.Parent.Id);
    poll.Value = "true";
    poll.Expires = DateTime.Now.AddMonths(12);
    Response.Cookies.Add(poll);


    RenderResults();
    }

    private void RenderResults()
    {
    pnlResults.Visible = true;
    pnlQuestion.Visible = false;

    Document pollDoc = GetPollDoc();
    lblQuestion.Text = pollDoc.Text;
    if (_hidequestion)
    lblQuestion.Visible = false;

    if (pollDoc.getProperty("info").Value.ToString().Length > 0)
    {
    lblInfo.Text = pollDoc.getProperty("info").Value.ToString();
    }

    int totalvotes = 0;
    int mostvotes = 0;
    int count = 0;
    int test = 0;

    foreach (Document childDoc in pollDoc.Children)
    {
    test = 0;

    if (childDoc.Published)
    {
    if (childDoc.getProperty("votes").Value != DBNull.Value)
    {

    if (Int32.TryParse(childDoc.getProperty("votes").Value.ToString(), out test) == true)
    {
    totalvotes += test;
    }


    if (test > mostvotes)
    {
    mostvotes = test;
    }
    }

    count++;
    }
    }

    Answer[] awnsers = new Answer[count];

    int i = 0;

    foreach (Document childDoc in pollDoc.Children)
    {
    test = 0;

    if (childDoc.Published)
    {
    if (Int32.TryParse(childDoc.getProperty("votes").Value.ToString(), out test) == false)
    {
    test = 0;
    }

    if (childDoc.getProperty("votes").Value != DBNull.Value)
    {
    double width;

    if (WidthTotalVotes)
    {
    width = CalcPercentage(test, totalvotes, 0);
    }
    else
    {
    width = CalcPercentage(test, mostvotes, 0);
    }
    awnsers[i] = new Answer(childDoc.Text, test, CalcPercentage(test, totalvotes,1), width);

    }
    else
    {
    awnsers[i] = new Answer(childDoc.Text, 0,0,0);
    }

    i++;

    }
    }

    if (SortResults)
    {
    Array.Sort(awnsers, 0, awnsers.Length, new AnswerComparer());
    }

    Literal3.Text = string.Empty;
    bool first = false;
    for (int j = 0; j < awnsers.Length; j++)
    {
    if (awnsers[j].votes == mostvotes)
    {
    first = true;
    }
    else
    {
    first = false;
    }

    Literal3.Text += RenderAwnser(awnsers[j].ToString(), awnsers[j].percentage, awnsers[j].votespercentage, first);

    }

    }
  • Chris A 11 posts 32 karma points
    Feb 15, 2011 @ 02:11
    Chris A
    0

    Thanks Tony !  Incidentally, I had come back to making this component work after having tried out Contour for poll functionality.  There was nothing wrong with the Contour option, but I appreciate the simplicity/hackability of Tim's Poll user control, and also like the fact the poll questions (and answer statistics) exist as nodes under the poll - which makes it a better "fit" for teaching to my end users.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies