Tuesday, August 26, 2014

DataAnnotation in models in edmx

In edmx model we have:
namespace DatabaseManager
{
    using System;
    using System.Collections.Generic;
   
    public partial class Item
    {

        public string IDescription { get; set; }
        public string IName { get; set; }

    }
}

If I set DataAnnotation like [Required] on property then it will not persist after edmx update. So it set annotation permanently we have to override meta information this class and merge our custom class with this class.
In our custom model:

namespace DatabaseManager
{
    [MetadataType(typeof(metaClass))]
    public partial class Item
    {

        private class metaClass
        {

            [DisplayName("Description")]
            public string IDescription { get; set; }

            [DisplayName("Name")]
            [Required()]
            public string IName { get; set; }

         }
     }
}

Now you set data annotation in your custom class it will work properly and no need to worry about edmx update.

Tuesday, August 12, 2014

Column level encryption in SQL server 2008r2

USE YourDB
GO

--get the list of keys in current adtabase
select * from sys.symmetric_keys

--get the list of certificates in current database
select * from sys.certificates


--To store encrypted data in the table you have to change the datatype to varbinary(256)


--set the database level encryption password
CREATE MASTER KEY ENCRYPTION
BY PASSWORD = '343k6WJussssszurWi'
GO

--create a certificate by which we are going to encrypt or decrypt data later
CREATE CERTIFICATE EncryptTESTCert
WITH SUBJECT = 'Encrypt
TESTCert'
GO

--create symmetric key by using certificate
CREATE SYMMETRIC KEY
TESTTableKey
WITH ALGORITHM = AES_256 ENCRYPTION
BY CERTIFICATE Encrypt
TESTCert
GO



--EXAMPLE

--Open encryption
OPEN SYMMETRIC KEY
TESTTableKey DECRYPTION
BY CERTIFICATE Encrypt
TESTCert

--encrypt data the show that
DECLARE @ResultVarBinary varbinary(256)              
SET @ResultVarBinary = ENCRYPTBYKEY(KEY_GUID('
TESTTableKey'),'test')  
select @ResultVarBinary

--decrypt data then show that
DECLARE @ResultSTring varchar(max)
SET @ResultSTring = CONVERT(VARCHAR(max),DECRYPTBYKEY(@ResultVarBinary))
select @ResultSTring

--close current encryption
CLOSE SYMMETRIC KEY
TESTTableKey


--NOTE
--Use ENCRYPTBYKEY while You are inserting data
--Use DECRYPTBYKEY When you are fetching data from table



Ref:
http://blog.sqlauthority.com/2009/04/28/sql-server-introduction-to-sql-server-encryption-and-symmetric-key-encryption-tutorial-with-script/


To use it in a easier way I have created separate SP and functions:-


ALTER procedure [dbo].[OpenEncryption]
AS
BEGIN
    OPEN SYMMETRIC KEY TESTTableKey DECRYPTION
    BY CERTIFICATE EncryptPCCASCert
END




ALTER procedure [dbo].[CloseEncryption]
AS
BEGIN
    CLOSE SYMMETRIC KEY
TESTTableKey
END




ALTER FUNCTION [dbo].[EncryptData]
(
    -- Add the parameters for the function here
    @text varchar(max)
)
RETURNS varbinary(256)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @ResultVar varbinary(256)       
       
    SET @ResultVar = ENCRYPTBYKEY(KEY_GUID('TESTTableKey'),@text)   

    -- Return the result of the function
    RETURN @ResultVar

END




ALTER FUNCTION [dbo].[DecryptData]
(
    -- Add the parameters for the function here
    @text varbinary(256)
)
RETURNS varchar(max)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @ResultVar varchar(max)       
       
    if @text is null
        begin
            SET @ResultVar =''
        end
    else
        begin
            SET @ResultVar = CONVERT(varchar(max),DECRYPTBYKEY(@text))
        end

    if @ResultVar is null
    BEGIN
        SET @ResultVar = ''
    END
    -- Return the result of the function
    RETURN @ResultVar

END


Sunday, August 3, 2014

ASP.Net 4.5 automatic minification for js and css

To reduce page content size I used asp.net 4.5 default minification system in my MVC 4 web application.
Step 1
 As per step 1 just use-
BundleTable.EnableOptimizations = true;
to minify all bundled js and css with no effort.

Step 2

As per step 2 -
To make it functional set debug="false" in web.config file.

Friday, July 25, 2014

Getting the left section of current Url upto domain name in C#

I was creating product's full links dynamically to do this I need to have the left section of the current URL from http to end of domain name.

Example:

My full link:
http://pro-post.blogspot.in/2014/07/extended-dropdownlistfor-to-create.html

I need: 
http://pro-post.blogspot.in

To achieve this ->

string leftSection = System.Web.HttpContext.Current.Request.Url.OriginalString.Replace(
System.Web.HttpContext.Current.Request.Url.PathAndQuery, "")
leftSection = leftSection.Substring(0, leftSection.LastIndexOf(':'));

ref: Soumyadip [http://soumyadip-cooldips.blogspot.in]

Wednesday, July 16, 2014

Extended DropdownListFor to create colored options

I had a situation to create a drop down list with multicolored options according to some criteria.
Like a dropdown consist of users, suppose users who are Administrator they will be shown in Green, if Restricted User then Red.
I have created this with DropDownListFor, by which it can be implemented easyly in mvc applications.

Steps:
1. Install Nuget package installer in visual studio.
2. Open Nuget Package Console
3. Write- Install-Package DropDownWithColorLegend

Friday, June 27, 2014

Show a perticular element at top of view port using jquery


Requirement:
 Show a particular section on load of the page. only when query string present in URL
 for that page and where div id- showsection exist.


Solution:
  if(document.getElementById("showsection") != null) 
  {
    var str = document.URL.split('?')[1];
    if(str != undefined && str.length>0)
    {
      jQuery("html, body").animate({scrollTop:jQuery("#showsection").offset().top},1000);
    }
  }


In HTML page:
<div id="showsection"></div>
<div id="test">
    test document
</div>

Description:
By placing javascript code in document ready, whole page will be scrolled up and show "test".
You do not have to put anything inside "showsection" div