validate first line of a textbox

Hi Experts,
  How can i modify my script to validat first line of textbox.
My current code is

 private void Btnupload_click(object sender, System.EventArgs e) {
           
            StreamWriter writer = new StreamWriter("C:\\temp.txt");
            writer.WriteLine(TextBox1.Text);
            writer.Close();
                   
        }      

so on Btnupload, the code save the content of the textbox to a file, i want to verify that the first line is always "pharmgkbnumber      date      techid      sampleid      samplesource      -288      note      fail_to_amp" .

Any suggestion?

thanks,

sudhir

sudAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PeterFearnCommented:
using System.Text.RegularExpressions;

 private void Btnupload_click(object sender, System.EventArgs e) {
           
  Regex re = new Regex("\Apharmgkbnumber     date     techid     sampleid     samplesource     -288     note     fail_to_amp$")

 if (!re.Match(TextBox1.Text).Success)
    return;

// will have left function now if no match

            StreamWriter writer = new StreamWriter("C:\\temp.txt");
            writer.WriteLine(TextBox1.Text);
            writer.Close();
                   
        }    


you might need to rejig the regular expression in the Regex constructor

\A = start of text
$ = end of line
sudAuthor Commented:
Hi PeterFearn,

What do you mean by " you might need to rejig the regular expression in the Regex constructor
\A = start of text
$ = end of line
" ??

I am getting error message : unrecgonized escape sequence at the code "Regex re=......";

Here is my code that i am using,

 private void Btnupload_click(object sender, System.EventArgs e) {
            Regex re = new Regex("\Apharmgkbnumber     date     techid     sampleid     samplesource     -288     note     fail_to_amp$");
     if (!re.Match(TextBox1.Text).Success)
       return;
            StreamWriter writer = new StreamWriter("C:\\temp.txt");
            writer.WriteLine(TextBox1.Text);
            writer.Close();
                   
        }

Thanks,

Sudhir
PeterFearnCommented:
sorry, my mistake, put @ before the string in the regex constructor

Regex re = new Regex (@"\Apharmgkbnumber     date     techid     sampleid     samplesource     -288     note     fail_to_amp$");
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Bob LearnedCommented:
Do you need regular expression validation, or are you just looking to compare strings?

If you need regular expression validation, then I would imagine a different expression.

Otherwise you could a comparison like this:
   if (this.textBox1.Lines(0).StartsWith(text))
   {
   }

Bob
sudAuthor Commented:
Hi Experts,
   I want to validate the first line of the textbox is "pharmgkbnumber      date      techid      sampleid      samplesource      -288      note      fail_to_amp"

user can enter as many lines as they want but the first line should be this.so when they click submit button i need to validate the first line, after that i can allow it to write it to a text file.

I am using the code below, it is running but not doing what it is supposed to do.

 private void Btnupload_click(object sender, System.EventArgs e) {
            Regex re = new Regex(@"\A pharmgkbnumber     date     techid     sampleid     samplesource     -288     note     fail_to_amp $");
     if (!re.Match(TextBox1.Text).Success)
       return;
            StreamWriter writer = new StreamWriter("C:\\temp.txt");
            writer.WriteLine(TextBox1.Text);
            writer.Close();
                   
        }

Thanks,

sudhir
      
Bob LearnedCommented:
If you just need string comparison, then you should keep it simple, and use the way that I describe.

Bob
sudAuthor Commented:
Hi TheLearnedOne,
  I am trying to use ur code but i am getting error 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Lines'      


Here is my code:

private void Btnupload_click(object sender, System.EventArgs e)
        {
            if (this.TextBox1.Lines(0).StartsWith("pharmgkbnumber     date     techid     sampleid     samplesource     -288     note     fail_to_amp"))
            {
                StreamWriter writer = new StreamWriter("C:\\temp.txt");
                writer.WriteLine(TextBox1.Text);
                writer.Close();

            }

        }

Thanks,

sudhir
PeterFearnCommented:
Bob is quite right, i'm over compliating things....

but the reason the regular expression is not working is because of the spaces after the \A and before the $

and possibly because of the spaces between the words, try this instead (copy it exactly)

Regex re = new Regex(@"\Apharmgkbnumber\s+date\s+techid\s+sampleid\s+samplesource\s+-288\s+note\s+fail_to_amp$");

\s+ matches one or more space characters (just incase your column delimiters are tabs)
sudAuthor Commented:
Hi PeterFearn
  the program is running but it doesnot give desired output.

Here is my code:

 private void Btnupload_click(object sender, System.EventArgs e)
        {
            Regex re = new Regex(@"\Apharmgkbnumber\s+date\s+techid\s+sampleid\s+samplesource\s+-288\s+note\s+fail_to_amp$");
             if (!re.Match(TextBox1.Text).Success)
              return;
                StreamWriter writer = new StreamWriter("C:\\temp.txt");
                writer.WriteLine(TextBox1.Text);
                writer.Close();
       

        }

And i am entering in the textbox

Line1:  pharmgkbnumber      date      techid      sampleid      samplesource      -288      note      fail_to_amp
Line2: dsdfsdfsdfsdfdsfsdfds

it should take it as Line1 is same, but it does not take it so nothing get written to temp.text file.

Apprecaite your help. Thanks,

sudhir

      
dffdsfsd
PeterFearnCommented:
what might be easier could be this

if (TextBox1.Text.IndexOf("pharmgkbnumber     date     techid     sampleid     samplesource     -288     note     fail_to_amp") != 0)
  return;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
Man, you people gotta be more specific about your situation. *GRIN*  There have been so many times that I have gone down a certain road, just to find out that it was the wrong one (this is not a mind reading site).

      int firstReturn = this.TextBox1.Text.IndexOf("\r");
      string lineText = this.TextBox1.Text.Substring(0, firstReturn);

Bob
sudAuthor Commented:
Hi Peter,
  Your current solution is working.Thanks,

Sudhir
sudAuthor Commented:
Hi Bob,
   I understand. Sorry for any inconvenience. Though i am able to get the result using peter code i was trying your method and here is the code:

private void Btnupload_click(object sender, System.EventArgs e)
        {
                       int firstReturn = this.TextBox1.Text.IndexOf("\r");
            string lineText = this.TextBox1.Text.Substring(0, firstReturn);
            if (this.TextBox1.Lines(0).StartsWith("pharmgkbnumber     date     techid     sampleid     samplesource     -288     note     fail_to_amp"))
            {
                StreamWriter writer = new StreamWriter("C:\\temp.txt");
                writer.WriteLine(TextBox1.Text);
                writer.Close();
            }
           
        }

It still give me errorError      System.Web.UI.WebControls.TextBox' does not contain a definition for 'Lines'      
Thanks ,

Sudhir
PeterFearnCommented:
Hi Sudhir,

Bob's solution works, but for System.Windows.Forms.TextBox (which contains a definition for Lines) rather than System.Web.UI.WebControls.TextBox (which doesn't)

If you're happy to, I'd recommend splitting the points...

also, you may find the ASP.NET topic a better place to post .net questions when they relate to web work, as there's usually an assumption in this topic that the questions are relating to windows forms or command line apps

cheers

Pete
sudAuthor Commented:
Thanks for the suggestion,

Sudhir
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.