0

First I read the TextBox1 using TextReader than tried to find a string 'flag' in TextBox1

 TextReader read = new System.IO.StringReader(TextBox1.Text);
            int rows = 5000;

            string[] text1 = new string[rows];
            for (int r = 1; r < rows; r++)
            {
                text1[r] = read.ReadLine();
            }

            string flag = "healthy";
            string[] readText = text1;
            foreach (string s in readText)
            {
                if ((s.Contains(flag) == true))
                {
                    TextBox2.Text = s.ToString();
                    break;
                }
                else
                {
                    TextBox2.Text = "Not Found";
                }
            }

than I got this error [enter image description here]

I want the program to find a keyword in a TextBox lines if the program finds it write the keyword with the whole line into another textbox TextBox2.

  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Selvin Nov 10 '22 at 13:53
  • No, cause I want to do this in Asp.Net C# and want to find a string value in TextBox and not a Gridview. When the teh algorithm finds the string in a whole text it should add the whole line( which contains the string too) to add in TextBox2... – user20465780 Nov 10 '22 at 14:03
  • What would be an easy method to find a string in a textbox and select the line and add the whole line to another textbox? (in ASP.NET C#) – user20465780 Nov 10 '22 at 14:09
  • `s` is null .. what did you not unerstand? [obviously because nature of `ReadLine`](https://dotnetfiddle.net/1ri8BG) – Selvin Nov 10 '22 at 14:09
  • Selvin sorry I am beginner and try to learn... how to change it to work? how to get the line from the text where is the string found? – user20465780 Nov 10 '22 at 14:14
  • take some course ... start with something simpler ... read the docuemntation ... – Selvin Nov 10 '22 at 14:15
  • I searched a lot today that`s why I am asking for help.... – user20465780 Nov 10 '22 at 14:17
  • if `s` is supposed to be a nullable string, I'd try `if ((s?.Contains(flag) == true))` // untested – Stefan Wuebbe Nov 10 '22 at 14:59

1 Answers1

0

I think you what to add "?" on s

to be like this:

if ((s?.Contains(flag) == true))
{ 
//..
Mohm Zanaty
  • 544
  • 7
  • 27