-2

I have 2 files and in the 1st file, I have textbox where the user can enter a value. I want to take that value and bring it to the other file. In that program, I have the user entering a value also in a textbox. If the value they enter is higher than the other you get an error.

For example: File 2 ask how many snickers you want to eat and File 1 (ask the user how many snickers we have). If what you want to eat is more than what you have you get an error.

I'm not sure how I can get this value in the other file.

FILE 1:

protected void Page_Load(object sender, EventArgs e)
    { 
            //create string and set it equal to the textbox text
            string snickersInventory = txtInventorySnickers.Text;
            //create a int and turn the string into an int
            int stockSnickers = Int32.Parse(snickersInventory);

     }

FILE 2:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
      //Check to see if the stockSnickers is lower than the amount of Snickers Value
    }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Mark
  • 25
  • 6
  • Note that files can contain many namespaces and classes (generally they shouldn't, but they can) so perhaps you ought to say "class 1" and "class 2", if that's how your application is organised. Anyway, you don't have fields or properties, so even with an instance of "file 1", you can't access it from "file 2" because it only exists for the duration of the `Page_Load` event. Edit: And with the addition of the ASP.NET tag, that complicates it a little. I think John's comment below is the way to go. – ProgrammingLlama Jun 12 '19 at 00:15
  • 1
    Not ready to vote to close, but this looks like a duplicate of [Send data from one page to another](https://stackoverflow.com/questions/12768674/send-data-from-one-page-to-another/12768904) – John Wu Jun 12 '19 at 00:15

3 Answers3

0

I hope this is something similar to what you're interested in:

/* Program 1 - Save our snicker count to a file */
var snickerInventory = txtInventorySnickers.Text;

// No need to convert to number if you're going to write directly to file.
System.IO.File.WriteAllText(@"c:\snickersInventoryCount.txt", snickerInventory);


/* Program 2 - Try to eat some snickers. */
var howManySnickersToEat = int.Parse(txtInventorySnickers.Text);
var snickersOnHandFileData = System.IO.File.ReadAllText(@"c:\snickersInventoryCount.txt");
var snickersOnHand = int.Parse(snickersOnHandFileData);

if(snickersOnHand < howManySnickersToEat) { /* Do error stuff here */ }

You'll definitely want to add error handling when parsing strings to ints. Take a look at int.TryParse(), especially if you are working with user input.

That's basically the gist of a simple write to/reading from a file. This is fine if the programs aren't running at the same time. You'll have problems if you're trying to read/write from the file at the same time.

You might consider using a database instead of a file as a means to share information between two applications.

Joshua Sprague
  • 350
  • 2
  • 6
0

Declare your File1's Textbox Modifier as public .

System.Web.Application.File f = System.Web.Application.["WebForm1"];

And in File2 Declare the following:

protected void btnOn_Click(object sender, EventArgs e)
    {
      var howMany = txt_howManySneakers.text;     // File1
      var EatSneakers = ((WebForm1)f).txtbox1.Text;  // File2

      if (EatSneaker > howMany )
        {
           // DO ERROR
        }
    }
Syafiqur__
  • 531
  • 7
  • 15
0

very simple, put the user entered value in a session object in file 1 like below

//create string and set it equal to the textbox text
string snickersInventory = txtInventorySnickers.Text;
//create a int and turn the string into an int
int stockSnickers = Int32.Parse(snickersInventory);

Session["stockSnickers"]=stockSnickers;

and get above value in file 2 like below

if(Session["stockSnickers"] != null)
{
   int stockSnickers = Convert.ToInt32(Session["stockSnickers"]);
   // do something with above value
}

I hope it helps....:)

Hussain Md
  • 119
  • 1
  • 11