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.

Sunday, January 08, 2012

Consuming the live Currency Conversion Web Service


There is a live .NET Web Service which offers all the curreny conversion eg. USD to INR etc.

This WS is available via live URL http://www.webservicex.net/CurrencyConvertor.asmx

This WS has a class CurrecnyConvertor which exposes one Function named ConversionRate and an enum named Currency which exposed the list of all the currencies like USD, INR, AED etc.

As you know Currency conversion will depend on two parameters ConvertFrom and ConvertTo and so the same is expected to be passsed while invoking ConversionRate function.

Here is a detailed list of steps:

1- Add a Web Reference to http://www.webservicex.net/CurrencyConvertor.asmx
2- Rename the Reference name to something more friendly like CurrencyConversionWS
3- In your project create an instance of the added Web service reference
4- On the created object invoke the ConversionRate function.
5- As mentioned above there is an enum which lists all the currencies and so access it directly from the object created.
6- Save the result into a double variable and multiply with your passed amount.
7- Multiply the amount you want to convert to the received conversion rate.

Here is the code you will require to have it all working:

CurrencyConversionWS.
CurrencyConvertor
objWS = new CurrencyConversionWS.CurrencyConvertor();

double usdToinr = objWS.ConversionRate(CurrencyConversionWS.Currency.USD, CurrencyConversionWS.Currency.INR);

double totalAmount = usdToinr * Double.Parse(textBox1.Text);

MessageBox.Show(totalAmount.ToString(),"Total Indian Rupees");

This is how it will look like:



What is SSPI in a .NET Connection String


SSPI stands for Security Support Provider Interface. The SSPI allows an application to use any of the available security packages on a system without changing the interface to use security services. The SSPI does not establish logon credentials because that is generally a privileged operation handled by the operating system.

Usually a .NETconnection string looks like this, you will have your own server, databse names ofcourse.


"Data Source=localhost\sql2012;Initial Catalog=AdventureWorks;
Integrated Security=SSPI"


Other than SSPI you can also use "true". Integrated Security actually ensures that you are connecting with SQL Server using Windows Authentication, not SQL Authentication; which requires username and password to be provided with the connecting string.




Thursday, December 08, 2011

Succedding at Technical Interview

I got this audio on how to succeed Technical Interview.

video