Wednesday, 14 December 2016

How does Math.Round(2.5) behave?


In C#, the result of Math.Round(2.5) is 2.
It is supposed to be 3, isn't it? Why is it 2 instead in C#?
Answer:
The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned.
 and so 2.5, being halfway between 2 and 3, is rounded down to the even number (2). this is called Banker's Rounding (or round-to-even), and is a commonly-used rounding standard.
The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding.
You can specify the behavior of Math.Round using an overload:
Math.Round(2.5, 0, MidpointRounding.AwayFromZero); // gives 3
Math.Round(2.5, 0, MidpointRounding.ToEven); // gives 2

Wednesday, 23 November 2016


SQL Server

How to refresh Intellisense Cache to Update Schema Changes in Sql Server.

Some time we change column name of table or some schema changes in database
but visual studio still show red underline under that changed keyword as its cache is not getting reflected.



So to remove that red line follow this : 1. CTRL + SHIFT + R . This will clear the cache of the Intellisense and it removes the underline from the keyword. 

2.You can also refresh the Intellisense cache by using Edit -> Intellisense -> Refresh Local Cache.





Thursday, 23 June 2016


Checking the transaction that is not yet Committed in SQL Server Query


SELECT
trans.session_id AS [SESSION ID],
ESes.host_name AS [HOST NAME],login_name AS [Login NAME],
trans.transaction_id AS [TRANSACTION ID],
tas.name AS [TRANSACTION NAME],tas.transaction_begin_time AS [TRANSACTION BEGIN TIME],
tds.database_id AS [DATABASE ID],DBs.name AS [DATABASE NAME],
@@TRANCOUNT ActiveTransCount
FROM sys.dm_tran_active_transactions tas
JOIN sys.dm_tran_session_transactions trans
ON (trans.transaction_id=tas.transaction_id)
LEFT OUTER JOIN sys.dm_tran_database_transactions tds
ON (tas.transaction_id = tds.transaction_id )
LEFT OUTER JOIN sys.databases AS DBs
ON tds.database_id = DBs.database_id
LEFT OUTER JOIN sys.dm_exec_sessions AS ESes
ON trans.session_id = ESes.session_id
WHERE ESes.session_id IS NOT NULL


sp_who2
sp_lock

What is the difference between ports 465 and 587?


Ports 465 and 587 are intended for email client to email server communication (sending email).
Port 465 is for smtps
SSL encryption is started automatically before any SMTP level communication.
Port 587 is for msa
It is almost like standard SMTP port. MSA should accept email after authentication (e.g. after SMTP AUTH). It helps to stop outgoing spam when netmasters of DUL ranges can block outgoing connections to SMTP port (port 25).
SSL encryption may be started by STARTTLS command at SMTP level if server supports it and your ISP does not filter server's EHLO reply (reported 2014 Nov).

Monday, 13 June 2016

Cannot truncate table because it is being referenced by a FOREIGN KEY constraint



way to delete record without  disabling and enabling constraint

DELETE FROM TABLENAME
DBCC CHECKIDENT ('DATABASENAME.dbo.TABLENAME',RESEED, 0)

Monday, 6 June 2016

Find  all stored procedures or Function in a specific SQL Server schema 


Use the information_schema.routines (which is fairly standard across RDBMSs such as MSSQL,Mysql):
If your proc names start "tSQLt" then they are probably in a schema, so you can find them with:

select
    'PROCEDURE  [' + routine_schema + '].[' + routine_name + ']'
from 
    information_schema.routines where routine_name like 'tSQLt%' and routine_type = 'PROCEDURE'  --OR routine_type = 'FUNCTION' 

If you wanted to execute all these drop statements, you can build a single execute using FOR XML PATH as follows:
declare @sql varchar(max)

set @sql = (
select
    'DROP PROCEDURE [' + routine_schema + '].[' + routine_name + '] ' 
from 
    information_schema.routines where routine_schema = 'tSQLt' and routine_type = 'PROCEDURE'FOR XML PATH ('')
)

exec (@sql)

Could not drop object 'dbo.Table1' because it is referenced by a FOREIGN KEY constraint

1.Firstly, you need to drop your FK.
To get all foreign key relationships referencing your table, you could use this SQL

SELECT * 
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('TableName')


SELECT 
'ALTER TABLE ' +  OBJECT_SCHEMA_NAME(parent_object_id) +
'.[' + OBJECT_NAME(parent_object_id) + 
'] DROP CONSTRAINT ' + name
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('TableName')
2.Then drop the constraint.
3.Then Drop Table .

Monday, 30 May 2016

How to get Domain Name in Controller By URL:

string DomainURL = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port);



SQL Useful:

Rename Table Name by stored Procedure command
Exec sp_rename 'TblCountry', 'TblCountry_PMS'


http://www.howtogeek.com/50354/restoring-a-sql-database-backup-using-sql-server-management-studio/

Wednesday, 27 April 2016


To check if object is an array and loop over it in C#


 public object value { get; set; }

Recently stuck with to check object is array or not.  if an object is an array then convert that array to comm separated string .

if (objArray != null) 
 {
            string ElementValue = "";
           var objArrayType = objArray.GetType();
           if (objArrayType .IsArray)
          {
               foreach (object o in (Array)objArray)
             {
                     ElementValue = ElementValue + "," + o;
             }
                     ElementValue = ElementValue.Substring(1);
         }
          else
          {
                    ElementValue = Convert.ToString(item.value);
            }
}

Thursday, 21 April 2016

Important Articles :

1.List vs IEnumerable vs IQueryable vs ICollection vs IDictionary

http://www.codeproject.com/Articles/832189/List-vs-IEnumerable-vs-IQueryable-vs-ICollection-v

Monday, 28 March 2016

HTTP Error 500.21 - Internal Server Error


Handler "ExtensionlessUrlHandler-Integrated-4.0" has a bad module "ManagedPipelineHandler" in its module list

Most likely causes:
  • Managed handler is used; however, ASP.NET is not installed or is not installed completely.
  • There is a typographical error in the configuration for the handler module list.
Things you can try:
  • Install ASP.NET if you are using managed handler.
  • Ensure that the handler module's name is specified correctly. Module names are case-sensitive and use the format modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule".
Solution:

1.run the command prompt as administrator
2.type the line in the command prompt below any one

%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i
or
%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i

don't change the directory of cmd
3.At the command prompt, type the following command, and then press Enter:
  iisreset