Thursday, May 30, 2019

REDIS Cache with C#, Learning Redis Cache, Capture DataTable into Redis Server

Redis Cache is a key-value cache that stores the information in a hash table format, providing the possibilities to store different types of structured data like strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs

1. Install Redis server (redis-server.exe) on your machine

2. Create RedisConnectorHelper class file

    public class RedisConnectorHelper
    {
        static RedisConnectorHelper()
        {
            RedisConnectorHelper.lazyConnection = new Lazy(() =>
            {
                return ConnectionMultiplexer.Connect("localhost");
            });
        }

        private static Lazy lazyConnection;

        public static ConnectionMultiplexer Connection
        {
            get
            {
                return lazyConnection.Value;
            }
        }
    }


3. You will noticed here ConnectionMultiplexer is not found in your application, so add using NuGet Package manager, search "StackExchange.Redis"

4. After installation you are ready to work in Redis application

5. Create the connection object of Redis server like this
    var cache = RedisConnectorHelper.Connection.GetDatabase();

6.  Store your values in this way
     cache.StringSet($"Device_Status:{i}", "value");

7. Retrive Redis/Cached values this way
     var value = cache.StringGet($"Device_Status:{i}");

8.  Some time we need to store DataTable into session/cache object so same we can do in following way.

    Store Values in Redis server:
   
   string TableName = "GetEmpData";
   foreach (DataRow dr in GetEmpData().Rows)
   {
      cache.StringSet($"Device_Status:"+ TableName + "_" +dr["id"].ToString(), string.Format("{0} : {1} : {2} : {3}", dr["Id"].ToString(), dr["Name"].ToString(), dr["Mobile"].ToString(), dr["Date"].ToString()));
   }

    Retrive Values from Redis server:
 
  foreach (DataRow dr in GetEmpData().Rows)
   {
     var value = cache.StringGet($"Device_Status:" + TableName + "_" + dr["id"].ToString());
     Console.WriteLine($"New Valueeeeee={value}");
   }
 
9. Sample Code Download Here

10. For Details information you can also visit the below link,

https://www.c-sharpcorner.com/UploadFile/2cc834/using-redis-cache-with-C-Sharp/

Monday, June 25, 2018

Find all table names with column name in sql

SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
    JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%MyCol%';

Tuesday, May 15, 2018

Check date is valid or not in JavaScript


Find a function to check date is valid or not in Java script

                        /**
                        * Get the number of days in any particular month                       
                        * @param  {integer} m The month (valid: 0-11)
                        * @param  {integer} y The year
                        * @return {integer}   The number of days in the month
                        */
                        var daysInMonth = function (m, y) {
                            switch (m) {
                                case 1:
                                    return (y % 4 == 0 && y % 100) || y % 400 == 0 ? 29 : 28;
                                case 8: case 3: case 5: case 10:
                                    return 30;
                                default:
                                    return 31
                            }
                        };

                        /**
                        * Check if a date is valid                       
                        * @param  {[type]}  d The day
                        * @param  {[type]}  m The month
                        * @param  {[type]}  y The year
                        * @return {Boolean}   Returns true if valid
                        */
                        var isValidDate = function (d, m, y) {                           
                            m = parseInt(m, 10) - 1;
                            return m >= 0 && m < 12 && d > 0 && d <= daysInMonth(m, y);
                        }; 

Calling From Function:-

var bits = tempVal.split('/');
alert(isValidDate(bits[0], bits[1], bits[2]));

Example:-

isValidDate(30, 3, 2013); // March 30, 2013 - true
isValidDate(29, 2, 2013); // February 29, 2013 - false
isValidDate(29, 2, 2012); // February 29, 2012 - true

Tuesday, April 24, 2018

What in Func in c#

    Func is a generic delegate that encapsulates a method that can accept parameters and return some value

//Annonymous function
Func MyFun1 = delegate(int[] a)
    {
        string tempString = "";
        foreach (int num in a) {
            tempString += num.ToString() + ",";
        }       
        return tempString;
    };




//Generic or Arrow function
    Func MyFun2 = (a) =>
    {
        string tempString = "";
        foreach (float num in a)
        {
            tempString += num.ToString() + ",";
        }
        return tempString;
    };


    private T[] Sort(T[] inputArray)
    {
        //Sort array
        //and return sorted array
        return inputArray;
    }

   int[] numArray = { 10, 15, 26, 98, 68 };
   float[] floatArray = { 10.9F, 15.5F, 26, 98.8F, 68 };
   lblMessage.Text = "Num : " + MyFun1.Invoke(Sort(numArray));
   lblMessage.Text += "Float : " + MyFun2.Invoke(Sort(floatArray));

Friday, April 13, 2018

Compare two dates in javascript

var yearDiff = 100;   
var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    var day = d.getDate();
    var dtToday = new Date(year - yearDiff , month, day)

    mydate = new Date(DOBDate);
    year = mydate.getFullYear();
    month = mydate.getMonth();
    day = mydate.getDate();
    mydate = new Date(year, month, day);

    if (mydate > dtToday) {
      alert("Age can not be greater than 100 years.");
    }
    else {
        alert("Prev Smaller");
    }

Tuesday, March 27, 2018

Get Publish page url and title in sitefinity



SELECT sf_page_node.Title_,
  _url_parent =
    CASE WHEN (sf_page_node_2.url_name_ = N'Pages')   THEN '/'
              ELSE sf_page_node_2.url_name_ +'/'
    END,
  sf_page_node.url_name_ AS url_page
FROM
  sf_page_node LEFT OUTER JOIN
  sf_page_node AS sf_page_node_2
  ON sf_page_node.parent_id = sf_page_node_2.id
WHERE (sf_page_node.root_id =
    (
      SELECT id FROM sf_page_node AS sf_page_node_1 WHERE (nme = 'FrontendSiteMap')
     )
   ) AND
  (sf_page_node.show_in_navigation = 1) AND
  (sf_page_node.render_as_link = 1) AND
  (sf_page_node.node_type = 0) AND
  (sf_page_node.approval_workflow_state_ = N'Published')


Query 2:-

SELECT sf_page_node.title_ AS "Page Title", sf_sites.nme AS "Site"
FROM sf_page_node
JOIN sf_sites on sf_page_node.root_id = sf_sites.site_map_root_node_id
WHERE sf_site.nme = 'ProjectName'

Monday, November 27, 2017

Find a specific text in a SQL Server Stored Procedure, Function, View

Declare @SearchString  varchar(50) = 'kotak_CMS..sf_subscriber'

Get Sp name with type and defination from sys.sql_modules and sys.objects tables
SELECT [Scehma]=schema_name(o.schema_id), o.Name, o.type, m.definition
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON o.object_id = m.object_id
WHERE m.definition like '%'+@SearchString+'%'

Get Sp name from syscomment and sysobjects table
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%'+@SearchString+'%'

Get Sp name from sys.procedures table
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@SearchString+'%'
GO

Find Column name in database tables
SELECT      c.name  AS 'ColumnName'
            ,t.name AS 'TableName'
FROM        sys.columns c
JOIN        sys.tables  t   ON c.object_id = t.object_id
WHERE       c.name LIKE '%Branch%'
ORDER BY    TableName ,ColumnName;
GO

Find Text in All Store Procedure
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
    FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_DEFINITION LIKE '%(7)%' 
    AND ROUTINE_TYPE='PROCEDURE'
GO