Makai Studio
Imi ‘Ike - Seeker Of Knowledge
Imi-?ike

ASP.Net and Paypal buy now button: Offer discount at checkout in code behind

November 19, 2009 08:17 by jdelpay

This is a very simple way to offer a discount at chckout for customers. It is more secure than the javascript way of doing it because the code is “hidding”. Following my previous tutorial called: “ASP.Net: adding a Paypal Buy Now in code behind.

Solution
On your default.aspx page add a button from the toolbox. Then under the button add a textbox with an id of couponCodeTXT.
Double click the button you just added to open the default.aspx.cs page (code Behind)
Add and customize the code below.

 

   1: using System;
   2: using System.Collections;
   3: using System.Configuration;
   4: using System.Data;
   5: using System.Linq;
   6: using System.Web;
   7: using System.Web.Security;
   8: using System.Web.UI;
   9: using System.Web.UI.HtmlControls;
  10: using System.Web.UI.WebControls;
  11: using System.Web.UI.WebControls.WebParts;
  12: using System.Xml.Linq;
  13:  
  14: public partial class cc_Default : System.Web.UI.Page
  15:  
  16: {
  17:  
  18: protected void Page_Load(object sender, EventArgs e)
  19:  
  20:     {         
  21:  
  22:    }   
  23:  
  24:  protected void Button1_Click(object sender, EventArgs e) 
  25:  
  26:    {    
  27:  
  28:    const string Server_URL = "https://www.paypal.com/cgi-bin/webscr?";
  29:    const string return_URL = "http://www.macarteverte.com/default.aspx";
  30:    const string cancelreturn_URL ="http://www.macarteverte.com/fail.aspx";
  31:    string cmd = "_xclick";
  32:    string business ="PCZNNK7K5A6MS";
  33:  
  34:    string item_name = "Loterie pour la carte verte";
  35:    double baseamt = 49.00;
  36:    int add = 1;        
  37:  
  38: //Simple way to add 10% discount coupon code
  39:  
  40:        string coupon = "mcv1756"; 
  41:        string couponCode = couponCodeTXT.Text; 
  42:        double amount;
  43:  
  44:        if (couponCode == coupon)
  45:  
  46:         {   
  47:  
  48:          amount = 49.00 - (49.00 * 10.00) / 100.00;
  49:  
  50:     } 
  51:  
  52:        else
  53:  
  54:        {
  55:  
  56:     amount = 49.00;
  57:  
  58:     } 
  59:  
  60:        //End coupon
  61:  
  62:        double shipping = 0.00 ;
  63:        double handling = 0.00;
  64:        int no_shipping = 1;
  65:        int no_note= 1;
  66:        string currency_code = "EUR";
  67:        string lc = "FR";
  68:        string bn = "PP-BuyNowBF";
  69:        string basedes = "ILCV 49.00" ;
  70:        string custom = User.Identity.Name;
  71:        string redirect="";
  72:        redirect+= Server_URL;
  73:        redirect += "cmd=" + cmd;
  74:        redirect += "&business=" + business;
  75:        redirect += "&item_name=" + item_name; 
  76:        redirect += "&baseamt=" + baseamt;
  77:        redirect += "&add=" + add; 
  78:        redirect += "&amount=" + amount;
  79:        redirect += "&shipping=" + shipping;
  80:        redirect += "&handling=" + handling;
  81:        redirect += "&no_shipping=" + no_shipping;
  82:        redirect += "&no_note=" + no_note;
  83:        redirect += "&currency_code=" + currency_code;
  84:        redirect += "&lc=" + lc;
  85:        redirect += "&bn=" + bn;
  86:        redirect += "&basedes=" + basedes;
  87:        redirect += "&return=" + return_URL;
  88:        redirect += "&cancel_return" + cancelreturn_URL;
  89:  
  90:        //Redirect to the payment page 
  91:  
  92:        Response.Redirect(redirect); 
  93:    }
  94: }
  95:  
  96:  

Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

ASP. Net : Adding a Paypal Buy Now in code behind

August 31, 2009 03:58 by jdelpay

Problem:

