Error when adding and editing a new option to a question in surveys

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
6/9/2012 7:21:50 AM
Gravatar
Total Posts 52

Error when adding and editing a new option to a question in surveys

I have found that in a survey question you create a new option, edit it changing the name when trying to post you get an error, that also happens if you try to add, edit and delete an option. This happens because for posting and deleting options the applications compares the list text and value properties assuming that if they are not equal the are already existing options with a valid guid in the value property. A fix for this will be to check if the property value contains a valid guid, if not assume is a new item. The SaveQuestionOption could be like this:

        private void SaveQuestionOptions(Question question)
        {
            QuestionOption option;
            int order = 0;
            Guid optionGuid;
            // Save options
            foreach (ListItem item in lbOptions.Items)
            {
                if (Guid.TryParse(item.Value, out optionGuid))
                {
                    option = new QuestionOption();
                }
                else
                {
                    option = new QuestionOption(optionGuid);
                }
                option.Answer = item.Text;
                option.QuestionGuid = question.QuestionGuid;
                option.Order = order++;
                option.Save();
            }
        }

and the BtnDeleteOption_Click like

        private void BtnDeleteOption_Click(Object sender, ImageClickEventArgs e)
        {
            Guid optionGuid;
           if (lbOptions.SelectedItem == null) return;
            if (Guid.TryParse(lbOptions.SelectedItem.Value, out optionGuid))
            {
                List<Guid> deletedOptions;
                deletedOptions = (List<Guid>)ViewState[Deleted_Options_Key] ?? new List<Guid>();
                //store the removed item in viewstate to remove from storage when saved
                deletedOptions.Add(optionGuid);
                ViewState.Add(Deleted_Options_Key, deletedOptions);
            }
            lbOptions.Items.Remove(lbOptions.SelectedItem);
        }

This will work for ASP.NET 4.0 since the Guid.TryParse function is not available in 3.5. For both version we can declare a helper function as:

        public static bool TryParse(string input, out Guid result)
        {
#if NET35
            try
            {
                result = new Guid(input);
                return true;  
            }
            catch (Exception ex)
            {
                result = Guid.Empty;
                return false;
            }
#endif
            return Guid.TryParse(input, out result);
        }

 

6/9/2012 12:08:05 PM
Gravatar
Total Posts 52

Re: Error when adding and editing a new option to a question in surveys

There is an error in the SaveQuestionOptions, the evaluation of the guid should be negated

        private void SaveQuestionOptions(Question question)
        {
            QuestionOption option;
            int order = 0;
            Guid optionGuid;
            // Save options
            foreach (ListItem item in lbOptions.Items)
            {
                if (!Guid.TryParse(item.Value, out optionGuid))
                {
                    option = new QuestionOption();
                }
                else
                {
                    option = new QuestionOption(optionGuid);
                }
                option.Answer = item.Text;
                option.QuestionGuid = question.QuestionGuid;
                option.Order = order++;
                option.Save();
            }
        }

6/11/2012 7:18:17 AM
Gravatar
Total Posts 18439

Re: Error when adding and editing a new option to a question in surveys

Thanks! I will make these changes and push them to the source code repository later today.

Best,

Joe

6/12/2012 7:56:36 AM
Gravatar
Total Posts 108
Community Expert

Re: Error when adding and editing a new option to a question in surveys

Hi Joe could you please post here when the help function for .net 3.5  would be implemented

6/12/2012 8:21:11 AM
Gravatar
Total Posts 18439

Re: Error when adding and editing a new option to a question in surveys

Hi German,

I don't understand your question. It seems unrelated to this thread. Please clarify.

Best,

Joe

 

6/12/2012 8:57:01 AM
Gravatar
Total Posts 108
Community Expert

Re: Error when adding and editing a new option to a question in surveys

Hi Joe I explain my self more, I just download the last code from the repository and the .net 3.5 version don't compile, the error was the Guide.TryParse function because don't exist in this version, reading this post I see that cecheto with the change that propose include a TryParse helper function in case the code was compile in .net 3.5

My question was related with the implementation of this function or any other work around that make the 3.5 code compile 

6/12/2012 9:50:48 AM
Gravatar
Total Posts 18439

Re: Error when adding and editing a new option to a question in surveys

Thanks! This is now fixed in the source code repository.

Best,

Joe

You must sign in to post in the forums. This thread is closed to new posts.