Solved

Values passing to paypal from C#

Posted on 2011-03-10
11
1,029 Views
Last Modified: 2012-05-11
Hi.

Can anyone work out why the values from my item_name and amount fields wont pass to paypal.

Many thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

public partial class OnlineAccounts_pay : System.Web.UI.Page
{
    private void getData(string number)
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection("Data Source=xxxxxxxxxxxxxxxx");
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT * from tblInvoices WHERE InvoiceNumber = @number", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

        sqlCmd.Parameters.AddWithValue("@number", Request.QueryString["number"]);
        sqlDa.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            amount.Value = dt.Rows[0]["InvoiceAmount"].ToString();
            item_name.Value = dt.Rows[0]["InvoiceNumber"].ToString();
        }
        connection.Close();
    } 

    
   protected void Page_Load(object sender, EventArgs e)
    {

        getData(Request.QueryString["number"]); 

        
    }
   

}

Open in new window


<input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="business" value="onlinepay@mysite.com" />
    <input type="hidden" name="currency_code" value="GBP" />
    <input type="hidden" name="return" value="http://www.mysite.com/InvoicePaid.aspx" />
    <input type="hidden" name="no_note" value="1" />
    
    


    Invoice Number: 
    <input type="text" name="item_name" ID="item_name" runat="server" readonly="readonly" />
        <br />
    Total Amount:  
    <input type="text" name="amount" ID="amount" runat="server" sreadonly="readonly" />
    <br /><br />
        <asp:Button ID="PayNow" runat="server" Text="PAY NOW!" CausesValidation="False" 
            PostBackUrl="https://www.paypal.com/cgi-bin/webscr" />

Open in new window



Payal screenshot
0
Comment
Question by:jonpwatson5
  • 6
  • 5
11 Comments
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 35106486
The problem is that is the item_name and amount should be the Hidden fields in the form.
e.g.
<input type="hidden" name="item_name" value="<%Response.Write (val_item_name)%>">
<input type="hidden" name="amount" value="<%Response.Write (val_amount)%>">

To pass the values to these hidden fields you can modify your code behind like below:
public string val_amount;
public string val_item_name;

if (dt.Rows.Count > 0)
        {
            val_amount.Value = dt.Rows[0]["InvoiceAmount"].ToString();
            val_item_name.Value = dt.Rows[0]["InvoiceNumber"].ToString();
        }
0
 

Author Comment

by:jonpwatson5
ID: 35117422
I'm now getting build errors.

Error      1      'string' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)      pay.aspx.cs      30      24

Error      2      'string' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)      pay.aspx.cs      31      27      

Error      3      ; expected      pay.aspx      21      
Error      4      ; expected      pay.aspx      22      


pay.aspx.cs

val_amount.Value = dt.Rows[0]["InvoiceAmount"].ToString();
val_item_name.Value = dt.Rows[0]["InvoiceNumber"].ToString();


'Value' is causing the error.



pay.aspx

<input type="hidden" name="item_name" value=" <% response.write (val_item_name) %>" >
<input type="hidden" name="amount" value="<%Response.Write (val_amount)%>" >


expected ; on these 2 lines?
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 35119609
sorry, it was a typo error, remove the value property as belows:

val_amount = dt.Rows[0]["InvoiceAmount"].ToString();
val_item_name = dt.Rows[0]["InvoiceNumber"].ToString();
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:jonpwatson5
ID: 35121572
Thanks.

That solved that bit, Been trying to get the front page to work as well but still getting the build erorrs with the ; parts?????
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 35121615
Any line numbers mentioned for errors ??
0
 

Author Comment

by:jonpwatson5
ID: 35121637
pay.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/OnlineAccounts/Site.master" AutoEventWireup="true" CodeFile="pay.aspx.cs" Inherits="OnlineAccounts_pay" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <p>
        Welcome to our e-payment service.</p>
    <p>
        For your security and safety, card payments are handled by our partner Paypal.<br />
        Please review the details below and click &#39;PAY NOW&#39; to proceed.</p>
    <p>
        Invoice Number:<asp:TextBox ID="txtInvoiceNumber" runat="server"></asp:TextBox>
    </p>
    <p>
        Invoice Amount:<asp:TextBox ID="txtInvoiceAmount" runat="server"></asp:TextBox>
    </p>
    <p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="onlinepay@mysite.com" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="return" value="http://www.mysite.com/InvoicePaid.aspx" />
<input type="hidden" name="no_note" value="1" />
<input type="hidden" name="item_name" value=" <% response.write (val_item_name) %>" >
<input type="hidden" name="amount" value="<%Response.Write (val_amount)%>" >
<asp:Button ID="btnPayNow" runat="server" Text="PAY NOW" 
            PostBackUrl="https://www.paypal.com/cgi-bin/webscr" />
    </p>
</asp:Content>

Open in new window


pay.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

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

    public string val_amount;
    public string val_item_name;

    protected void Page_Load(object sender, EventArgs e)
    {
        getData(Request.QueryString["number"]);
    }

    protected void getData(string number)
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=databasename;User ID=myuser;Password=mypassword");
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT * from tblInvoices WHERE InvoiceNumber = @number", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlCmd.Parameters.AddWithValue("@number", Request.QueryString["number"]);
        sqlDa.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            val_amount = dt.Rows[0]["InvoiceAmount"].ToString();
            val_item_name = dt.Rows[0]["InvoiceNumber"].ToString();

            txtInvoiceAmount.Text = dt.Rows[0]["InvoiceAmount"].ToString();
            txtInvoiceNumber.Text = dt.Rows[0]["InvoiceNumber"].ToString();

           

            

        }

    }
}

Open in new window


Errors:

Warning      1      Validation (XHTML 1.0 Transitional): Empty elements such as 'input' must end with />.      \OnlineAccounts\pay.aspx      24      2
Warning      2      Validation (XHTML 1.0 Transitional): Empty elements such as 'input' must end with />.      \OnlineAccounts\pay.aspx      25      2
Error      3      ; expected      OnlineAccounts\pay.aspx      24      
Error      4      ; expected      OnlineAccounts\pay.aspx      25      
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 35121659
Replace line number 24's text in ASPX as belows:
<input type="hidden" name="item_name" value=" <% response.write (val_item_name) %>" />

Replace line number 25's text in ASPX as belows:
<input type="hidden" name="amount" value="<%Response.Write (val_amount)%>" />
0
 

Author Comment

by:jonpwatson5
ID: 35121665
Error      1      ; expected      \OnlineAccounts\pay.aspx      24      
Error      2      ; expected      \OnlineAccounts\pay.aspx      25      
0
 
LVL 18

Accepted Solution

by:
Ajay Sharma earned 500 total points
ID: 35121740
Replace line number 24's text in ASPX as belows:
<input type="hidden" name="item_name" value=" <% Response.Write (val_item_name); %>" />

Replace line number 25's text in ASPX as belows:
<input type="hidden" name="amount" value="<%Response.Write (val_amount); %>" />
0
 

Author Comment

by:jonpwatson5
ID: 35121758
Brilliant, thats now working. Really appreciate the help :)
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 35121791
I am glad it's working now.

Alternative to the Response.Write, you can modify the code as:

<input type="hidden" name="amount" value="<%= val_amount %>" />
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now