You can't have HTML form tags inside aspx form tags. The problem is that ASP.NET pages are forms themselves, surrounded by a single <form Runat="Server"> tag that automatically posts back to itself. Therefore, you cannot embed an HTML form inside this server form.
Shown below is typical code for a PayPal "BuyNow" button.

 

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="dradams@dradamsweb.com">
<input type="hidden" name="item_name" value="ASP.NET 2.0 Tutorial">
<input type="hidden" name="item_number" value="WDS03">
<input type="hidden" name="amount" value="52.00">
<input type="hidden" name="return" 
  value="http://www.dradamsweb.com/default.aspx">
<input type="hidden" name="cancel_return" 
  value="http://www.dradamsweb.com/default.aspx">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" border="0" name="submit"
  src="https://www.paypal.com/en_US/i/btn/x-click-but01.gif" 
  alt="Make payments with PayPal - it's fast, free and secure!">
</form>


Solution

On your default.aspx page add a button from the toolbox
Double click the button you just added to open the default.aspx.cs page (code Behind)
Add and customize the code below

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; 


public partial class cc_Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
const string Server_URL = "https://www.paypal.com/cgi-bin/webscr?";
const string return_URL = "http://www.PageWhenOk/default.aspx";
const string cancelreturn_URL = "http://www.PageWhenCancel.com/cc.fail.aspx"; 


string cmd = "_xclick";
string business ="your business email";
string item_name = "Name of the item";
double baseamt = 49.00;//amount
int add = 1;
double amount = 49.00 ;
double shipping = 0.00 ;
double handling = 0.00;
int no_shipping = 1;
int no_note= 1;
string currency_code = "EUR"; //Replace with country Currency code
string lc = "FR";
string bn = "PP-BuyNowBF";
string basedes = "ILCV 49.00" ;

string redirect="";
redirect+= Server_URL;
redirect += "cmd=" + cmd;
redirect += "&business=" + business;
redirect += "&item_name=" + item_name;
redirect += "&baseamt=" + baseamt;
redirect += "&add=" + add;
redirect += "&amount=" + amount;
redirect += "&shipping=" + shipping;
redirect += "&handling=" + handling;
redirect += "&no_shipping=" + no_shipping;
redirect += "&no_note=" + no_note;
redirect += "&currency_code=" + currency_code;
redirect += "&lc=" + lc;
redirect += "&bn=" + bn;
redirect += "&basedes=" + basedes;
redirect += "&return=" + return_URL;
redirect += "&cancel_return" + cancelreturn_URL;
//Redirect to the payment page
Response.Redirect(redirect);

}
}



Voila!


Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Using Arrays - C# Tutorial and Sample - Part 1

February 8, 2008 19:20 by jdelpay

An Array is an unordered sequence of elements. All the elements in an array have the same type, followed by a pair of square brackets, followed by the variable name.
   

int[] pinNumbers; //personal identification numbers


note: C and C++ programmers should note that the size of the array is not part of thedeclaration.


Arrays are reference type
The size of an array does not have to be a constant, it can be calculated at run time.

int size = int.Parse(Console.ReadLine());
    int[] pinNumbers = new int[size];


Its possible to create arrays of size 0. Useful in situations where the size is determined dynamically. Array with size 0 is not a null array.


Initializing Array Variable
i.e Initializing pinNumbers to an array of 4 int with the following values: 9,3,7,2

 

int[] pinNumber = new int[4] {9,3,7,2};

// the number in the curly bracket must match the size of the array


the value within the curly brace do not have to be constants.
   

Random r = new Random();

int[] pinNumber = new int[4]{ r.next() %10, r.next() %10,

r.next() %10, r.next() %10};


note: The system.Random class is a pseudo -random number generator. The Next method returns a nonnegative random number.


When initializing an array, you can omit the 'new' expression and the size of the array:

int[] pinNumbers {9,7,3,2};

   
Accessing Individual Array Elements

  To access an individual array element you must provide an index indicating which element you require.

int myNumber; 
 
    myNumber =  pinnumbers[3] ; 
 

you can also change the contents of an array by assigning a value to an indexed element:

myNumber = 167; 
 
pinnumbers[2] = myNumber; 
 

the initial element in an array is indexed 0 (zero) and not 1.

All array elements access is bounds-checked. when using an integer index that is less than 0 or greater than or equal to the lengh of the array, the compiler throws an indexOutOfRangeException

