How to reference a variable (variable does not exist in the current context) in a USING clause in C#?

I am developing a C# application in VS2010.

In the following code, how could I resolve the error:

Error      1      The name 'banknum' does not exist in the current context      \\msad\root\na\ny\users\zimmermj\visual studio 2010\Projects\SMRWebForm\SMRWebForm\SMRWebForm1.aspx.cs      71      29      SMRWebForm
-------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace SMRWebForm
{
    public partial class SMRWebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> ReadFile = File.ReadAllLines(@"U:\SnrMgtRpt\OI_DRS.txt").ToList();

            foreach (string line in ReadFile)
            {                
                string banknum = line.Substring(0, 4);
                string transDtYR = line.Substring(4, 4);
                string transDtMnth = line.Substring(8, 2);                
            }

            const string myInsert = "INSERT INTO tbl_SMR_OI_WRS (bankNum, transYR, transMnth, transDay, clientAcct, transType, recordNum, recordNum, processYr) VALUES ('{0}', {1}, {2}, {3}, '{4}', '{5}', '{6}', {7}, '{8}')";
           
            const string connString = "Data Source=FSMSAPPSSQLDEV,7003;Initial Catalog=UDLPWM;User ID =UDLClient;PWD=Call_Me_UDLA;";

            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                StringBuilder sb = new StringBuilder();
                string[] sArray = new string[9];
                sArray[0] = banknum;      <-------------------  error
                sArray[1] = transDtYR;
                sArray[2] = transDtMnth;
                sb.AppendFormat(myInsert, sArray);
                string cmdText = sb.ToString();
                using (SqlCommand cmd = new SqlCommand(connString))
                {
                   int rowsAffected = cmd.ExecuteNonQuery();
                }
            }
        }
    }
}
zimmer9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
string banknum;
                string transDtYR;
                string transDtMnth;                
            foreach (string line in ReadFile)
            {                
                banknum = line.Substring(0, 4);
                transDtYR = line.Substring(4, 4);
                transDtMnth = line.Substring(8, 2);                
            }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
Note that ignores all the lines until the last line in the file - because you loop through each line and then throw the values from the previous line away.  It is however the logic you have in your code snippet.  

(I suspect it is incorrect and you actually want to have the rest of the code inside that foreach loop.)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.