Friday, December 29, 2017

How to create Roman Numerals function in SQL


CREATE Function [dbo].[GetRomanNo] ( @N as varchar(20) )
RETURNS VARCHAR(100)
AS
BEGIN
  DECLARE @s varchar(100), @r varchar(100), @i bigint, @p int, @d bigint
  SET @s = ''
  SET @r = 'IVXLCDM' -- Roman Symbols

  /* There is no roman symbol for 0, but I don't want to return an empty string */
 IF @n=0
  SET @s = '0'
 ELSE
 BEGIN
  SELECT @p = 1, @i = ABS(@n)
  WHILE(@p<=5)
  BEGIN
   SET @d = @i % 10
   SET @i = @i / 10
   SELECT @s = (CASE WHEN @d IN (0,1,2,3) THEN Replicate(SubString(@r,@p,1),@d) + @s
        WHEN @d IN (4) THEN SubString(@r,@p,2) + @s
        WHEN @d IN (5,6,7,8) THEN SubString(@r,@p+1,1) + Replicate(SubString(@r,@p,1),@d-5) + @s 
        WHEN @d IN (9) THEN SubString(@r,@p,1) + SubString(@r,@p+2,1) + @s END)
   SET @p = @p + 2
  END
 
  SET @s = Replicate('M',@i) + @s
 
  IF @n < 0
   SET @s = '-' + @s
  END

 RETURN @s
END 

Result

Thursday, December 28, 2017

How to use Foreach Loop in LINQ

public class Student
     {
            public int StudentID { get; set; }
            public string StudentName { get; set; }
        }

        public void GetStudent()
        {
            List<Student> obj = new List<Student>();

            Student objStudent1 = new Student();
            objStudent1.StudentID = 1;
            objStudent1.StudentName = "Test 1";
            obj.Add(objStudent1);

            Student objStudent2 = new Student();
            objStudent2.StudentID = 2;
            objStudent2.StudentName = "Test 1";
            obj.Add(objStudent2);

            obj.ForEach(c => {
                if (c.StudentID == 1)
                {
                    Console.Write("Student :" + c.StudentName);
                }
            });
        }

Saturday, December 23, 2017

How to display Notification in Notification Area of Taskbar in Windows Application

How to display Notification in Notification Area of Taskbar in Windows Application

DateTime Dates = Convert.ToDateTime("12/7/2012 6:09:11 PM");
TimeSpan diff = Dates - System.DateTime.Now;
notifyIcon1.Icon = SystemIcons.Exclamation;
notifyIcon1.BalloonTipTitle = "Days Remaning";
notifyIcon1.BalloonTipText = "Day : " + diff.Days.ToString();
notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(30000);

How to read & write file in isolation storage in c#

How to read & write file in isolation storage in c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.IsolatedStorage;
using System.Diagnostics;

namespace Program
{
        //Variable
        string FullLine;
        const string ISOLATED_FILE_NAME = "Setting.txt";
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

        private void ISOStorageUse_Load(object sender, EventArgs e)
        {
            cbYesNo.Text = "No";
            ddlStandard.SelectedIndex = 0;
           
            #region Get Data
            IsolatedStorageFileStream iStream = new IsolatedStorageFileStream(ISOLATED_FILE_NAME, FileMode.OpenOrCreate, isoStore);
            StreamReader reader = new StreamReader(iStream);
            FullLine = reader.ReadToEnd();
            reader.Close();
            #endregion Get Data

            #region Apply Value
            string SplitPart = FullLine.Replace("\r\n", ",");
            string[] Parts = SplitPart.Split(',');

            if (SplitPart == "") return;
            dtpDate.Value = Convert.ToDateTime(Parts[0]);
            tbName.Text = Parts[1];
            ddlStandard.SelectedIndex = Convert.ToInt32(Parts[2]);
            cbYesNo.Checked = Convert.ToBoolean(Parts[3]);
            #endregion Apply Value
        }

       // Form Closing Event
       private void ISOStorageUse_FormClosing(object sender, FormClosingEventArgs e)
       {
            // Write some text into the file in isolated storage.
            IsolatedStorageFileStream oStream = new IsolatedStorageFileStream(ISOLATED_FILE_NAME, FileMode.OpenOrCreate, isoStore);
            StreamWriter writer = new StreamWriter(oStream);
            writer.WriteLine(dtpDate.Value);
            writer.WriteLine(tbName.Text);
            writer.WriteLine(ddlStandard.SelectedIndex);
            writer.WriteLine(cbYesNo.Checked);
            writer.Close();
       } 
}

How to use String Builder in C#

How to use String Builder in C#


using System;
using System.Text;

class Program
{
    static void Main()
    {
       StringBuilder builder = new StringBuilder();
       // Append to StringBuilder.
       for (int i = 0; i < 10; i++)
       {
            builder.Append(i).Append(" ");
       }
       Console.WriteLine(builder);
    }
}