try 
 
    { 
 
        int[] pinnumbers ={9,3,7,2}; 
 
         Console.WritLine(pinNumber[4]); //error, the 4th element is at index 3 
 
   } 
 
   catch(indexOutOfRangeException ex) 
 
   { 
 
      ... 
 
   }

Visit:Makai Studio

AddThis Social Bookmark Button

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.Net | C#
Actions: E-mail | Permalink | Comments (14) | Comment RSSRSS comment feed

Creating Value Types With Enumerations C# Sample

February 6, 2008 04:17 by jdelpay

Here is how to declare an Enumeration type called team whose literal values are the name of soccer clubs.


    enum team {Barcelona, Marseille, Arsenal, Galaxy}

Note: Internally, an enumeration associates an integer value with each element. By default, the numbering start at 0for the first element and goes up

{Barcelona(0), Marseille(1), Arsenal(2), Galaxy(1)}

Using an Enumeration:


enum team {Barcelona, Marseille, Arsenal, Galaxy}
    class Soccer
{

public void Method(team parameter)
{
    team localVariable;

}
    private team currentTeam;
}

Note: Before you can use the value of an enumeration variable, it must be assigned a value. You can only assign valid values to an enumeration. For Example

team french = team.Marseille;
Console.writeLine(french) //writes out 'Marseille'

Note: You have to write team.french rather than french

If needed you can explicictly convert an enumeration variable to a string that represents its current value.

string country = french.toString();
console.WriteLine(country); //also writes out 'Marseille'

To retreive the Underlying Integer value of an Enumeration:

enum team {Fconnection, Marseille, Arsenal, Galaxy}

team french = team.Marseille;
Console.WriteLine((int)french) //writes out '1'

Choosing Enumeration Literal Value:


    enum team {Barcelona=1, Marseille=2, Arsenal=3, Galaxy=4}

Choosing Enumeration's Underlying Type:


    enum team: short {Barcelona, Marseille, Arsenal, Galaxy}

note: The main reason of doing this is to save memory an int occupies more memory than a short
        int range of value = -2147483648 and 2147483647
        short range of value = -32768 and 32767

 


Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.Net
Actions: E-mail | Permalink | Comments (6) | Comment RSSRSS comment feed

Converting String to Int in a Method With C#

January 28, 2008 06:47 by jdelpay

In the sample below, I will show you how to convert a string to int inside a method 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace method001
{
    class Program
    {
        static int calculDob(int a)
        {
            int year = 2008 - a; //calculation
            return year; // the value being returned

        }
        static void Main(string[] args)
        {
            Console.WriteLine("How old are you? ");// asking for some data
            int age = Convert.ToInt16(Console.ReadLine());// convert the entry to a int type and assign the value to the age variable
            Console.WriteLine("You were born in: {0}",calculDob(age));

        }
    }
}

 


Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.Net
Actions: E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

PHP Vs ASP.Net

January 18, 2008 07:18 by MarkLea


Do a Google search for "PHP vs ASP.NET" (or "ASP.NET vs PHP") and the first hit you'll get is a seemingly well-written Oracle article. When the article was first published, the .NET community certainly made note of it.  There's actually a follow up article by the same author, but he only skirts around the mistakes he made and never really corrects them.

Macromedia might write documentation with horrible code examples, but Oracle flat out lies. Shouldn't we expect more from Oracle? There's a fine print at the bottom of the article that says "The opinions expressed by this author are entirely his own and do not reflect the position of Oracle or any other corporation." That's of little value when it shows up on oracle.com and as the first hit on Google.

