0

I'm trying to make a database browser in a C# console application.

Now I have a piece of code that checks the login data for the MySql Server, but it is wrapped in a while loop. That means the variable basicConn inside the while loop is inaccessible, so I can't use it outside the loop. Now I declared that variable before the loop and filled inside the loop, but after the loop I want to use the connection but I get the error: " Use of unassigned local variable `basicConn' ".

Here is my piece of code:

//Check voor het inloggen om uit de whileLoop te komen; bool check1 = false; //Er mogen maximaal 3 foute login acites gemaakt worden; int fails = 0;

MySqlConnection basicConn;

Console.WriteLine ("Log hier in om verbinding te maken met de MySql server.\n");

//Blijf alles wat in deze whileLoop staat herhalen tot het goed is gegaan;
while (check1 == false)
{
    //Als er 3 fouten zijn gemaakt met inloggen wordt het programma gesloten;
    if (fails < 3 && fails >= 0)
    {
        Console.WriteLine ("Poging {0}", fails + 1);

        //Hostnaam;
        string host = "";
        Console.Write ("Hostname: ");
        host = Console.ReadLine ();

        //Gebruikersnaam;
        string gebruikersnaam = "";
        Console.Write ("Gebruikersnaam: ");
        gebruikersnaam = Console.ReadLine ();

        //Wachtwoord;
        string wachtwoord = "";
        Console.Write ("Wachtwoord: ");
        wachtwoord = Console.ReadLine ();

        //MySql Connectie basis;
        string connString = "Server=" + host + ";Uid=" + gebruikersnaam + ";Password=" + wachtwoord + ";";
        basicConn = new MySqlConnection (connString);

        try
        {
            //MySQl Connectie maken;
            basicConn.Open ();

            Console.WriteLine ("Connectie is gemaatk!");

            check1 = true;
        }
        catch (Exception)
        {
            Console.WriteLine ("Er is iets misgegaan met een connectie maken, probeer het opnieuw.\n\n");

            check1 = false;

            fails += 1;
        }
    }
    else
    {
        Console.WriteLine ("Je aantal pogingen zijn mislukt!\nHet programma wordt gesloten.");

        //Laat het programma 1.5 seconden wachten tot het verdergaat;
        Thread.Sleep (1500);
        //Laat het programma sluiten;
        Environment.Exit (1);
    }
}


//          ===============================================================================================================================


MySqlCommand db = basicConn.CreateCommand ();
db.CommandText = "SHOW DATABASES";
MySqlDataReader Read_db;
Read_db = db.ExecuteReader ();
while (Read_db.Read()) {
    string row = "";
    for (int i = 0; i < Read_db.FieldCount; i++)
        row += Read_db.GetValue (i).ToString ();
    Console.WriteLine (row);
}
Read_db.Close ();
  • 1
    Possible duplicate of [C# Use of unassigned local variable](http://stackoverflow.com/questions/5710485/c-sharp-use-of-unassigned-local-variable) – Stephen Ross Mar 17 '16 at 13:51

2 Answers2

1

Change first line to:

MySqlConnection basicConn = null;

And don't forget to check if it's still null after the loop.

Surfin Bird
  • 488
  • 7
  • 16
  • This worked! thx ;) Sorry for late answer, stackoverflow didn work 100% here –  Mar 22 '16 at 12:36
0

need to declare it with a default value outside the loop

MySqlConnection basicConn = new MySqlConnection()

or equivalent. Basically the compiler doesn't know whether it is set within the loop and so it thinks it might need to use the value set outside the loop. In your case, it hasn't been set, hence the error.

C. Knight
  • 739
  • 2
  • 5
  • 20