Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, January 9, 2020 8:36 PM
My code behind for GetCasesButton_Click have an error which I am unable to fix. I need help.
The error is on await CandidateCaseController.GetNextBAtchNumber() line.
It says **The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. **
Please help fix this.
Here is the code behind for GetCasesButton_Click
protected void GetCasesButton_Click(object sender, EventArgs e)
{
#region Required Field Validation
if (CaseNumbersTextBox.Text.Length < 1)
{
string myStringVariable = "Case number textbox cannot be empty.";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
}
if (RequestorDropDownList.SelectedItem.Value !=null)
{
ListItem requestorItem = new ListItem();
requestorItem = (ListItem)RequestorDropDownList.SelectedItem;
}
if (ReasonDropDownList.SelectedItem.Value !=null)
{
ListItem reasonItem = new ListItem();
reasonItem = (ListItem)ReasonDropDownList.SelectedItem;
}
#region Parse case number entries
string userEnteredCaseNumbers = CaseNumbersTextBox.Text;
userEnteredCaseNumbers = userEnteredCaseNumbers.Replace("\r", ",");
userEnteredCaseNumbers = userEnteredCaseNumbers.Replace("\n", ",");
while (userEnteredCaseNumbers.Contains(",,"))
userEnteredCaseNumbers = userEnteredCaseNumbers.Replace(",,", ",");
List<string> userEnteredCaseNumberList = new List<string>();
userEnteredCaseNumberList = userEnteredCaseNumbers.Split(',').Where(x => x.Length > 0).ToList();
userEnteredCaseNumberList = userEnteredCaseNumberList.Select(s => s.Trim()).ToList();
#endregion
try
{
#region Get Batch Number
int newBatchNumber = await CandidateCaseController.GetNextBatchNumber();
#endregion
#region Insert entered case numbers in database
foreach(string caseNumber in userEnteredCaseNumberList)
{
EditCandidateCaseModel newCandidate = new EditCandidateCaseModel();
newCandidate.CaseNbr = caseNumber;
//newCandidate.RequestorInfoID = requestorItem.Value;
//newCandidate.RequestorInfoID = (ListItem)RequestorDropDownList.SelectedItem.Value;
//newCandidate.ReasonID = requestorItem.Value;
newCandidate.BatchNumber = newBatchNumber;
newCandidate.EntryStaffUserName = this._loggedInUserName;
await CandidateCaseController.PostCandidate(newCandidate);
}
#endregion
}
catch (Exception ex)
{
string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "GetCasesButton_Click()", ex.Message);
MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + errorMsg + "');", true);
return;
}
}
Here is the aspx code for the button
<asp:Button ID="GetCasesButton" runat="server" Text="Get Cases" Font-Size="8.25"
BackColor="RoyalBlue" ForeColor="White" Height="24px" Width="74px"
OnClick="GetCasesButton_Click" />
All replies (3)
Thursday, January 9, 2020 8:40 PM âś…Answered
Hello,
This is the solution
protected async void GetCasesButton_Click(object sender, EventArgs e)
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
Thursday, January 9, 2020 11:19 PM
Hello,
Did adding async resolve your issue?
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
Friday, January 10, 2020 2:16 AM
Hi,
Thank you for posting here.
Karen's answer is correct, we can only use await in an async method.
You can read the Microsoft documentation to learn more about asynchronous programming, it's really useful.
Asynchronous programming with async and await
Best Regards,
Timon
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].