Honestly, who would ever buy a product/service from Oracle or iHeavy Inc. (the author's employer) when they clearly don't know what they are talking about? Sean Hull's article shows that opinions can be wrong, and ignorance is still king. I'm calling Sean out and daring him to prove me wrong!

Object Oriented:
So according to Sean, PHP 5 and ASP.NET both have "strong" object oriented capabilities. It's true that PHP5 language supports every important OO concept, but that's just one part of the equation. Anyone remotely familiar with the ASP.NET or WinForms control model knows that for .NET, being object oriented goes beyond the language.  In ASP.NET, html markup is represented as server-side objects. This provides the ability to easily program against controls as well as extend them.  Gone are the day of procedural includes, you now include an object you can program against. This is a non trivial difference that's at the core of the two platforms.  PHP5 has really good OO support, but compared to ASP.NET it isn't remotely close .


Exceptions:
Both PHP5 and ASP.NET support exceptions. Funny how your PHP code uses db_check_errors() for it's "exception handling". Is that how you handle exceptions in PHP? Funnier still is that your ASP.NET example doesn't have any exception handling (or the fact that this ASP.NET example uses Console.WriteLine). If you ignore the fact that PHP5 doesn't have a finally (or using), which is pretty minor, the fact is a number of built-in functions don't throw exceptions nearly as much as they should. Let's take a look at the PHP documentation for opening a connection to oracle:

$conn = oci_connect('hr', 'hr', 'orcl');
if (!$conn) {
   $e = oci_error();
   print htmlentities($e['message']);
   exit;
}

So PHP5 has good support for exceptions, but you'll mostly be checking return values to see if an exceptional situation occurred. Joel Spolsky might switch to PHP.


Speed and Efficiency:
PHP5 is quicker than ASP.NET because Sean said so. No benchmark code is provided. It just is! It's really hard to argue against such a phantom point.  I will say that PHP5 doesn't have a caching API out of the box or support for OutputCaching (you need a Pear library for that sort of stuff).  Oh ya, PHP5 is also more efficient, but there's no explanation as to what that means.


Price:
Any price comparison that doesn't take into account total cost of ownership is incomplete. Sean himself admits that "PHP is the quick and dirty type of solution". I know the TOC argument is thrown around by Microsofties a lot. But any experienced developer knows that maintenance cost can far outweigh all other costs. If PHP really is a quick and dirty (and it really is), shouldn't a sane business owner be worried about how easily and quickly they'll be able to maintain it?

I know this is an old article and most have let it go, but I feel duty bound to clear up some of the FUD. I work with PHP daily, and I do like it.  I plan on making a post about why you would pick PHP over ASP.NET (ie, if you are using the SqlDataSource, just switch to PHP now). But Sean clearly has no idea what he's talking about.

 


Visit:Makai Studio

AddThis Social Bookmark Button

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Generating Random Numbers in the .NET Framework using C#

December 13, 2007 05:32 by jdelpay

for a sample on a working form : http://www.makaistudio.com/inquiry.aspx 

Random numbers may be generated in the .NET Framework by making use of the Random class. This class may be instantiated using the following code:

//Create a new Random class in C#
Random RandomClass = new Random();
Random Integers

Once the class has been instantiated, a random integer can be obtained by calling the Next method of the Random class:

//C#
int RandomNumber = RandomClass.Next();

The value of RandomNumber will, therefore, be assigned a random whole number between 1 and 2,147,483,647.

In most coding situations, it is more desirable to create a random number within a certain size range. In this case, the Next method should be called with two arguments: the minimum value and the maximum value. For example, the following assigns RandomNumber to a value that is greater or equal to 4 and less than 14:

//C#
int RandomNumber = RandomClass.Next(4, 14);

Note that an ArgumentOutOfRangeException will be raised if the minimum value is larger than the maximum value.

It is also possible to specify just the maximum value using a different constructor. The following will return a return a random integer that is greater or equal to 0 and less than 14:

//C#
int RandomNumber = RandomClass.Next(14);

Again, an ArgumentOutOfRangeException will be raised if the maximum value is smaller than 0.
Random Floating Point Numbers

As well as returning random integers, the Random class can also return floating point numbers. The NextDouble method returns a random number as a Double. The random number's value is always greater or equal to 0.0, and less than 1.0:

//Create a random double using C#
double RandomNumber = RandomClass.NextDouble();

by joel delpay


Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.Net
Actions: E-mail | Permalink | Comments (15) | Comment RSSRSS comment feed

Send Email in ASP.Net C# Sample

November 15, 2007 06:48 by jdelpay

The form is located at www.makaistudio.com/inquiry.aspx For the date picker i used http://www.basicdatepicker.com/bdplite/ its free!

The Code:  

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class quoteonline : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    } 

    protected void btnSendmail_Click(object sender, EventArgs e)

    {

        // we are using System.Net.Mail.SmtpClient in 2.0

        SmtpClient smtpClient = new SmtpClient("smtp.yourdomain.com");
        smtpClient.Credentials = new System.Net.NetworkCredential("contact@yoursite.com", "password");
        MailMessage message = new MailMessage();

      try

        {

            MailAddress fromAddress = new MailAddress(emailtxt.Text, nametxt.Text);  
            // You can specify the host name or ipaddress of your server
 
            // Default in IIS will be localhost
            //smtpClient.Host = "smtp.yourSMTPinfo.com";
            //If SMTP server requires a secure connection or the authenticated 
            //Default port will be 25

            smtpClient.Port = 25; 
            //From address will be given as a MailAddress Object 
            message.From = fromAddress; 
            // To address collection of MailAddress 
            message.To.Add(contact@yoursite.com);
            message.Subject = "Quote"

            // CC and BCC optional 
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress(admin1@yoursite.com)
            //message.CC.Add("info@yoursite.com");
            //message.CC.Add("sales@yoursite.com");  
            // You can specify Address directly as string 
            //message.Bcc.Add(new MailAddress(admin2@yoursite.com)); 
            //message.Bcc.Add(new MailAddress("admin3@yoursite.com")); 

            //Body can be Html or text format

            //Specify true if it  is html message

            message.IsBodyHtml = false

            // Message body content

            message.Body = "Name: "+nametxt.Text+"\n"+"Email: "+emailtxt.Text+"\n"+"Business: "+businesstxt.Text+"\n"+"Phone: "+phonetxt.Text+"\n"+"Current Website: "+currentwebsitetxt.Text+"\n"+"Service Needed: "+CheckBoxList1.SelectedValue+"\n"+"Project Deadline: "+BDPLite1.SelectedDate+"\n"+"Adds on or Modules: "+CheckBoxList2.SelectedValue+" "+CheckBoxList3.SelectedValue+" "+CheckBoxList4.SelectedValue+"\n"+"Preferred Site: "+siteyoulike1txt.Text+" "+siteyoulike2txt.Text;

              // Send SMTP mail

            smtpClient.Send(message); 
            lblStatus.Text = "Email successfully sent."
            Response.Redirect("thankyou.aspx?Name=" + nametxt.Text);// Read my tutorial on how to xfer pages values to another                       

 

        }

        catch (Exception ex)

        {

            lblStatus.Text = "Send Email Failed." + ex.Message;

        }

    }

    protected void BDPLite1_SelectionChanged(object sender, EventArgs e)

    {

        BDPLite1.DateFormat = "d";

        Response.Write(BDPLite1.SelectedDateFormatted);

    }


Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.Net
Actions: E-mail | Permalink | Comments (30) | Comment RSSRSS comment feed

