Tuesday, February 07, 2012

File Read Write using System.IO via StreamReader and StreamWriter

using System;
using System.IO;
namespace FileRead_ExceptionHandling
{
    class Program
    {
        static void Main(string[] args) 
        {
            // declaring stream-reader here so it become accessible to the
            // code blocks of try { } and finally { }

           StreamReader sr = null;                      
            try
           {
                  // this assume you will have a file on C:\ as mentioned below
                  sr = new StreamReader(@"c:\TestCode2.log");
                  string text = sr.ReadToEnd();

                  Console.WriteLine(text);
           }           
          
           catch (FileNotFoundException ex)
           {
               Console.WriteLine(ex.Message + "\n
               The wrong file name or path is provided!! Try Again");
           }
           catch (Exception ex)
           {
               Console.WriteLine("Try again" + ex.Message);
           }
          
            finally
            {
                if (sr != null)
                {
                   sr.Close();
                Console.WriteLine("Stream closed");
                }
                else
                {
                   Console.WriteLine("Stearm is Null");
                   Console.WriteLine("Try Again");
                }

               // Performing stream-write operation to the same file
               StreamWriter sw = new StreamWriter(@"c:\TestCode.log", true);
               sw.WriteLine("Line 1");
               sw.Close();   

               Console.ReadLine();
              }
        }
    }
}

Sunday, February 05, 2012

SQL Server 2012 RC0 SSMS Target Invocation Exception


As SQL Server 2012 and Visual Studio 2011 Developer Preview are not yet released and so there bits are unstable when installed in wrong sequence.

This exception is known to occur when you start SSMS (SQL Server Management Studio) of SQL server 2012. This exception is being caused to occur when you installed VS 2011 on SQL 2012 RRC0 installed machine, I.e you install SQL Server 2012 1st and then VS 2011.

Otherwise you might have noticed that prior to installation of VS 2011; your SSMS was working fine.

many sites advise to uninstall and re-install SQL Server and VS 2011 in appropriate sequence or even keep these in two different machines atleast until these products are mature and finally released.

This is pretty cokplicated issues, now to solve this you need to modify a registry entry and you are set to work again.

1- Start regedit.exe

2- Expand HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio

3- You will see a node "11.0_Config", DELETE this

4- Close Registry Editor,

5- Restart SQL Server 2012 SSMS (SQL Server Management Studio)

It will load just fine as expected.

Hope this will help.