Wednesday 10 August 2016

C# compiler


Hello there!

This article will help us to develop code which could basically compile our code. This could help us in creating web/ windows based  C# compiler even TTD as well. Enjoy the code.


using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;


namespace WebSharpCompilerBusiness
{
    public class WebSharpCompiler
    {
        public List<string> Compile(string programText)
        {
            List<string> messages = new List<string>();
            if (String.IsNullOrEmpty(programText))
            {
                messages.Add("program text cannot be null or empty");
            }
            CompilerResults compilerResults = ProcessCompilation(programText);
            foreach (CompilerError error in compilerResults.Errors)
            {
                messages.Add(String.Format("Line {0} Error No:{1} - {2}", error.Line, error.ErrorNumber, error.ErrorText));
            }

            return messages;
        }

        public CompilerResults ProcessCompilation(string programText)
        {
            CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp");
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = false;
            return codeDomProvider.CompileAssemblyFromSource(parameters, programText);
        }
    }
}

Now, for demo purpose I am simply demonstrating an UnitTest over here.

        [TestMethod]
        public void TestCompilerSingleError()
        {
            WebSharpCompiler compiler = new WebSharpCompiler();
            string programText = @"
          using System;
          namespace HelloWorld
          {
              class HelloWorldClass
              {
                  static void Main(string[] args)
                  {
                   
                      //  byte c = 1/0;
                      Console.ReadLine();
                  }
              }
          }";
            List<string> compilerErrors = compiler.Compile(programText);
            Assert.AreEqual(compilerErrors.Count, 0);
        }


That's all!

Cheers!

Wednesday 3 August 2016

How to find out connection string issue


Data Link Properties Dialog Box

The Data Link Properties dialog box is the standard Windows system interface for configuring connection strings to data sources. This dialog box exposes all of the properties that the selected OLE DB provider supports. A link to the resulting data connection is displayed under the Data Connections node in the Server Explorer.
To access this dialog box in Visual Studio .NET, select Connect to Database from the Tools menu or click the Connect to Database icon in Server Explorer. You also can open this dialog box by double-clicking a universal data link (.udl) file in Windows Explorer, and in a variety of other ways, including programmatically.
To use it directly:
Just need to save a blank notepad with ".UDL" extension and double click to open it. There is a Connections table where you can set credentials and test your DB and save connection string as well.
To view connection open this UDL file in notepad.