Error coming while submitting values to Form
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();
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