Q:

Unassigned local variable when passing integer from database to another form in C#

I am trying to pass an integer into another form in C#.  After checking the user's login credentials, I want to get is ID and then pass it into another form.  However, I get an error saying that the value I'm trying to pass is an unassigned local variable.  Here's my code:
FORM 1
private void button1_Click(object sender, EventArgs e)
{
    if (u.Text == "" || p.Text == "")
    {
        MessageBox. Show("Please enter username and password", "Error", MessageBoxButtons. OK, MessageBoxIcon. Error);
    }

    else
    {
        try 
        {
            // Query for registration
            string view = "SELECT * FROM person WHERE username = '" + u.Text + "' AND password = '" + p.Text + "';";

            // Start SQL connection
            MySqlConnection myConn = new MySqlConnection(connectionString);
            MySqlCommand myCommand = new MySqlCommand(view, myConn);
            MySqlDataAdapter da = new MySqlDataAdapter(myCommand);
            MySqlDataReader myReader;

            myConn. Open();
            myReader = myCommand. ExecuteReader();

            if (myReader. HasRows)
            {
                int id;
                while (myReader. Read())
                {
                    id = myReader. GetInt32("person_id");
                    //person_id = myReader. GetInt("person_id");
                    MessageBox. Show(id. ToString());

                }
                myConn. Close();

                // Create instance of the form
                Form3 dash = new Form3(id); //unassigned local variable id
                dash. Show(); // Show the form
                this. Hide(); // Hide this current form
            }

            else
            {
                MessageBox. Show("Incorrect username or password", "Error", MessageBoxButtons. OK, MessageBoxIcon. Error);
            }
        }
        catch (Exception ex)
        {
            MessageBox. Show(ex. Message);
        }
    }

}

FORM 3
public partial class Form3 : Form
{
    int final_id;
    public Form3(int id)
    {
        InitializeComponent();
        assignGlobal(id);
    }

    public void assignGlobal(int b)
    {
        final_id = b;
        MessageBox. Show(final_id. ToString());
    }

}

I have already imported using MySql. Data. MySqlClient.  I really cannot figure out why it shows that error.  Can someone please tell me where I went wrong?  What should I change?  Your help will be much appreciated.  Thank you so much. 

A:

You while loop might not execute if the reader is empty.  So you need to explicitly assign a default value for the id:
int id = 0;

Or you need to handle that case using an if else statement.  
Also if you expect one value only, i suggest that you use ExecuteScalar instead of a ExecuteReader . 
You need to ask yourself a question here : What should my program do if the reader doesn't return any value ? 