Help with an error in my code.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 772 Views - Last Post: 22 October 2013 - 10:05 PM Rate Topic: -----

#1 delina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-October 13

Help with an error in my code.

Posted 20 October 2013 - 12:00 PM

Hello everyone,

I got such great help last time, I figured I could ask another question. This week I am writing an address book application in C# using Visual Studio Windows Form Application.

My design is as follows:
I have a name label called nameLabel
I have a name textbox called nameTextBox
I have an EMail Address label called emailLabel
I have an EMail textbox called emailTextBox
I have an Add Contact button called button1
I have a contacts label called contactsLabel
I have a Refresh button called button2
I have a list box called listBox1

In this assignment I have to enter a contact and save it to a .txt file by clicking the Add Contact button. Then by clicking the Refresh button I have to retrieve the .txt file and display all the contact currently in the .txt file in the list box.

Here is my code so far:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Lab7A
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void nameLabel_Click(object sender, EventArgs e)
        {

        }

        private void nameTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void emailLabel_Click(object sender, EventArgs e)
        {

        }

        private void emailTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name, email;

            try
            {
                using (StreamWriter storeInfo = new StreamWriter("info.txt", true))
                {
                    name = nameTextBox.Text;
                    email = emailTextBox.Text;

                    storeInfo.WriteLine(name + "|" + email);
                    this.button2.Visible = true;
                    nameTextBox.Text = string.Empty;
                    emailTextBox.Text = string.Empty;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Error!\n" + exc.Message);
            }
        }

        private void contactsLabel_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string info;
            string [ ] recordData = new string [2];
            string cname = string.Empty;
            string cemail = string.Empty;
            string outInfo = string.Empty;;

            try
            {
                using (StreamWriter getInfo = new StreamWriter("info.txt"))
                {
                    listBox1.Items.Clear();
                    while ((info = getInfo.ReadLine()) != null)
                    {
                        recordData = info.Split('|');
                        outInfo = recordData[0] + "     " +
                            String.Format("{0:c}", double.Parse(recordData[1]));
                        listBox1.Items.Add(outInfo);
                        listBox1.Visible = true;
                    }
                }
            }
            catch (Exception exc)
            {
                listBox1.Text = exc.Message;
            }
        }
    }
}



I am new at using files so I am sure my code is not perfect. If you have any suggestions at what I can fix or work on feel free to let me know. The error I am getting is in the while loop. It says the ReadLine is not in the System.IO.StreamWriter definition.

Thanks so much in advance!
:001_icon16:

Is This A Good Question/Topic? 0
  • +

Replies To: Help with an error in my code.

#2 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 229
  • Joined: 28-August 13

Re: Help with an error in my code.

Posted 20 October 2013 - 12:05 PM

Why are you using a streamWRITER in the button2 click event, when you need to READ data from a file.
What you need is a streamREADER object.

http://msdn.microsof...reamreader.aspx



EDIT: also, for your while loop while reading in data, the while condition should be..(sr = the StreamReader object btw)

...

while(!sr.EndOfStream){ ... }


which basically means "continue reading until we're at the end of the file".

This post has been edited by Rhino1111: 20 October 2013 - 12:14 PM

Was This Post Helpful? 1
  • +
  • -

#3 delina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-October 13

Re: Help with an error in my code.

Posted 20 October 2013 - 12:13 PM

View PostRhino1111, on 20 October 2013 - 12:05 PM, said:

Why are you using a streamWRITER in the button2 click event, when you need to READ data from a file.
What you need is a streamREADER object.

http://msdn.microsof...reamreader.aspx


Wow, I must be very tired. I am not sure why I was using StreamWriter. I am going to fix that and see if I have any further questions.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 delina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-October 13

Re: Help with an error in my code.

Posted 20 October 2013 - 12:24 PM

Okay, so I fixed the StreamWriter and now I have this problem.

First, the textboxes and the list box are completely transparent when I debug.
Second, what I enter into the textboxes saves to the .txt file, but when I click the Refresh button it is not working. Nothing is showing up in the list box.

Any ideas?

This post has been edited by andrewsw: 20 October 2013 - 12:47 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Help with an error in my code.

Posted 20 October 2013 - 12:44 PM

Use a MessageBox within your loop to check whether it is reading the file correctly:

    MessageBox.Show(outInfo);

