Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I have an error when I am setting my flag in my program code. I set the first flag and no error but when i set the second flag I get the error, "
The name 'con76' does not exist in the current context
". I do not know why I am getting this error. Here is my code:

C#
if (AMMC_MATCHED == true)
            {
                //CALL CALCULATION FUNCTIONS

                //Calculate Base Salary Percent
                BASE_PERCENT = 0;
                CALC_BASE_PERCENT(BASE_SALARY, AAMC_MIN, AAMC_MID, AAMC_MAX);

                //Calculate Total Salary Percent
                TOTAL_PERCENT = 0;
                CALC_TOTAL_PERCENTAGE(TOTAL_SALARY, AAMC_MIN, AAMC_MID, AAMC_MAX);

                //Calculate New Salary based on Percentage passed
                NEW_BASE_SAL = 0;
                CALC_BASE_SAL(DEFAULT_PERCENTILE, AAMC_MIN, AAMC_MID, AAMC_MAX);
                NEW_TOT_SAL = NEW_BASE_SAL + SUPPLIMENT;

                //Calculate New Percentage Based on new Total Salary
                NEW_TOT_PCT = 0;
                CALC_TOTAL_PERCENTAGE2(NEW_TOT_SAL, AAMC_MIN, AAMC_MID, AAMC_MAX);

                SALARY_ADJ = NEW_BASE_SAL - BASE_SALARY;


                //UPDATE EMPLOYEE BASE DATA Table with New percentages and calculated salaries for current person

                SqlConnection con76 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PERCENTAGE_CALC_DB"].ConnectionString);
                con76.Open();
                
                SqlCommand scmd6 = new SqlCommand("UPDATE EMPLOYEE_BASE_DATA SET CURRENT_BASE_PCT = '" + BASE_PERCENT + "', CURRENT_TOTAL_PCT = '" + TOTAL_PERCENT + "', REQUESTED_PCT = '" + DEFAULT_PERCENTILE + "', SALARY_ADJUSTMENT = '" + SALARY_ADJ + "', NEW_TOTAL_PCT = '" + NEW_TOT_PCT + "', SALARY_FOR_REQ_PCT = '" + NEW_BASE_SAL + "', X_CURRENT = '" + BASE_SALARY + "', PROPOSED_X = '" + NEW_BASE_SAL + "', Y_SUPPLEMENT = '" + SUPPLIMENT + "', Z_INCENTIVE = '" + SALARY_ADJ + "', TOTAL_COMPENSATION = '" + TOTAL_SALARY + "', CURRENT_TOTAL_COMP = '" + TOTAL_SALARY + "', MATCHED = 1 WHERE MNUMBER = '" + MNUMBER + "' AND SCENARIO = '" + TextBoxSCENARIO.Text + "'", con76);
                scmd6.ExecuteNonQuery();
                

            }
            else if (AMMC_MATCHED == false)
            {
                
                SqlCommand scmd67 = new SqlCommand("UPDATE EMPLOYEE_BASE_DATA SET MATCHED = 0 WHERE MNUMBER = '" + MNUMBER + "' AND SCENARIO = '" + TextBoxSCENARIO.Text + "'", con76);
                scmd67.ExecuteNonQuery();
                
            }


What I have tried:

I have tried to set another connection and it seems to clear the error but I should not have to do that since this is just one connection open.
Posted
Updated 15-Apr-20 4:18am

Quote:
How to fix error, "the name 'con76' does not exist in the current context"?

You need to study what is the scope of a variable.
C#
if () {
    // what is created here
}
else {
    // does not exist here
}

C#
// what is created here
if () {
    // exist here
}
else {
    // and here
}

-----
C#
SqlCommand scmd6 = new SqlCommand("UPDATE EMPLOYEE_BASE_DATA SET CURRENT_BASE_PCT = '" + BASE_PERCENT + "', CURRENT_TOTAL_PCT = '" + TOTAL_PERCENT + "', REQUESTED_PCT = '" + DEFAULT_PERCENTILE + "', SALARY_ADJUSTMENT = '" + SALARY_ADJ + "', NEW_TOTAL_PCT = '" + NEW_TOT_PCT + "', SALARY_FOR_REQ_PCT = '" + NEW_BASE_SAL + "', X_CURRENT = '" + BASE_SALARY + "', PROPOSED_X = '" + NEW_BASE_SAL + "', Y_SUPPLEMENT = '" + SUPPLIMENT + "', Z_INCENTIVE = '" + SALARY_ADJ + "', TOTAL_COMPENSATION = '" + TOTAL_SALARY + "', CURRENT_TOTAL_COMP = '" + TOTAL_SALARY + "', MATCHED = 1 WHERE MNUMBER = '" + MNUMBER + "' AND SCENARIO = '" + TextBoxSCENARIO.Text + "'", con76);

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
v3
C#
// define your connection outside the if

SqlConnection con76 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PERCENTAGE_CALC_DB"].ConnectionString);

if (AMMC_MATCHED == true)
{
    // rest of code
}
else if (AMMC_MATCHED == false)
{
    // con76 is not available here too
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900