Question

This is just part of a C# windows form Application. When the user enters their information, checks the check box to save info and hits the payment button the information is saved to a .txt file. When user enters last name and clicks autofill if name is found in file it will auto fill the boxes but i'm having issues with this part. For some reason it skips the firstname box and enters the last name in the first name box the card number in the last name box, etc,etc. What am I doing wrong?

my Code:

private void Submitbutton_Click(object sender, EventArgs e)
{

if (BillcheckBox.Checked && CreditradioButton.Checked == true)
{
StreamWriter stream_writer = new StreamWriter("PaymentInfo.txt");

stream_writer.WriteLine(FirstNametextBox.Text);

stream_writer.WriteLine(LastNametextBox.Text);

stream_writer.WriteLine(CardtextBox.Text);

stream_writer.WriteLine(MonthcomboBox.Text);

stream_writer.WriteLine(YearcomboBox.Text);


//Closes the file
stream_writer.Close();

// Clears the Controls.
FirstNametextBox.Clear();

LastNametextBox.Clear();

CardtextBox.Clear();

MonthcomboBox.SelectedItem = null;

YearcomboBox.SelectedItem = null;

MessageBox.Show("Thank You for your purchase! Your billing information has been saved!");

}

if (CreditradioButton.Checked == true)

{
MessageBox.Show("Thank You for your purchase! You're billing information was not saved.");
}


if (CashradioButton.Checked == true)

{
//Enter order amount
double order = Convert.ToDouble(OrdertextBox.Text);

//Enter cash recieved
double cash = Convert.ToDouble(CashtextBox.Text);

//Calculates total
Outputlabel.Text = String.Format("${0:0.00}", order - cash);

MessageBox.Show("Thank You for your purchase! ");


//Clears controls
OrdertextBox.Clear();

CashtextBox.Clear();

Outputlabel.Text = "";
}
}
Payment Enter Payment Information O Credit Card First Name Last Name Doe Card Number 123456789 Cash Jane Enter Order T ot Tot

private void AutoFillbutton_Click(object sender, EventArgs e)

{

string line;

string name;

name = LastNametextBox.Text;

StreamReader stream_reader = new StreamReader("PaymentInfo.Txt");

while ((line = stream_reader.ReadLine()) != null)

{

if (line.Contains(name))

{

FirstNametextBox.Text = stream_reader.ReadLine();

LastNametextBox.Text = stream_reader.ReadLine();

CardtextBox.Text = stream_reader.ReadLine();

MonthcomboBox.Text = stream_reader.ReadLine();

YearcomboBox.Text = stream_reader.ReadLine();

}

}

}

Payment Enter Payment Information O Credit Card First Name Last Name 123456789 Card Number: 03 Cash Doe Enter Order To Enter

Payment Enter Payment Information O Credit Card First Name Last Name Doe Card Number 123456789 Cash Jane Enter Order T ot Total Card Type: Visa MasterCardAmerican Express Month Year Expiration Date: 03 ▼ 2022 Set as default billing method Auto Fill Make Payment Exit
Payment Enter Payment Information O Credit Card First Name Last Name 123456789 Card Number: 03 Cash Doe Enter Order To Enter Cash Amount Total Card Type: VisaMasterCardAmerican Express Month Year 20 ▼ Expiration Date Set as default billing method Auto Fill Make Payment Exit
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Below are the observations

When writing into the File :

  • When billing method checkbox and Credit radio button is checked then all the details like first name , last name , card number ,card type and expiration date will be written in file PaymentInfo.txt on the each line.
  • This means first line contains first name , second line contains last name , third line contains card number etc.

When reading data from the File :

  • When auto fill button is clicked trying to read each line from the file and checking for the name with line.Contains(name).
  • But what happen when this code runs at first time the line line = stream_reader.ReadLine() given in while loop will read the first line from file name PaymentInfo.txt which contains first name.
  • Then statement if (line.Contains(name)) check whether last name is present in this line then if will return false because last name is not present.
  • When second time while loop will read the next line and statement if line.Contains(name)) becomes true and will read stream_reader.ReadLine(); reading next line each time and hence getting the output shown in screen 2.

Solution :

  • Either write all details on one line with Write() method and then search for the last name in that line.
  • or otherwise write last name as a first line in the PaymentInfo.txt and then search for it like wise given in the question.
  • Another option is read records as per first name.

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
This is just part of a C# windows form Application. When the user enters their information, checks the check box to save info and hits the payment button the information is saved to a .txt file. When...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT