I have a (probably stupid) question I hope someone can help me with.. So, I set a variaible beased on a form in put in the code behind, but then, a few lines down when I go to call it , it has a undefinded value. I don't understand.. Below is the code: The problem is that when i try to reference theamount on the line I marked "PROBLEM HERE" is that when I call the API, the value for theamount is undefinded.. even though it should have a value, and does above...

please help if you can... right now i have 5 if statements with the code in all, when i only need one and the amt value set above it... ERGH...

using System;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Net;
using System.IO;

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

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        Response.Write("<br>Page has been posted back.");

        //send payment info to api, send them an email, go to thank you page

            var ccex = Request.Form["ExpMon"] + Request.Form["ExpYear"];    
            var date1 = System.DateTime.Now.ToString();
            var pt = Request.Form["plantype"];

            Response.Write("<BR>ccex = " + ccex);        

            Response.Write("pt.ToString() = " + pt.ToString());
            if (pt.ToString() == "Gold")            
            { 
            var theamount = "150.10";
            }
            else
            {
            var theamount = "205.08";
            }

            String post_url = "XXX/transact.dll";           
                    Dictionary<string, string> post_values = 
                                new Dictionary<string, string>();

                                post_values.Add("x_login", "XXX");
                                post_values.Add("x_tran_key", "XXX");
                                post_values.Add("x_delim_data", "TRUE");
                                post_values.Add("x_delim_char", "|");
                                post_values.Add("x_relay_response", "FALSE");

                                post_values.Add("x_type", "AUTH_CAPTURE");
                                post_values.Add("x_method", "CC");
                                post_values.Add("x_card_num", Request.Form["ccnum"]);
                                post_values.Add("x_exp_date", ccex);

    PROBLEM HERE                            post_values.Add("x_amount", theamount); 

                                post_values.Add("x_description", "Plan Purchase: " + Request.Form["plan"] + ": " + Request.Form["plantype"]);

                                post_values.Add("x_first_name", Request.Form["fname"]);
                                post_values.Add("x_last_name", Request.Form["lname"]);
                                post_values.Add("x_address", Request.Form["addr"]);
                                post_values.Add("x_email", Request.Form["emaila"]);
                                post_values.Add("x_state", Request.Form["state"]);
                                post_values.Add("x_zip", Request.Form["zip"]);

                             'etc code goes on.....



    }
}

You are declaring your variable inside the curly brackets. This is the scope of the variable.

Move it onto line 25 with

string theamount;

then assign it in your if statements as you are but remove the "var "

but i need to set the value based on the returned value of the var pt.TOString()...

based on a value, i need to charge either 10, 20, 30, 40, 50 etc.

so i need an IF ELSE statement to determine the amount to be charged before i pass values off the the ecommerce api

Thats ok but variables have scope. In C# if you declare a variable inside { } then it can only be used there. You need to declare it elsewhere as below:

using System;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Net;
using System.IO;

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

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Response.Write("<br>Page has been posted back.");

            //send payment info to api, send them an email, go to thank you page

            var ccex = Request.Form["ExpMon"] + Request.Form["ExpYear"];
            var date1 = System.DateTime.Now.ToString();
            var pt = Request.Form["plantype"];
            string theamount;

            Response.Write("<BR>ccex = " + ccex);

            Response.Write("pt.ToString() = " + pt.ToString());

            if (pt.ToString() == "Gold")
            {
                theamount = "150.10";
            }
            else
            {
                theamount = "205.08";
            }

            String post_url = "XXX/transact.dll";

            Dictionary<string, string> post_values = new Dictionary<string, string>();

            post_values.Add("x_login", "XXX");
            post_values.Add("x_tran_key", "XXX");
            post_values.Add("x_delim_data", "TRUE");
            post_values.Add("x_delim_char", "|");
            post_values.Add("x_relay_response", "FALSE");

            post_values.Add("x_type", "AUTH_CAPTURE");
            post_values.Add("x_method", "CC");
            post_values.Add("x_card_num", Request.Form["ccnum"]);
            post_values.Add("x_exp_date", ccex);

            post_values.Add("x_amount", theamount);

            post_values.Add("x_description", "Plan Purchase: " + Request.Form["plan"] + ": " + Request.Form["plantype"]);

            post_values.Add("x_first_name", Request.Form["fname"]);
            post_values.Add("x_last_name", Request.Form["lname"]);
            post_values.Add("x_address", Request.Form["addr"]);
            post_values.Add("x_email", Request.Form["emaila"]);
            post_values.Add("x_state", Request.Form["state"]);
            post_values.Add("x_zip", Request.Form["zip"]);
        }
    }
}

Good, now re-read my original comments and it will all make sense!

it does :) ty for help

Youre welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.