1

I am getting errors on variables FaxPro, EmailPro, FaxStat, and EmailStat.

while (reader.Read())
{
    string CustNo = reader["CUSTNO"].ToString();
    string Phone = reader["PHONE"].ToString();
    string Fax = reader["FAX"].ToString();
    string Email = reader["PRI_EMAIL"].ToString();
    string Type = reader["TYPE"].ToString();

    if (Type.Contains("H"))
    {
        if (Type.Contains("F"))
        {
            string FaxStat = "Y";
            string FaxPro = "PENDING";
        }
        else
        {
            string FaxStat = "N";
            string FaxPro = "NONE";
        }
        if (Type.Contains("E"))
        {
            string EmailStat = "Y";
            string EmailPro = "PENDING";
        }
        else
        {
            string EmailStat = "N";
            string EmailPro = "NONE";
        }
//outbox
// id, account, type, title, number, fax, email, faxpro, emailpro, faxstat, emailstat, filepath, datesent

        MySqlCommand mycommand = new MySqlCommand("INSERT INTO outbox (id, account, type, title, number, fax, email, faxpro, emailpro, faxstat, emailstat, filepath, datesent) VALUES('0','" + CustNo + "', 'CUSTOMER', 'test', '" + Phone + "', '" + Fax + "', '" + Email + "', '" + FaxPro + "', '" + EmailPro + "', '" + FaxStat + "', '" + EmailStat + "', 'test', NOW())", conn);
  mycommand.ExecuteNonQuery();

Errors are:

The name 'FaxPro' does not exist in the current context C:...\Form2.cs

... and so on for EmailPro, FaxStat, and EmailStat.

2
  • 3
    Your variables are out of scope - they only exist between the {braces} in which they were declared. Please read up on variable scopes Commented Nov 18, 2011 at 22:39
  • You should lowercase your variable names. Commented Nov 18, 2011 at 22:43

3 Answers 3

3

Declare your strings in the beginning of your function, so they have scope all throughout. At the moment, you're declaring FaxPro, EmailPro, FaxStat, EmailStat inside your if/else statement blocks, and once that block ends, they go out of scope.

By declaring them once at the beginning of your function you will avoid declaring them multiple times in your while loop.

//small example
public void myFunc()
{
    string CustNo, Phone, Fax, Email, Type, FaxStat, FaxPro, EmailStat, EmailPro;

    //set up query and reader
    //...
    while(reader.read())
    {
         CustNo = reader["CUSTNO"].ToString();
         //etc.
    }
    //reader.close(); conn.close();
}
Sign up to request clarification or add additional context in comments.

Comments

0

The declaration of the variables is out of scope. They are declared in the if statement. Move the declaration to an outer scope like that:

while (reader.Read()) 
{ 
    string CustNo = reader["CUSTNO"].ToString(); 
    string Phone = reader["PHONE"].ToString(); 
    string Fax = reader["FAX"].ToString(); 
    string Email = reader["PRI_EMAIL"].ToString(); 
    string Type = reader["TYPE"].ToString(); 

    string FaxStat = string.Empty;
    string FaxPro = string.Empty;
    string EmailStat = string.Empty; 
    string EmailPro = string.Empty; 
    if (Type.Contains("H")) 
    { 
        if (Type.Contains("F")) 
        { 
            FaxStat = "Y"; 
            FaxPro = "PENDING"; 
        } 
        else 
        { 
            FaxStat = "N"; 
            FaxPro = "NONE"; 
        } 
        if (Type.Contains("E")) 
        { 
            EmailStat = "Y"; 
            EmailPro = "PENDING"; 
        } 
        else 
        { 
            EmailStat = "N"; 
            EmailPro = "NONE"; 
        } 
        //outbox 
        // id, account, type, title, number, fax, email, faxpro, emailpro, faxstat, emailstat, filepath, datesent 

        MySqlCommand mycommand = new MySqlCommand("INSERT INTO outbox (id, account, type, title, number, fax, email, faxpro, emailpro, faxstat, emailstat, filepath, datesent) VALUES('0','" + CustNo + "', 'CUSTOMER', 'test', '" + Phone + "', '" + Fax + "', '" + Email + "', '" + FaxPro + "', '" + EmailPro + "', '" + FaxStat + "', '" + EmailStat + "', 'test', NOW())", conn); 
        mycommand.ExecuteNonQuery(); 

Comments

0
while (reader.Read())
{
    string CustNo = reader["CUSTNO"].ToString();
    string Phone = reader["PHONE"].ToString();
    string Fax = reader["FAX"].ToString();
    string Email = reader["PRI_EMAIL"].ToString();
    string Type = reader["TYPE"].ToString();
    // Declare your variables here
    string FaxStat, FaxPro, EmailStat, EmailPro;

    if (Type.Contains("H"))
    {
        if (Type.Contains("F"))
        {
            FaxStat = "Y";
            FaxPro = "PENDING";
        }
        else
        {
            //...
            //...

2 Comments

You shouldn't redeclare the variables. string FaxState = "Y";
Thank you! I started out using PHP. Which I think allows you to get away with a lot. But, C# is forcing me to learn programming the way I should have from the beginning.

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.