How do I fix error await operator can only be used within an async method?

  • Thread starter Thread starter winkimjr2
  • Start date Start date
W

winkimjr2

Guest
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" />

Continue reading...
 
Back
Top