The anti-forgery token could not be decrypted error in ASP .Net MVC

The anti-forgery token could not be decrypted. If this application is hosted by a Web Farm or cluster, ensure that all machines are running the same version of ASP.NET Web Pages and that the <machineKey> configuration specifies explicit encryption and validation keys. AutoGenerate cannot be used in a cluster.

 

The above error will happen when we have multiple AntiForgeryToken in same form and we try to submit the form Or any object that is submitting to action that has multiple AntiForgeryToken (This case will happen if we try to submit FormData or serialized data ) in case of ajax calls. This is a ASP MVC error it can happen while we creating forms.


For Example

<form id="dummy"  action="/Admin/Product/Create" method="post">

 @Html.AntiForgeryToken()

 <label  >Product Name</label>
 <input id="txtName" name="ProductName" type="text" placeholder="Product Name" >
 @Html.AntiForgeryToken()				
 <button id="btnSave" name="btnSave" >Save</button>									
         
</form>									

 

In the above example, we can see the form has two call to generate Anti Forgery Token so when we submit the form we will get the error “The anti-forgery token could not be decrypted”.

 

Types of variables in c#

value and reference type             The c# support two major variable types, they are “Value type and Reference type”. Int, Float, Decimal , Char , etc are the value types and Classes, , Delegates , Interfaces , Arrays , etc are the Reference types.

Now we have seen c# has two different types of variable types. These types must have some difference so lets see how this value and reference type differs.

Value type and Reference type

I hope everyone knows, when ever we initialize a variable there is a memory allocation will happens, we should know this to know about the difference of value and reference types.

Value type

  • Value type variable store data directly into the memory location.
  • If we get any copy of the data to another variable and try to change any variable data , the changed data will affect only to the variable we try to change , other variable remain the old value.
  • Value type data will be saved in Stack memory.

Reference type

  • Reference type variable does not save data directly to the memory location. it stores the reference of the actual data.
  • If we get any copy of the data to another variable and try to change any variable data , the changed data affect every copy of the object.
  • Reference type data will be saved in Heap memory

Example

using System;

class RefClass
{
    public int Value = 0;
}

class TestProgram
{

static void Main() {

    int valtype1 = 0;
    int valtype2 = valtype1;
    valtype2 = 999;
    RefClass refObj1 = new RefClass();
    RefClass refObj2 = refObj1;
    refObj2.Value = 999;
    Console.WriteLine("Value type values: {0}, {1}", valtype1, valtype2);
    Console.WriteLine("Reference type values: {0}, {1}", refObj1.Value, refObj2.Value);
}

}

 

Output

Values: 0, 999
Refs: 999, 999

Value type

check the above example valtype2 is assigned from the variable valtype1 and changing the data valtype2. but in the output the change has been reflected only to the changed variable.

Reference type

check the above example refObj2 is assigned from the object refObj1 and changing the data inside the object refObj2. The change we made was in refObj2 but in the out put the change applied in the refObj2 reflected refObj1.

The requested page cannot be accessed because the related configuration data for the page is invalid in MVC

The requested page cannot be accessed

The error message “The requested page cannot be accessed because the related configuration data for the page is invalid in MVC”  is occurred due to invalid code in the web.config file. Invalid code in the sense you may given a invalid tag like for <connectionString> you may given <constr>. it makes the error we should use the current xml tag names.

Please see the below examples

Actual tag code for connection string

<connectionString>

you may be made mistake like

<connectionstring>

See the above example the ‘S’ letter for the two example is different because the web.config file xml tags are case sensitive. it should follow the visual studio rules to avoid errors.

In web.config xml file we should specify the tags currently according to the visual studio needs.

if anything wrong in the web.config file you will get the error like

The requested page cannot be accessed because the related configuration data for the page is invalid in MVC

 

How to move second div to first in mobile view using css ?

we may need to show divs in different ways in mobile views. Like the same now consider a situation there is a requirement to move the second div to first and first div to second in mobile view. for web view it is normal.

You can find below code that helps to move second div to first and first div to second using css.

If you want to test

Step 1 : the code just copy the code
Step 2 : open a editor or notepad and paste the code
Step 3 : save the code in any location with file extension “.html” , Eg : index.html
Step 4 : open the file in a browser and check the mobile view.

