Wednesday, March 20, 2013

Replace Html Tags in c#

using System.Text.RegularExpressions;

///
/// Removes any html tags - used mainly on meta description for Products.
/// Expects: string.
/// Returns: string.
///

public class ReplaceHTMLTags
{
    public static string Strip(string text)
    {
        return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
    }
}

Transaction Rollback concept in SQL server

CREATE Procedure [dbo].[spUserInsert] -- 'sahil1233','sahil122.enest@gmail.com','','lakhwant','singh','#57,urban estate','ludhiana','punjab','india','144001',12323,'1234567',0,1,1,1,1,1,0,1,1    
(
    @UserId int,                                             
     @UserName nvarchar(50),
    @EmailAddress nvarchar(50),
    @CompanyName nvarchar(50),
    @FirstName nvarchar(50),
    @Surname nvarchar(50),
    @Address nvarchar(250),
    @City nvarchar(50),
    @States nvarchar(50),
    @Country nvarchar(50),
    @Zipcode nvarchar(50),
    @Phone nvarchar(50),
    @Password nvarchar(50),
    @IsAccountVerified int,
    @IsUserInvited int
   
)
AS
 BEGIN
   /*=============================================================================               
    * Constants               
    *============================================================================*/         
 declare                
       @SUCCESS             smallint,               
       @FAILED              smallint,               
       @ERROR_SEVERITY      smallint,               
       @ERROR_STATE1        smallint,               
       @theErrorMsg         nvarchar(4000),               
       @theErrorState       int,               
       @CompanyId           int,
       @RetGuid             uniqueidentifier = NEWID()
      
  
   select                
       @SUCCESS = 0,                
       @FAILED  = -1,                
       @ERROR_SEVERITY = 11,               
       @ERROR_STATE1 = 1 
      
Begin Try
 begin transaction
    if(@IsUserinvited<>1)
        Begin
            if not exists(select 1 from dbo.MT_Company where companyName=@companyName)
                begin
                    if not exists(select 1 from MT_Users where  UserName=@UserName)
                        begin
                            Insert into dbo.MT_Company (CompanyName,CreatedBy,CreatedDate,IsCompanyEnabled,EnforceMileageRate)
                            values(@CompanyName,@UserName,GETDATE(),1,1)
                            set @CompanyId=@@IDENTITY
                                       
                            Insert into  dbo.MT_Users (UserName,EmailAddress,FirstName,Surname,[Address],City,States,
                            Country,ZipCode,Phone,[Password],[IsAccountVerified],[MileageCalculationAllow],IsDeleted,CreateBy,CreateDate,UserGuid,companyID,IsCompanyAdmin)values
                           (@UserName,@EmailAddress,@FirstName,@Surname,@Address,@City,@States,@Country,
                            @Zipcode,@Phone,@Password,@IsAccountVerified,1,0,@UserName,GETDATE(),@RetGuid,@CompanyId,1)
                            set @UserId=@@IDENTITY
                       
                            Insert into dbo.MT_Transaction (CompanyId,UserId,EnableUser,[CurrentMonth],CurrentYear)values
                            (@CompanyId,@UserId,1,MONTH(getdate()),Year(getdate()))
           
                           SELECT  Convert(nvarchar(max),@RetGuid) As InsertedID
                        end
                    else
                        begin
                            select Convert(nvarchar(50),0) as InsertedID
                        end
                end
            else
                begin
                    select Convert(nvarchar(50),1) as InsertedID                   
                end
               
               
        End
    else
        begin
                declare @InvitedCompanyId int
           
                select @InvitedCompanyId=CompanyId from dbo.MT_Company where CompanyName=@CompanyName
           
            if not exists(select 1 from MT_Users where  UserName=@UserName)
                begin
                    Insert into  dbo.MT_Users (UserName,EmailAddress,FirstName,Surname,[Address],City,States,
                    Country,ZipCode,Phone,[Password],[IsAccountVerified],[MileageCalculationAllow],IsDeleted,CreateBy,
                    CreateDate,UserGuid,companyID,IsCompanyAdmin)values
                    (@UserName,@EmailAddress,@FirstName,@Surname,@Address,@City,@States,@Country,
                    @Zipcode,@Phone,@Password,1,1,0,@UserName,GETDATE(),@RetGuid,@InvitedCompanyId,0)
               
                    SELECT  Convert(nvarchar(max),@RetGuid) As InsertedID
                end
            else
                begin
                    select Convert(nvarchar(50),0) as InsertedID
                end
       
        end       
  commit transaction     
End Try
    begin catch               
          if @@trancount > 0   
              rollback transaction   
          set @theErrorMsg = error_message()               
          set @theErrorState = error_state()               
          raiserror (@theErrorMsg, @ERROR_SEVERITY, @theErrorState)               
          return (@FAILED)               
    end catch  
  

                                 
     

END

Image Resize in C# code behind

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

///
/// Resizes any images passed to it and created a thumbnail if wanted.
/// Expects: string, int, int, bool, stream.
///

public class ImageResizer
{
    public static void Resize(string filePath, int maxWidth, int maxHeight, Stream buffer)
    {
        Image image = Image.FromStream(buffer);

        float origimgWidth = image.PhysicalDimension.Width;
        float origimgHeight = image.PhysicalDimension.Height;
        float imgWidth = origimgWidth, imgHeight = origimgHeight;
        float imgSize, imgResize = 0;

        //resize by width
        if ((imgWidth > imgHeight))
        {
            imgSize = imgWidth;
            imgResize = imgSize <= maxWidth ? (float)1.0 : maxWidth / imgSize;
        }
        else
            //resize image based on height it the height and width are the same or if the height is greater than the width
            if ((imgHeight > imgWidth))
            {
                imgSize = imgHeight;
                imgResize = imgSize <= maxWidth ? (float)1.0 : maxHeight / imgSize;
            }
            else
                if ((imgHeight == imgWidth))
                {
                    imgSize = imgHeight;
                    imgResize = imgSize <= maxWidth ? (float)1.0 : maxHeight / imgSize;
                }

        imgWidth *= imgResize;
        imgHeight *= imgResize;

        //save the image using the dimensions gathered above
        ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[1];
        EncoderParameters eParams = new EncoderParameters(1);
        eParams.Param[0] = new EncoderParameter(Encoder.Quality, 80L);
        Bitmap bmp = new Bitmap((int)imgWidth, (int)imgHeight);
        Graphics gr = Graphics.FromImage(bmp);
        gr.SmoothingMode = SmoothingMode.HighQuality;
        gr.CompositingQuality = CompositingQuality.HighQuality;
        gr.InterpolationMode = InterpolationMode.High;
        Rectangle rectDestination = new Rectangle(0, 0, (int)imgWidth, (int)imgHeight);
        gr.DrawImage(image, rectDestination, 0, 0, origimgWidth, origimgHeight, GraphicsUnit.Pixel);
        bmp.Save(filePath, codec, eParams);
        bmp.Dispose();
        image.Dispose();
    }
}

fill in blanks

ALL Reading Blanks: Special All approaches aim to increase blood flow to areas of tension and to release painful knots opt1 muscle kn...