Monday, March 16, 2020

How to use Querystring Parameters in Asp.Net MVC to Retrieve or Send Data


Query string in asp.net mvc with accessing from URL now let’s call Action Method from Action Link from passing Query string. Calling Action method from Action Link with Passing Query string.



With Optional Parameters 

@Html.ActionLink("Call Index Action", "Index", new { ID = 10, Name = 3, Department = "test" })

Without Optional Parameters 

@Html.ActionLink("Call Details Action", "Details", new { Name = 3, Department = "test" })

Friday, March 13, 2020

How to generate List of previous 12 months with year from DateTime.Now

var lastSixMonths = Enumerable.Range(0, 12)
                                   .Select(i => DateTime.Now.AddMonths(i -12))
                                    .Select(date => date.ToString("MMM"));


Output :
  "Mar"
"Apr"
"May"
"Jun"
"Jul"
"Aug"
"Sep"
"Oct"
"Nov"
"Dec"
"Jan"
"Feb"


var lastSixMonths = Enumerable.Range(0, 12) .Select(i => DateTime.Now.AddMonths(i -12)) .Select(date => date.ToString("MM/yyyy"));

Output :
  "03/2019"
"04/2019"
"05/2019"
"06/2019"
"07/2019"
"08/2019"
"09/2019"
"10/2019"
"11/2019"
"12/2019"
"01/2020"

"02/2020"

Wednesday, March 11, 2020

Visual Studio Expand/Collapse keyboard shortcuts

Ctrl+M,O would collapse all constructors, methods, properties in a file. It would also collapse all regions.

Ctrl+M,L would expand all constructors, methods, properties in a file. It would also expand all regions.



Visual studio 2017 except it is turned off by default. It can be enabled under Tools -> Options -> Text Editor -> C# -> Advanced -> Outlining -> "Collapse #regions when collapsing to definitions"



Visual Studio 2015:

Tools > Options > Settings > Environment > Keyboard
Defaults:

Edit.CollapsetoDefinitions: CTRL + M + O

Edit.CollapseCurrentRegion: CTRL + M +CTRL + S

Edit.ExpandAllOutlining: CTRL + M + CTRL + X

Edit.ExpandCurrentRegion: CTRL + M + CTRL + E

To set and use IntelliJ's shortcuts:

Edit.CollapsetoDefinitions: CTRL + SHIFT + NUM-

Edit.CollapseCurrentRegion: CTRL + NUM-

Edit.ExpandAllOutlining: CTRL + SHIFT + NUM+

Edit.ExpandCurrentRegion: CTRL + NUM+


Collapse to definitions
CTRL + MO
Expand all outlining
CTRL + MX
Expand or collapse everything
CTRL + ML

Wednesday, March 4, 2020

Remove extra spaces from a string

Here is a simple one:

trimmedstr = str.replace(/\s+$/, '');

When you need to remove all spaces at the end:

str.replace(/\s*$/,'');
When you need to remove one space at the end:

str.replace(/\s?$/,'');
\s means not only space but space-like characters; for example tab.

If you use jQuery, you can use the trim function also:

str = $.trim(str);

But trim removes spaces not only at the end of the string, at the beginning also.

 var str  = "Hello World  ";
  var ans = str.replace(/(^[\s]+|[\s]+$)/g, '');

  alert(str.length+" "+ ans.length);

Tuesday, March 3, 2020

Difference Between NEWSEQUENTIALID() and NEWID() in SQL Server

NEWSEQUENTIALID() and NEWID() both generates the GUID of datatype of uniqueidentifier.


NEWSEQUENTIALID:
  1. NEWSEQUENTIALID() generates GUID in hexadecimal incremental interval. The hexadecimal number can be any placed in GUID
  2. Function NEWSEQUENTIALID() can not be used in SQL queries and it can be only used in DEFAULT clause of table. NEWSEQUENTIALID() are predictable
  3. NEWSEQUENTIALID() generates the GUID in sequential order.

NEWID:

  • NEWID() generates the GUID in random order whereas 
  • in case of privacy or security use NEWID() instead of NEWSEQUENTIALID()
  • NEWID() will work perfectly fine when used in queries.


Table Syntax :

CREATE TABLE tablename
 (
 NewIDColumnName uniqueidentifier DEFAULT NEWID(),
 NewSeqColumnName uniqueidentifier DEFAULT NewSequentialID()
)

Check for empty GUID in SQL

select cast(cast(0 as binary) as uniqueidentifier)

Declare @GuidParam uniqueidentifier

 SET  @GuidParam ='00000000-0000-0000-0000-000000000000'


IF @GuidParam = CAST(CAST(0 AS BINARY) AS UNIQUEIDENTIFIER)
BEGIN
   --Guid is empty
END

Monday, March 2, 2020

To write Case Statement in WHERE Clause

 DECLARE @FirstName VARCHAR(100)
SET @FirstName = 'King'

DECLARE @LastName VARCHAR(100)
SET @LastName = 'Test'

SELECT FirstName, LastName
FROM Employee
WHERE 
    FirstName = CASE
    WHEN LEN(@FirstName) > 0 THEN  @FirstName
    ELSE FirstName
    END
AND
    LastName = CASE
    WHEN LEN(@LastName) > 0 THEN  @LastName
    ELSE LastName
    END
GO

Mixed Content: The page at xxx was loaded over HTTPS, but requested an insecure

 Mixed Content: The page at ' https ://www.test.com/signup.aspx' was loaded over HTTPS, but requested an insecure script ' http ...