Move second div to first and first div to second using css

<html>
<style>
.parent{
width:100%;
}

.parent div
{
width:50%;
float:left;

}
@media only screen and (max-width: 767px) {
   
    .parent 
    {   
       display: table;
    }
    .parent div{width: 100%;float: none}
    .leftColumn
    {
        display:table-footer-group; 
  color:red;
     
  
    }
    .rightColumn
    {
        display: table-header-group;
      
    } 
 
 }
</style>
<body>

<div class="parent">
 <div class="leftColumn">
 This is the first div
 </div>

 <div class="rightColumn">
 This is the second div
 </div>
<div>

</body>

</html>

 

 

How to use switch statement in c# ?

Switch statement is used to check a variable with a set of conditions called cases. We can check a variable whether the value satisfies certain conditions.
For example below I have checked a variable is A, B or C. in some case we need to do some operations according to the check then we can go for the switch case.

Syntax for switch statement in c#

switch(expression_data) {
case condition1 :
Method1(s);
break; /* optional */
case condition2 :
Method2(s);
break; /* optional */
.
.
.
.
.

default : /* Default is optional */
methodN(s);
}

Example for switch statement in c#

using System;
namespace FindVariable
{
class Program
{
static void Main(string[] args)
{
char character_variable = 'B';

switch (character_variable)
{
case 'A':
Console.WriteLine("Variable has value A");
break;
case 'B':
Console.WriteLine("Variable has value B");
break;
case 'C':
Console.WriteLine("Variable has value C");
break;

default:
Console.WriteLine("The variable not have the chartectors A,B and C");
break;
}
Console.WriteLine("The variable has value{0}", character_variable);
Console.ReadLine();
}
}
}

Output

Variable has value B
The variable has value B

Array in Javascript

Array is a variable that is used to hold more than one value in a single variable. That is we can hold a list of data inside an array.

How to declare an array in javascript ?

We can declare an array in two ways. these are the ways we can achieve it.

Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayName2 = ["data 1", "data 1", "data 3"];

How to add elements to array in javascript ?

Javascript array has a method push, the method we can use to add new elements to the array. when we use push the element will be added at the end of the array.
Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3
arrayName1.push("data 4");
var arrayNewLength=arrayName1.length; // output will be 4

How to delete an elements from array in javascript?

Javascript array has method named pop , it help us to delete an element from an array. when we use pop the elements at the end will be deleted.

Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3
arrayName1.pop();
var arrayNewLength=arrayName1.length; // output will be 2

How to find number of elements in an array in javascript ?

Javascript array has a property called length , the length property help us to find the number of elements exist in an array.
Example

var arrayName1 = new Array("data 1", "data 2", "data 3");
var arrayLength=arrayName1.length; // output will be 3

What is comments and types of comments in javascript?

                          For two reasons we can have comments in the code, one is to define the description of the specific code and the another thing is to prevent the execution of code .

There are two type of comments are available in javascript

1. Single line comment
2. Multiple line comment

Single line comment

Single line comment is used to comment a single line of code.

Syntax

//the code we need to comment

Example

// var total=0;

Multiple line comment

Multiple line comment is used to comment a block of coded that is multy line commenting.

Syntax

/*
the code we need to comment line 1
the code we need to comment line 2
the code we need to comment line 3
*/

Example

/*
var a=10;
var b=20;
var c=a+b;
*/

 

How to comment codes in TWIG ?

Whenever we write codes we may need to comment some codes to avoid displaying and executing the line.we need two ways of commenting , they are single line commenting and multi line commenting.

Single line commenting

Single line commenting is used to comment a line of code .

Syntax

