Error coming while submitting values to Form

Posted by Community Admin on 04-Aug-2018 13:53

Error coming while submitting values to Form

All Replies

Posted by Community Admin on 09-Apr-2015 00:00

Hi,

We have created a custom webform to store values in a "Form" of Sitefinity but it gives an error when two concurrent request comes for saving data back to "Form" of Sitefinity.

Below is the code which we have written to save information:

using (FormsManager manager = new FormsManager())
        
            manager.Provider.SuppressSecurityChecks = true;
 
            var form = manager.GetFormByName("sf_graduateform");
 
            if (form != null)
            
                FormEntry entry = manager.CreateFormEntry(String.Format("0.1", manager.Provider.FormsNamespace, form.Name));
 
                entry.SetValue("FormTextBox_C001", FirstName.Text);
                entry.SetValue("FormTextBox_C002", LastName.Text);
                entry.SetValue("FormTextBox_C003_0", Email.Text);
                entry.SetValue("FormTextBox_C004_0", Telephone.Text);
                entry.SetValue("FormDropDownList_C005", CountryOfResidence.SelectedItem.Value);
                entry.SetValue("FormMultipleChoice_C006", legal_yes.Checked ? "Yes" : "No");
 
                if (UploadDocumentation.HasFile)
                
                    //check filesize
                    const int maxFileSize = 5242880;
                    int fileSize = UploadDocumentation.PostedFile.ContentLength;
 
                    if (fileSize > maxFileSize)
                    
                        UploadSizeWarning.Text = "The file should be under 5MB";
                        return;
                    
                
 
                string providerName = "SystemLibrariesProvider";
                LibrariesManager librariesManager = new LibrariesManager(providerName);
                var document = librariesManager.GetDocuments().ToList().FirstOrDefault(d => d.Title == UploadDocumentation.FileName && !d.IsDeleted);
 
                document = UploadPDF(document, librariesManager);
 
                entry = AddLinkToResponse(document, entry);
 
                entry.SetValue("FormParagraphTextBox_C008", additional_links.Text);
 
                entry.IpAddress = SystemManager.CurrentHttpContext.Request.UserHostAddress;
                entry.SubmittedOn = DateTime.UtcNow;
                //entry.UserId = ClaimsManager.GetCurrentUserId();
 
                form.FormEntriesSeed++;
                entry.ReferralCode = form.FormEntriesSeed.ToString(CultureInfo.CurrentUICulture.Name);
                //
                manager.SaveChanges();
            

The issue is that when we write "form.FormEntriesSeed++;" and when we get concurrent requests it crash down as per the attached screenshot (PFA: error_while_updating.png).

We checked it after removing above code but as per the link docs.sitefinity.com/for-developers-create-form-responses it is written that we must write above given code.

One thought in our mind is that we can apply the lock on the whole code given above but we are very unsure whether it is proper way or not.

Please provide your kind inputs in this matter.

Regards,
Harshil Shukla

Posted by Community Admin on 14-Apr-2015 00:00

Hello Harshil,

We have just replied in the ticket you have opened related to this issue. I am pasting here the suggestions from the ticket reply for your convenience:

I came up with two possible solutions for your case:

1. The first one would be to first create the form entry, increment the seed and set the referral and save changes. After that, process the form upload and associate the uploaded file with the form entry.

2. The second approach would be to leave your code as is and first create the entry without incrementing the seed, and save the changes. This will ensure that the form entry has been created ok and after that, use a try catch to increment the seed and set the referral:http://screencast.com/t/We7qsu1cQ. With this approach, the entry will be created as expected always and the end user will not a get an error under any circumstances. In case there is a problem when the seed is updated, the error will be handled and the entry will simply appear without a number in the backend.


Regards,
Sabrie Nedzhip
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed