This question is related 2 of my previous questions which have been posted on StackOverflow in last few days.
1st Question which was regarding a MySqlClient EndOfStreamException.
The answer for the 1st question leads me to Switch libraries from MySQL ADO.NET to MySQL connector.
I had some issues with some MySQL keywords since moving from MySQL ADO.NET to MySQL connector. I got the answer to that question as well.
Now the program is running without any errors. But unfortunately, there is a catch since the data is not written to the database whatsoever.
With the intent of finding a solution to this issue I tried to add "await" keyword to ExecuteNonQueryAsync() as of the below code.
private bool OpenConnection()
{
try
{
//connection.Open(); //line commented
**await connection.OpenAsync();**
Console.WriteLine("MySQL connected.");
return true;
}
catch (MySqlException ex)
{
}
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:\Users\Admin\source\Bargstedt.csv"))
{
//List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
***await reader.OpenAsync();***
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO jobs"
+ "(nJobNumber,strClientReference,datPromisedDelivery)"
+ "VALUES (@jobNo, @strClientName, @strClientReference)";
var cmd = new MySqlCommand();
cmd.CommandText= querynew;
cmd.Parameters.AddWithValue("strClientName", "MySqlDbType.VarChar").Value =(values[1]);
cmd.Parameters.AddWithValue("strClientReference", "MySqlDbType.VarChar").Value = values[2];
***await cmd.ExecuteNonQueryAsync();***
// cmd.ExecuteNonQueryAsync(); //Line commented
}
}
this.CloseConnection();
}
}
Before implemented this, I reckoned the issue was with the execution line since any of the data isn't written back to the database.
That is the reason it persuades me to include await open and execute lines to the source code likewise given in this example.
Unfortunately, in all the places I have used await keyword (Highlighted in the above code) is triggering an error:
Error CS4032 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'.
How to fix this issue and what do you think the reason which the database is not updating?
would it be fixed if this 'await' method implemented correctly?