ASP.NET 2.0 - Transferring Page Values to Another Page - C# Sample

November 7, 2007 02:58 by jdelpay

Introduction

As developper We always come into situations in which we need to transfer values from one page to another page. In this article, I will show you some ways of transferring values from one page to another page. You can see how this work by visiting http://www.makaistudio.com/inquiry.aspx.

Using Response.Redirect

Let's first see how to transfer using Response.Redirect method. Let's says you have a text box with data in it when you Press the finish submit or finish button.

Tip: Sometimes we want to transfer to another page inside the catch exception, meaning exception is caught and we want to transfer to another page. If you try to do this, it may give you a System.Threading exception. This exception is raised because you are transferring to another page leaving behind the thread running. You can solve this problem using:

Response.Redirect("WebForm.aspx",false);

This tells the compiler to go to page "WebForm.aspx", and "false" here means that don't end what you were doing on the current page. You should also look at the System.Threading class for threading issues. Below, you can see the C# code of the button event. "txtName" is the name of the text field whose value is being transferred to a page called "WebForm.aspx". "Name" which is just after "?" sign is just a temporary response variable which will hold the value of the text box.

private void Button1_Click(object sender, System.EventArgs e)
{
    // Value sent using HttpResponse
    Response.Redirect("WebForm.aspx?Name="+txtName.Text);
}


Now, where do I collect the values? First, we check that the value entered is not null. If it's not, then we simply display the value on the page using a Label control. Note: When you use Response.Redirect method to pass the values, all the values are visible in the URL of the browser. You should never pass credit card numbers and confidential information via Response.Redirect.

if (Request.QueryString["Name"]!= null)
    Label3.Text = Request.QueryString["Name"];


Visit:Makai Studio

AddThis Social Bookmark Button

Currently rated 4.2 by 10 people

  • Currently 4.2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5