Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everybody,

I'm trying to make a C# code that finds out if the input from the textbox field is already in a TXT file.
Currently I have the code below to store the Input from the textbox.

What I want is a check that go's through the TextSA.txt file and only checks for the ChildOrderInput.Text and if it already exists in that TXT file. The Date and OrderInputText should be ignored in this check.

C#
File.AppendAllText("textSA.txt", DateTime.Now.ToString("dd-MM-yyyy") + OrderInputText + " " + ChildOrderInput.Text + "\n" + Environment.NewLine); }

Do you guys have any ideas on how to do this?

Also if the check finds a duplicate it should give an error to the user that the ChildorderInput.Text is already in the file and then send the DateTime.Now back to the user.

Hope anyone can help me!

Thanks!

grtz Robert

What I have tried:

File.AppendAllText("textSA.txt", DateTime.Now.ToString("dd-MM-yyyy") + OrderInputText + " " + ChildOrderInput.Text + "\n" + Environment.NewLine); }
Posted
Updated 17-Aug-17 9:06am
v2
Comments
PIEBALDconsult 17-Aug-17 21:13pm    
It's almost CSV, so why not use CSV and then access it as if it were a database?
BillWoodruff 18-Aug-17 3:09am    
While you have two solid answers to your question ... taken literally ... now, I wonder why you are using reading/writing a file for this ... an "expensive" process.

If you want to see an example that uses efficient in-memory storage, just ask.

Try:
C#
string strTheFile = File.ReadAllText(@"D:\Test Data\textSA.txt");
if (strTheFile.Contains(ChildOrderInput.Text))
    {
    Console.WriteLine("Found!");
    }
 
Share this answer
 
You can do this:

  1. Read the file line-by-line.
  2. For each file:

    1. Split into two parts by " " and take the second part. That gives you the previously saved ChildOrderInput. Note: this will not work of OrderInputText can possibly contain spaces; because, if that is possible, a space is not a clear separator for OrderInputText and ChildOrderInput.Text. So if OrderInputText can contain spaces, use another separator than " ".
    2. If that saved ChildOrderInput equals the current one, break the loop and show the error.

(As a side note, I don't see the point of "\n" + Environment.NewLine - if you want two newlines, you better use Environment.NewLine + Environment.NewLine)
C#
bool displayError = false;
using (StreamReader sr = new StreamReader("textSA.txt"))
{
    while (sr.Peek() > -1)
    {
        string line = sr.ReadLine();
        if (!string.IsNullOrWhiteSpace(line))
        {
            string parts = line.Split(new char[] { ' ' }, 2);
            if (parts.Length < 2) { /* the line isn't in the correct format; handle it correctly if it can happen */ }

            string previousChildOrderInput = parts[1];
            if (previousChildOrderInput.Equals(ChildOrderInput.Text))
            {
                displayError = true;
                break;
            }
        }
    }
}
if (displayError)
{
    // display error

    // Displaying it here rather than inside the loop makes sure that the file is already closed before the error message is displayed.
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900