Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
How to fix below error?
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<system.web.mvc.actionresult>'

Am getting above error. I really don't have any idea about this. Could anyone help me on this?
My code,
C#
[HttpPost]
     public ActionResult Createaccount(Register reg)
     {
         if (ModelState.IsValid)
         {
             var client = new MongoClient();
             var db = client.GetDatabase("foo");

             var collection = db.GetCollection<Register>("people");
             var user = new Register { fname = reg.fname, lname = reg.lname, age = reg.age, pwd = reg.pwd, email = reg.email };

             await collection.InsertOneAsync(user);
             TempData["msg"] = "sucess";
             ModelState.Clear();
             return Redirect("/angular/createaccount");

         }

         return View();
     }


If anybody has any idea please share it.
Posted

You Need to include "async" identifier in your method signature. MSDN is your friend:
https://msdn.microsoft.com/en-us/library/hh191443.aspx[^]
 
Share this answer
 
Comments
Am Gayathri 10-Apr-15 3:08am    
Thanks
// it will work if your .net framework would be 4 or 4.5 or upper

[HttpPost]
public async Task<ActionResult> Createaccount(Register reg)
{
if (ModelState.IsValid)
{
var client = new MongoClient();
var db = client.GetDatabase("foo");
var collection = db.GetCollection<register>("people");
var user = new Register { fname = reg.fname, lname = reg.lname, age = reg.age, pwd = reg.pwd, email = reg.email };

await collection.InsertOneAsync(user);
TempData["msg"] = "sucess";
ModelState.Clear();
return Redirect("/angular/createaccount");

}

return View();
}
 
Share this answer
 
v2
Comments
Am Gayathri 10-Apr-15 3:08am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900