There is a debugging tutorial linked in my signature which would enable you to step through your code.

BTW

Quote

I have a Refresh button called button2

You went to the trouble to rename your other controls, why not all of them?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Help with an error in my code.

Posted 20 October 2013 - 12:50 PM

Quote

First, the textboxes and the list box are completely transparent when I debug.

Are you saying they are not transparent when your application is run without debugging?

But there is nothing in your code above that would cause this, more likely some property, or properties, that you have changed for these controls (or something that a rebuild might resolve).

FWIW:
Build solution vs. Rebuild solution vs. Clean solution Menu

This post has been edited by andrewsw: 20 October 2013 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 delina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-October 13

Re: Help with an error in my code.

Posted 20 October 2013 - 03:23 PM

View Postandrewsw, on 20 October 2013 - 12:50 PM, said:

Quote

First, the textboxes and the list box are completely transparent when I debug.

Are you saying they are not transparent when your application is run without debugging?

But there is nothing in your code above that would cause this, more likely some property, or properties, that you have changed for these controls (or something that a rebuild might resolve).

FWIW:
Build solution vs. Rebuild solution vs. Clean solution Menu


When I just look at my design tab everything seems to be fine. It must be something I changed on accident. I will try and rebuild to get that fixed.

View Postandrewsw, on 20 October 2013 - 12:44 PM, said:

Use a MessageBox within your loop to check whether it is reading the file correctly:

    MessageBox.Show(outInfo);

There is a debugging tutorial linked in my signature which would enable you to step through your code.

BTW

Quote

I have a Refresh button called button2

You went to the trouble to rename your other controls, why not all of them?


Thank you for trying to help, it means a lot. I am just learning. I have been doing well so far, but this lab I am definitely having trouble. I read the whole chapter, but the book mostly gives console examples and that really doesn't help all that much.

I will try and use the MessageBox and let you know what I am getting.

I renamed all of them except the buttons because I liked the button names the way they were. I can remember which button was 1 and 2, the rest were a bit harder. Haha!
Was This Post Helpful? 0
  • +
  • -

#8 delina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-October 13

Re: Help with an error in my code.

Posted 20 October 2013 - 03:36 PM

Okay, I inserted the message box, but the message box came up blank. So I am definitely having trouble retrieving the data.

