Monday, December 20, 2010

payment through paypal in Asp.Net

A simple Way to payment through Paypal..
For this it is compulsory to login in your pay pal account.



Now come to the coding part ..  Take one text-box and a button

 protected void Button1_Click(object sender, EventArgs e)
    {
       protected decimal dec;(take this globally)
        try
        {
            dec = Convert.ToDecimal(TextBox1.Text);
                       
        }

        catch(Exception ex)
        {
            Label1.Visible = true;
        }
        paypalquery();
    }


 protected void paypalquery()
    {

//Make a class named it as "paypal"
        paypal pay = new paypal();
        pay.AccountEmail = paypal.Email;
        pay.PayPalBaseUrl = paypal.PayPalUrl;
        pay.Amount = dec;
        pay.ItemName = "New Item Added";

        pay.SuccessUrl = Request.Url + "?paypal=success";
        pay.CancelUrl = Request.Url + "?paypal=cancel";

        Response.Redirect(pay.submiturl());

        return;


    }



this is cs class

public class paypal
{


    public string LogoUrl = "";
    public string AccountEmail = "";
    public string BuyerEmail = "";
    public string SuccessUrl = "";
    public string CancelUrl = "";
    public string ItemName = "";
    public decimal Amount = 0.00M;
    public string InvoiceNo = "";


    public static string Email = "PayPalEmailAccount@YourCompany.com";
    public static string PayPalUrl = "https://www.sandbox.paypal.com/us/cgi-bin/webscr?";

    public string PayPalBaseUrl = "https://www.paypal.com/cgi-bin/webscr?";


public paypal()
{
//
// TODO: Add constructor logic here
//
}

    public string submiturl()
    {
        StringBuilder buil = new StringBuilder();

        buil.Append(PayPalBaseUrl + "cmd=_xclick&business=" + HttpUtility.UrlEncode(AccountEmail));

       if (Amount != null)
           buil.AppendFormat("&amount={0:f2}", Amount);

       if (ItemName != null && ItemName != "")
           buil.AppendFormat("&item_name={0}", HttpUtility.UrlEncode(ItemName));

       if (SuccessUrl != null && SuccessUrl != "")
           buil.AppendFormat("&return={0}", HttpUtility.UrlEncode(SuccessUrl));

       if (CancelUrl != null && CancelUrl != "")
           buil.AppendFormat("&cancel_return={0}", HttpUtility.UrlEncode(CancelUrl));

        return buil.ToString();
    }


    public class configuration
    {
        public static string email = "PayPalEmailAccount@YourCompany.com";
        public static string PayPalUrl = "https://www.sandbox.paypal.com/us/cgi-bin/webscr?";
    }


}

Difference b/w String and String Builder

Usually, String is immutable which means once you have the content there you cannot change it. While StringBuilder is mutable because it allows you to change the content you have already placed.
Notice that a string allows you to change the content but offcourse it creates a new string reference. For example, all string methods return a new string and don't update the same string variable.
When you have huge amount of concatenation, best recommendation to go for StringBuilder!
Example:
String and StringBuilder class stores strings. But when you cannot change a String object after creating one.
eg: String name = "Prasad";
By saying you cannot change the name object means you cannot change the value in name object internally. When you change the name object value to something else, a new String object is creating in memory and assign the new value.

eg: name = "Prasad Reddy";

A new name object is creating in memory and the value "Prasad Reddy" is assinging to the newly created space.

But StringBuilder class occupies the same space even if you change the value.

If you are doing string concatenation StringBuilder class is far better in performance than String class.

You can use StringBuilder's Append() method to use concatenation.


for example

String strSen = "hello";
strSen+= "Hello";


StringBuilder sbb = new StringBuilder();
sbb = sbb.Append("goldy");
sbb = sbb.Append("gupta");



Both are doing same thing but the process is faster in stringbuilder..

Wednesday, November 24, 2010

display number into words with use of sql function

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO



Create FUNCTION [dbo].[udf_Num_ToWords] (

@Number Numeric (38, 0)
) RETURNS VARCHAR(8000)

AS BEGIN

DECLARE @inputNumber VARCHAR(38)
DECLARE @NumbersTable TABLE (number CHAR(2), word VARCHAR(10))
DECLARE @outputString VARCHAR(8000)
DECLARE @length INT
DECLARE @counter INT
DECLARE @loops INT
DECLARE @position INT
DECLARE @chunk CHAR(3) -- for chunks of 3 numbers
DECLARE @tensones CHAR(2)
DECLARE @hundreds CHAR(1)
DECLARE @tens CHAR(1)
DECLARE @ones CHAR(1)

IF @Number = 0 Return 'Zero'

