Sunday, September 29, 2013

 Basic Intro to Cursor in MS SQL


Cursor works as a foreach loop in ms sql.  
@@FETCH_STATUS = 0 -> The FETCH statement was successful if  ,
@@FETCH_STATUS = -1 -> The FETCH statement failed or the row was beyond the result set,
@@FETCH_STATUS = -2 -> The row fetched is missing.


DECLARE @ServiceId INT
    DECLARE @ContentId INT
    DECLARE @ContentExist INT = 1

    --cursor start
    DECLARE monitoring_cursor CURSOR
    FOR
    SELECT ID
        ,ContentID
    FROM dbo.[Services]
   
    OPEN monitoring_cursor

    FETCH NEXT
    FROM monitoring_cursor
    INTO @ServiceId
        ,@ContentId



    WHILE @@FETCH_STATUS = 0
    BEGIN
        IF NOT EXISTS (
                SELECT *
                FROM dbo.ContentData
                WHERE ContentID = @ContentId
                  )
        BEGIN
            SET @ContentExist = 0;
        END

        IF @ContentExist = 0
        BEGIN
            INSERT INTO dbo.Monitoring (
                [Date]
                ,[Status]
                ,[Description]
                )
            VALUES (
                GETDATE()
                ,'Failed'
                ,'Service' + @ServiceId + 'no content'
                )
        END
        ELSE
        BEGIN
            INSERT INTO dbo.Monitoring (
                [Date]
                ,[Status]
                ,[Description]
                )
            VALUES (
                GETDATE()
                ,'OK'
                ,NULL
                )
        END

        FETCH NEXT
        FROM monitoring_cursor
        INTO @ServiceId
            ,@ContentId
    END

    CLOSE monitoring_cursor

    DEALLOCATE monitoring_cursor
        --- cursor end

Wednesday, September 25, 2013

Function To Check ASCII or UTF-8 Format


public string CheckEncoding(string input)
        {
            string returnValue = String.Empty;
            string sOut = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(input));
            if (input == sOut)
            {
                return "ASCII";
            }
            else
            {
                var utf8Format = Encoding.UTF8;
                byte[] b = utf8Format.GetBytes(input);
                int taster = b.Length;
                bool checkUtf8 = false;
                if (b.Length >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF)
                {
                   checkUtf8 = true;
                }
                else
                {
                    int i = 0;
                    while (i < taster - 4)
                    {
                        if (b[i] <= 0x7F)
                        {
                            i += 1;
                            continue;
                        }
                        if (b[i] >= 0xC2 && b[i] <= 0xDF && b[i + 1] >= 0x80 && b[i + 1] < 0xC0)
                        {
                            i += 2;
                            checkUtf8 = true;
                            continue;
                        }
                        if (b[i] >= 0xE0 && b[i] <= 0xF0 && b[i + 1] >= 0x80 && b[i + 1] < 0xC0 && b[i + 2] >= 0x80 &&
                            b[i + 2] < 0xC0)
                        {
                            i += 3;
                            checkUtf8 = true;
                            continue;
                        }
                        if (b[i] >= 0xF0 && b[i] <= 0xF4 && b[i + 1] >= 0x80 && b[i + 1] < 0xC0 && b[i + 2] >= 0x80 &&
                            b[i + 2] < 0xC0 && b[i + 3] >= 0x80 && b[i + 3] < 0xC0)
                        {
                            i += 4;
                            checkUtf8 = true;
                            continue;
                        }
                        checkUtf8 = false;
                        break;
                    }
                }
                if(checkUtf8 == true)
                {
                    returnValue = "UTF-8";
                }
            }
            return returnValue;
        }

Saturday, September 7, 2013

Basic Introduction To ASP.NET - MVC 3 Framework


  • First of all, install mvc 3 framework. Download From: http://www.asp.net/mvc/mvc3.
  • After installing, Open Microsoft Visual Studio. Goto File and Create a New Project.
  • Choose ASP.NET MVC 3 Web Application and choose the Empty Template / Internet Application.
  • Now in Solution Explorer, right click the Controllers Folder and add Controller. (Lets name it HomeController). Also add a model in Model Folder (Lets name it HomeModel).
  • Open the HomeController.cs and modifiy it as: (include using MvcApplication1.Models)





  • Open SQL server and in your database, create a table. Here, infoTable is created as:






  • Open the HomeModel.cs and modifiy it as: (include using MvcApplication1.Connection)






  • Here, In Connection FolderDatabaseConnection class method getConnection() returns connection string where you define your connection to your server,database.






  • Then add a View by clicking inside public ActionResult Index() method.






  • Open Index.cshtml and write the html code.




  • demo.css looks like:




  • Output :


NOTE:


  • Content Folder include all css files and images that are being used by the project.
  • Global.asax.cs  includes the public static void RegisterRoutes(RouteCollection routes) method where default controller and its view can be set as per required during build.