Wednesday, 14 October 2015

How to handle 403 error in ASP.Net

How to handle 403 error in ASP.Net

Using the following code you can not resolve 403 error.

<customErrors mode="On">
      <error statusCode="400" redirect="error/error_page.aspx?ID=400" />
      <error statusCode="403" redirect="error/error_page.aspx?ID=403" />
      <error statusCode="404" redirect="error/error_page.aspx?ID=404" />
      <error statusCode="500" redirect="error/error_page.aspx?ID=500" />
</customErrors>

To resolve 403 error use the following code.

 <httpErrors errorMode="Custom">
      <error statusCode="403" subStatusCode="14" path="http://mywebsite.com/error/error_page.aspx?id=403" responseMode="Redirect" />
  </httpErrors>

Tuesday, 6 October 2015

What is the differnce between var and dynamic keyword?

What is the differnce between var and dynamic keyword?

Suppose we have code like below.

var i= 123;
i="ABC";
print(i);
Above code will give compilation error "Cannot implicitly convert type 'string' to 'int'",
because when we initialize the var variable first time it will decide the datatype according
to value which we assign,
here first we assign i=123, so on the basis of value i is integer, intetger datatype is got
assigned and therefore later we cannot assign string value to it.

And suppose with dynamic keyword we have code like below

dynamic i= 123;
i="ABC";
print(i);
Above code gives the output as "ABC" because as name suggest the dynamic variable can deal
with any kind data type, no matter which first value is assign to this variable.

Can we overload methods in following way?

Can we overload methods in following way?

public void MySampleMethod(int i) { }
public void MySampleMethod(ref int i) { }
The signature of a method consists of the name of the method,
the number of type parameters and the type and kind (value, reference, or output)
of each of its formal parameters, considered in the order left to right.

So with above code as there is use of "ref" keyword which makes it unique signature
comapre to other one i.e. we can overload method in above way.

Can we overload methods in following way?

public void MyTestMethod(ref int x){}
public void MyTestMethod(out int x){}

though kind of parameter is a part of signature as
like previous case but here you have used ref and out toghether which is providing
same meaning to the methods therefor you cannot overload methods in same class or
interface solely by ref and out so above code will throw following error.

"Cannot define overloaded method 'MyTestMethod' because it differs from another method only on ref and out"

The differences between LEN and DATALENGTH in SQL Server

The differences between LEN and DATALENGTH in SQL Server

LEN
Returns the number of characters, rather than the number of bytes,
of the given string expression, excluding trailing blanks.

DATALENGTH
Returns the number of bytes used to represent any expression.

So what does that mean? It means that the LEN function will first right
trim the value and then give you a count of the charaters, the DATALENGTH
function on the other hand does not right trim the value and gives you the
storage space required for the characters.

Take a look at this example
declare @v nchar(5)
select @v ='ABC  '


select len(@v),datalength(@v)
The output for len is 3 while the output for datalength =10. The reason that
datalength returns the value 10 is because nvarchar uses 2 bytes to store 1
character by using unicode while varchar is using ascii which requires 1 byte
per charaters

How to remove leading 0 in SQL

Remove leading 0 in SQL:

select substring(ColumnName, patindex('%[^0]%',ColumnName), len(ColumnName))