SELECT @inputNumber = CONVERT(varchar(38), @Number)
     , @outputString = ''
     , @counter = 1
SELECT @length   = LEN(@inputNumber)
     , @position = LEN(@inputNumber) - 2
     , @loops    = LEN(@inputNumber)/3

IF LEN(@inputNumber) % 3 <> 0 SET @loops = @loops + 1

INSERT INTO @NumbersTable   SELECT '00', ''
    UNION ALL SELECT '01', 'one'      UNION ALL SELECT '02', 'two'
    UNION ALL SELECT '03', 'three'    UNION ALL SELECT '04', 'four'
    UNION ALL SELECT '05', 'five'     UNION ALL SELECT '06', 'six'
    UNION ALL SELECT '07', 'seven'    UNION ALL SELECT '08', 'eight'
    UNION ALL SELECT '09', 'nine'     UNION ALL SELECT '10', 'ten'
    UNION ALL SELECT '11', 'eleven'   UNION ALL SELECT '12', 'twelve'
    UNION ALL SELECT '13', 'thirteen' UNION ALL SELECT '14', 'fourteen'
    UNION ALL SELECT '15', 'fifteen'  UNION ALL SELECT '16', 'sixteen'
    UNION ALL SELECT '17', 'seventeen' UNION ALL SELECT '18', 'eighteen'
    UNION ALL SELECT '19', 'nineteen' UNION ALL SELECT '20', 'twenty'
    UNION ALL SELECT '30', 'thirty'   UNION ALL SELECT '40', 'forty'
    UNION ALL SELECT '50', 'fifty'    UNION ALL SELECT '60', 'sixty'
    UNION ALL SELECT '70', 'seventy'  UNION ALL SELECT '80', 'eighty'
    UNION ALL SELECT '90', 'ninety'  

WHILE @counter <= @loops BEGIN

SET @chunk = RIGHT('000' + SUBSTRING(@inputNumber, @position, 3), 3)

IF @chunk <> '000' BEGIN
SELECT @tensones = SUBSTRING(@chunk, 2, 2)
    , @hundreds = SUBSTRING(@chunk, 1, 1)
    , @tens = SUBSTRING(@chunk, 2, 1)
    , @ones = SUBSTRING(@chunk, 3, 1)

IF CONVERT(INT, @tensones) <= 20 OR @Ones='0' BEGIN
SET @outputString = (SELECT word
                                      FROM @NumbersTable
                                      WHERE @tensones = number)
                   + CASE @counter WHEN 1 THEN '' -- No name
                       WHEN 2 THEN ' thousand ' WHEN 3 THEN ' million '
                       WHEN 4 THEN ' billion '  WHEN 5 THEN ' trillion '
                       WHEN 6 THEN ' quadrillion ' WHEN 7 THEN ' quintillion '
                       WHEN 8 THEN ' sextillion '  WHEN 9 THEN ' septillion '
                       WHEN 10 THEN ' octillion '  WHEN 11 THEN ' nonillion '
                       WHEN 12 THEN ' decillion '  WHEN 13 THEN ' undecillion '
                       ELSE '' END
                               + @outputString
   END
ELSE BEGIN
             SET @outputString = ' '
                            + (SELECT word
                                    FROM @NumbersTable
                                    WHERE @tens + '0' = number)
        + '-'
                             + (SELECT word
                                    FROM @NumbersTable
                                    WHERE '0'+ @ones = number)
                   + CASE @counter WHEN 1 THEN '' -- No name
                       WHEN 2 THEN ' thousand ' WHEN 3 THEN ' million '
                       WHEN 4 THEN ' billion '  WHEN 5 THEN ' trillion '
                       WHEN 6 THEN ' quadrillion ' WHEN 7 THEN ' quintillion '
                       WHEN 8 THEN ' sextillion '  WHEN 9 THEN ' septillion '
                       WHEN 10 THEN ' octillion '  WHEN 11 THEN ' nonillion '
                       WHEN 12 THEN ' decillion '   WHEN 13 THEN ' undecillion '
                       ELSE '' END
                            + @outputString
END

IF @hundreds <> '0' BEGIN
SET @outputString  = (SELECT word
                                      FROM @NumbersTable
                                      WHERE '0' + @hundreds = number)
           + ' hundred '
                                + @outputString
END
END

SELECT @counter = @counter + 1
    , @position = @position - 3

END

SET @outputString = LTRIM(RTRIM(REPLACE(@outputString, '  ', ' ')))
SET @outputstring = UPPER(LEFT(@outputstring, 1)) + SUBSTRING(@outputstring, 2, 8000)


RETURN @outputString END



Insert query

insert into table_3 values(1,100,dbo.udf_Num_ToWords(100) )


how to call

select dbo.udf_Num_ToWords(@Number)