Hi, I'm sure many of you understand variable scope and encapsulation in dot.net so I apologize, I'm self taught and though I consider myself an intermediate c# coder now, I deal with this stuff as it comes up. I have a large "using" loop for a teradata connection and that is working fine. I decided to bomb proof it a little and put a "try" on the first line that connects to the database (and I'm sure this works, I have done it in a lot of other places) so I could capture an exception if it fails (I do have to say that the error messages are pretty good now if you don't bother with a try). When I put the try inside the using all of the sudden the "reader" was out of scope for the rest of the using loop. An outline of the relevant code is shown below. Any suggestions about using a try inside a using?
using (TdConnection cn = new TdConnection()) { //declarations int currentRow = 1; // set the output file // there is some mention of problems with multiple streamwriters but as long // as you close both files, this works. string outputFileNm = ConfigurationManager.AppSettings["configOutputFileNm"]; string logFileNm = ConfigurationManager.AppSettings["logFileNm"]; //string lsrv = "", ldb = "", ldept = "", cont = "", mech = ""; if (File.Exists(k.outputFileNm)) { File.Delete(k.outputFileNm); } StreamWriter ws; ws = new StreamWriter(k.outputFileNm); if (File.Exists(k.logFileNm)) { File.Delete(k.logFileNm); } StreamWriter wslog; wslog = new StreamWriter(k.logFileNm); begDate = fetchDate.fetchStageDate(); Console.WriteLine("Begin date is: " + begDate); // fetch teradata connection parameters from central source xconn = ReadDept.fetchTDConn(); //string[] s = xconn.Split(new char[] { '\t' }); string[] s = xconn.Split(new char[] { '\t' }); // ... set teradata connection //Console.WriteLine("conn string was: " + xconn); cn.ConnectionString = conStrBuilder.ConnectionString; cn.Open(); TdCommand cmd = cn.CreateCommand(); cmd.CommandText = "select blah from blah ... "; // can't "try" this without losing reader scope for some reason, probably because of the using loop TdDataReader reader = cmd.ExecuteReader(); // ... all the reader and write stuff } // end using loop