0

I have an input file saved in a specified folder and used a stream reader to read the content one line at a time. So my program reads the first line and adds the content in the textbox. I wanted to get the first line of the text file, and use it as an input for the program via textbox. So my question would be how do I read a textbox content that was displayed from an input file and use it as an input for the program.

            string inputFile = @"c:\temp\ScrappedData\input.txt";
            Numbers(inputFile);

            //gets the contents of the input file loaded by Numbers method
            string lakeName = textBox1.Text;

            string partialWebAdd = "http://www.google.com?=";

            //Gets the url entered by the user from the textbox
            string url = partialWebAdd + lakeName;
            MessageBox.Show(url.ToString());
            //string url = textBox1.Text;

So, Numbers(inputFile) loads the first line from input.txt(which is some numerical value that matches data in the website) and the website address displays good. but when I actually try to scrape it, webclient is not reading the input of the textbox. Do you know why? I tried using same code without getting input from the text file, rather typed whole website data into the box and it works fine. Thank you for your ideas. Below is the part of code for Number Method and webrequest.

string sourceCode = GetSource.getSourceCode(url);



            //Marks the start point of scrape 
            int startIndex = sourceCode.IndexOf("Name:");

            //Marks the endpoint of the html to scrape
            int endIndex = sourceCode.IndexOf("For more information");

            //Gets the string between the specified startIndex and endIndex
            MessageBox.Show(startIndex.ToString()+ "dang bro" +endIndex.ToString());
            sourceCode = sourceCode.Substring(startIndex, endIndex - startIndex);





            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //Write the contents of the webpage to the file specified in path #162
            StreamWriter sWriter = new StreamWriter(path);
            sWriter.Write(sourceCode);
            MessageBox.Show("Contents have been Scrapped!");

            textBox1.Clear();
            sWriter.Close();

Webrequest class:

class GetSource
{
    public static string getSourceCode(string url)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        string sourceCode = streamReader.ReadToEnd();

        streamReader.Close();
        response.Close();
        return sourceCode;
    }
}

}

Number Method:

public void Numbers(string filename)
   {


       string input;

       if (System.IO.File.Exists(filename) == true)
       {
           System.IO.StreamReader objectReader;
           objectReader = new System.IO.StreamReader(filename);
           while ((input = objectReader.ReadLine()) != null)
           {
               Single output;
               if (Single.TryParse(input, out output))
               {
                   textBox1.AppendText(output.ToString());
                   MessageBox.Show("input to the textbox from input.txt was successful");


               }
               else
               {

               }
           }
           objectReader.Close();

       }
       else
       {
           MessageBox.Show("No Such File" + filename);
       }
   }
Try_Learning
  • 31
  • 1
  • 10
  • Show the code where you use the WebClient. – CodeCaster Mar 02 '15 at 15:42
  • I will add the code but it is working fine when I try input manually. The problem is that the program is not reading the input from the text file. For instance, I tried copying the complete website address in the text file. But the input was not read from the textbox. So the issue is with textbox not accepting the contents loaded as an input during runtime. I will post the code though if that helps. Thanks – Try_Learning Mar 02 '15 at 15:49
  • Then your problem description is not clear. There's nothing special about reading a textbox's text, it's just a string property of an object. If the problem is in reading the file, show the contents of your (poorly-named if I may say) `Numbers()` method. – CodeCaster Mar 02 '15 at 15:50
  • I have added the code you asked for. – Try_Learning Mar 02 '15 at 17:27

0 Answers0