I'm trying to parse data from MariaDB MySQL database, but I have a problem when I try to return CODE. The public static string given below works perfectly, I just want to use parsed data in another class, but whenever the code passes while (rdr.Read()) function, string OTP just dissapears.
I've also tried to move return CODE part outside of the while function braces, but then I'm getting the following error:
"The name 'CODE' does not exist in the current context".
My code:
public static string TESTCODE()
{
string connStr = "server=XX.XX.XX.XX;user=username;database=dbname;password=password;";
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
string query = "Select * from testbase where name='George' and lastname='Brown' order by number desc limit 1";
MySqlCommand cmd = new MySqlCommand(query, conn);
using (MySqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
string result= rdr.GetString(0);
string CODE;
CODE = Regex.Match(result, @"\d+").Value;
return CODE ;
}
}
return null;
}
Thank you in advance.