0

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.

4
  • On which line do you get the error message from the above code? Commented Nov 22, 2019 at 8:50
  • Hi Steve. I don't get error on the code given above. All I want to do is to use string CODE in another class, but whenever the code passes WHILE loop, string CODE is not returned, it just dissapears :( Commented Nov 22, 2019 at 8:57
  • You are returning it, so its value doesn't disappear, you just need to get the return value from TESTCODE and assign to another local variable. Like string ANewCODE = TESTCODE(); if(!string.IsNullOrEmpty(ANewCode)) {.....} Commented Nov 22, 2019 at 9:07
  • By the way, if the compiler gives you an error, it tells you also where the error happens. If you don't post the code where do you have the error then your questions fills exactly the close reason No minimal reproducible example given Commented Nov 22, 2019 at 9:10

3 Answers 3

0

SharpLab has no support for the SQL Classes, but those should not mater anyway. I punched this code into SharpLab and it compiles, aside from the expected warning (unreachable return null):

using System;

public class C {
    public void M() {
    }

    public static string TESTCODE()
    {
        while(true){
          string CODE;
          CODE = "not a null";
          return CODE ;
        }

        return null;
    }
}

That leaves only one conclusion: This is not the line causing the issue.

Something else has tripped up the compiler. And it tripped it up so badly, it can not even tell you where the issue is anymore. Please fix all the other issues in your code.

Sign up to request clarification or add additional context in comments.

Comments

-1

You should define string CODE; before while loop, not inside.

11 Comments

While it is not a bad general advice, it does not explain this Compiler error in the least.
I tried that way as well, but then I get error Use of unassigned local variable CODE
@Christopher - it explains "I've also tried to move return CODE part outside of the while function braces". If only return was moved, than that's the error.
Also, if you want to return at first loop why use while at all?
compiler tells you exacly what is problem - it's a matter of range of variable. variables defined inside loops, usings, if's etc... can be seen only INSIDE them, not outside. you need to define them outside. then, when defining it outside you simply need to initialize it someway = maybe var CODE = string.Empty;
|
-1

as i mentioned in comment - you should read more about ranges of variables. the one defined inside lambda, whiles, usings, ifs etc... are accesible only there, not outside. this is why if you want to have them outside - vou need to define them outside. also you need to somehow initialize it. try following code:

using (MySqlDataReader rdr = cmd.ExecuteReader())
{
   string CODE = string.Empty;
   while (rdr.Read())
   {
      string result= rdr.GetString(0);
      CODE = Regex.Match(result, @"\d+").Value;
   }
   return CODE ;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.