{# Commented code #}

Example

{#   <h3>{{ heading_title }}</h3>   #}

Multi line commenting

Multi line commenting is used to comment a part of codes or multiple lines of codes. the syntax and way of commenting is same as of single line commenting for the multi line commenting.

Syntax

{# Commented code #}

Example

{#

{% if product %}
<span>{{ product.title }}</span>
{% endif %}

#}

How to find Nth highest salary in Sql Server ?

How to find Nth highest salary in Sql Server ?

Finding the first or second or Nth highest salary of the person is a most important thing for the developers, as a developer we should know this. Mostly we hear this question in the interview.

Consider the below table with table name Employee

SELECT * FROM [dbo].[Employee]

How to find highest salary in Sql Server?

There are so many ways to find highest salary, you can see below some of the examples

Method 1

select max(salary) as highest_salary from [dbo].[Employee]

Method 2

select top 1 salary as highest_salary from [dbo].[Employee] order by salary desc

Method 3

;WITH CTE AS
(
SELECT salary ,ROW_NUMBER() OVER (ORDER BY Salary Desc) AS [Rank]
FROM [dbo].[Employee] group by salary
)
SELECT  salary as highest_salary
FROM CTE
WHERE [Rank]=1;

How to 2nd highest salary in Sql Server?

We are seen some of the methods to find highest salary above, we cannot use method 1 and method 2 to find the 2nd highest salary, but we can use the method 1 by little change in the code.

There are so many ways to 2nd highest salary, you can see below some of the example

Method 1

SELECT MAX(salary) FROM Employee WHERE Salary NOT IN ( SELECT Max(Salary) FROM Employee);

Method 2

SELECT TOP 1 Salary FROM ( SELECT TOP 2 Salary FROM Employee ORDER BY Salary DESC) AS MyTable ORDER BY Salary ASC;

Method 3

;WITH CTE AS
(
SELECT salary ,ROW_NUMBER() OVER (ORDER BY Salary Desc) AS [Rank]
FROM [dbo].[Employee] group by salary
)
SELECT  salary as highest_salary FROM CTE WHERE [Rank]=2;

How to Nth highest salary in Sql Server?

Previously we have seen highest salary and 2nd highest salary solutions, also we know the query using CTE is used again used in 2nd highest salary with a little modification, the modification is we are changing the rank

If we want 1st highest salary we have to give rank as 1

If we want 2nd highest salary we have to give rank as 2

So we can use the same query to find the nth salary like below

;WITH CTE AS
(
SELECT salary ,ROW_NUMBER() OVER (ORDER BY Salary Desc) AS [Rank]
FROM [dbo].[Employee] group by salary
)

SELECT  salary as highest_salary FROM CTE WHERE [Rank]=”Nth”;

Example

;WITH CTE AS
(
SELECT salary ,ROW_NUMBER() OVER (ORDER BY Salary Desc) AS [Rank] 
FROM [dbo].[Employee] group by salary
)
SELECT  salary as highest_salary FROM CTE WHERE [Rank]=”Nth”;

Finding 5th highest salary in sql server

;WITH CTE AS
(
SELECT salary ,ROW_NUMBER() OVER (ORDER BY Salary Desc) AS [Rank]
FROM [dbo].[Employee] group by salary
)
SELECT  salary as highest_salary
FROM CTE WHERE [Rank]=5;

The view ‘Index’ or its master was not found or no view engine supports the searched locations

Problem

The view ‘Index’ or its master was not found or no view engine supports the searched locations. The following locations were searched:

  • ~/Views/Home/Index.aspx
  • ~/Views/Home/Index.ascx
  • ~/Views/Shared/Index.aspx
  • ~/Views/Shared/Index.ascx
  • ~/Views/Home/Index.cshtml
  • ~/Views/Home/Index.vbhtml
  • ~/Views/Shared/Index.cshtml
  • ~/Views/Shared/Index.vbhtml

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Solution

            The problem will occurs when we trying to access an action in the controller.  Consider the below action

public class HomeController : Controller
{
           public ActionResult Index()
           {
                 return View();
            }
}

The action index returns a view. Then it will check the corresponding view file in the following locations 

~/Views/Home/

 ~/Views/Shared/

Not only the location, it will check corresponding view with the action name that is Index is in our case, with following extensions

  • .aspx
  • .ascx
  • .aspx
  • .ascx
  • .cshtml
  • .vbhtml
  • .cshtml
  • .vbhtml

In the folder home or shared should contain a file with any of the extension above. 

After creating the view in the specified folder the controller will get the correct view, then view will be shown in the page and we can avoid this error. This is one of the error we face when we start mvc projects. But is a basic thing in mvc in .net, it will understood this with in some days after starting study of mvc in .net.