I think my trouble is in the array:

 private void button2_Click(object sender, EventArgs e)
        {
            string [ ] recordData = new string [2];
            string cname = string.Empty;
            string cemail = string.Empty;
            string outInfo = string.Empty;;

            try
            {
                using (StreamReader getInfo = new StreamReader("info.txt"))
                {
                    listBox1.Items.Clear();
                    while (!getInfo.EndOfStream)
                    {
                        outInfo = recordData[0] + "     " +
                            String.Format("{0:c}", double.Parse(recordData[1]));
                        listBox1.Items.Add(outInfo);
                        listBox1.Visible = true;
                    }
                }
            }
            catch (Exception exc)
            {
                listBox1.Text = exc.Message;
            }


I am not sure how to display the name and the email address. I used an example out of the book, but the book used numbers.

This post has been edited by andrewsw: 20 October 2013 - 03:37 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Help with an error in my code.

Posted 20 October 2013 - 03:44 PM

You have changed your code. You are no longer reading lines from the file, nor are you populating recordData using Split, so it remains empty. You were nearer with your previous code, after changing the second StreamWriter to a StreamReader.

BTW There is no need to keep quoting a previous post, there is a larger Reply button a little further down the page.

This post has been edited by andrewsw: 20 October 2013 - 03:46 PM

Was This Post Helpful? 1
  • +
  • -

#10 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 229
  • Joined: 28-August 13

Re: Help with an error in my code.

Posted 20 October 2013 - 04:22 PM

Your problem is your actually never grabbing any data from the text file.
Let's take a look at lines 10-20 in your code, where the work for reading in the data from the text file should be happening.

 using (StreamReader getInfo = new StreamReader("info.txt")) // You create a StreamReader object on info.txt

               {

                   listBox1.Items.Clear();  // You clear your listbox

                   while (!getInfo.EndOfStream) //You enter a while loop until you reach the end of the file

                   {

                       outInfo = recordData[0] + "     " + String.Format("{0:c}", double.Parse(recordData[1])); // This assignment is .. null + "     ", and your trying to parse another null value to a double. These values are null because you never actually assign anything to your recordData array after instantiating it. I'm pretty sure trying to parse a null value will throw you into the catch block.

                       listBox1.Items.Add(outInfo);  

                       listBox1.Visible = true; //no need for this in the while loop. should be after the loop.

                   }

               }



As you can see from the comments, you're never reading in any data in your while loop.

basically what you need to do is make a call to getInfo.ReadLine() in your while loop and store that into a string. You can then parse that string if you're using delimiters to format your data. You can then use those string[] entries to build up your "outInfo" string, or do whatever you need to do with them.

so it would look something like this...

mystring = getInfo.ReadLine()
recordData = mystring.Split(delimiter char array, stringsplitoptions)
build your outInfo string
Add it to listbox

This post has been edited by Rhino1111: 20 October 2013 - 04:29 PM

Was This Post Helpful? 1
  • +
  • -

#11 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3808
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Help with an error in my code.

Posted 20 October 2013 - 04:31 PM

View PostRhino1111, on 20 October 2013 - 11:22 PM, said:

basically what you need to do is make a call to getInfo.ReadLine() in your while loop and store that into a string. You can then parse that string if you're using delimiters to format your data. You can then use those string[] entries to build up your "outInfo" string, or do whatever you need to do with them.

so it would look something like this...

mystring = getInfo.ReadLine()
recordData = mystring.Split(delimiter char array, stringsplitoptions)
build your outInfo string
Add it to listbox

The OP had these lines in his/her first post, but later removed them.

This post has been edited by andrewsw: 20 October 2013 - 04:33 PM

Was This Post Helpful? 0
  • +
  • -

#12 delina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-October 13

Re: Help with an error in my code.

Posted 22 October 2013 - 06:03 PM

View PostRhino1111, on 20 October 2013 - 04:22 PM, said:

Your problem is your actually never grabbing any data from the text file.
Let's take a look at lines 10-20 in your code, ..


Thanks for helping. I took the code back a little, and tried playing around with the array values. I still can't get the list box to show anything. I have no idea how to fix the array.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Lab7A
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void nameLabel_Click(object sender, EventArgs e)
        {

        }

        private void nameTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void emailLabel_Click(object sender, EventArgs e)
        {

        }

        private void emailTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name, email;

            try
            {
                using (StreamWriter storeInfo = new StreamWriter("info.txt", true))
                {
                    name = nameTextBox.Text;
                    email = emailTextBox.Text;

                    storeInfo.WriteLine(name + "|" + email);
                    this.button2.Visible = true;
                    nameTextBox.Text = string.Empty;
                    emailTextBox.Text = string.Empty;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Error!\n" + exc.Message);
            }
        }

        private void contactsLabel_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string info;
            string [ ] recordData = new string [2];
            string cname = string.Empty;
            string cemail = string.Empty;
            string outInfo = string.Empty;;

            try
            {
                using (StreamReader getInfo = new StreamReader("info.txt"))
                {
                    listBox1.Items.Clear();
                    while (!getInfo.EndOfStream)
                    {
                        info = getInfo.ReadLine();
                        recordData = info.Split ('|');
                        outInfo = recordData[1] + "     " +
                            String.Format("{0:c}", double.Parse(recordData[1]));
                        listBox1.Items.Add(outInfo);
                    }
                    
                    listBox1.Visible = true;

                }
            }
            catch (Exception exc)
            {
                listBox1.Text = exc.Message;
            }
        }
    }
}


This post has been edited by andrewsw: 23 October 2013 - 03:14 AM
Reason for edit:: Reduced large quote

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Help with an error in my code.

Posted 22 October 2013 - 07:08 PM

If you set a breakpoint on line 97, are you actually seeing items added to the listbox?
Was This Post Helpful? 0
  • +
  • -

#14 delina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 13-October 13

Re: Help with an error in my code.

Posted 22 October 2013 - 07:34 PM

View PostSkydiver, on 22 October 2013 - 07:08 PM, said:

If you set a breakpoint on line 97, are you actually seeing items added to the listbox?


No, the list box stays completely blank the whole time. I checked the .txt file and everything is saving properly, I just can't seem to utilize the list box and make anything show up.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Help with an error in my code.

Posted 22 October 2013 - 08:06 PM

But is it hitting the breakpoint? What values for outInfo are you seeing?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2