Simple ASP.NET question about .cs pages

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1296 Views - Last Post: 29 April 2011 - 09:24 AM Rate Topic: ***** 2 Votes

#1 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Simple ASP.NET question about .cs pages

Posted 14 April 2011 - 11:48 AM

In a web application I'm developing, I'm trying to link up a method from a .cs document to the page, Default.aspx. However, I don't know how to access it from the pages code.

I have a button on my page, and when it's clicked it tries to run

searcher((String)txtStartDate.Text, (String)txtEndDate.Text);


but it gives me a compilation error when I load the page.

searcher is in Default.aspx.cs, which looks like this:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace firstApplication
{
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            //100% working

            DateTime today = DateTime.Now;

            txtEndDate.Text = today.ToShortDateString();
            txtStartDate.Text = today.Date.AddMonths(-6).ToShortDateString();

        }

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

        public String searcher(String start, String end)
        {

            string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.log");
            int i = 0;

            while (i <= filePaths.Length)
            {
                string fName = @filePaths[i];
                StreamReader testTxt = new StreamReader(fName);
                string allread = testTxt.ReadToEnd();
                testTxt.Close();
                string regMatch = "abcdefghijklmnopqrstuvwxyz";

                if (Regex.IsMatch(allread, regMatch))
                {
                    Console.WriteLine("found\n");
                }
                else
                {
                    Console.WriteLine("not found\n");
                }
            }

            return null;
            
        }
    }
}



So, what do I do to let my main page access searcher? The code language is C# by the way.

This post has been edited by Erber: 14 April 2011 - 11:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Simple ASP.NET question about .cs pages

#2 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Simple ASP.NET question about .cs pages

Posted 14 April 2011 - 12:32 PM

Can you supply us with the compile error


BTW , no need to cast

this

searcher((String)txtStartDate.Text, (String)txtEndDate.Text);

should be this

searcher(txtStartDate.Text, txtEndDate.Text);
Was This Post Helpful? 1
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Simple ASP.NET question about .cs pages

Posted 14 April 2011 - 01:04 PM

if this is an asp:Button control then the eventhandler expects two parameters, an object and the EventArgs. You do not need to pass the text from your textboxes as parameters as you can access the textbox controls directly from the server side.

Your button code in the .cs file should look like

protected void searcher(object sender, EventArgs e)
{
   string startDate = txtStartDate.Text;
   string endDate = txtEndDate.Text;

   // the rest of your code
}



You'll probably want to print your output to a label control rather than to the console though since ASP.NET will not print to a console unless you're in debug mode, but then you need to use Debug.WriteLine instead of Console.WriteLine. This will print to the output window in Visual Studio.

And in your Button definition, you do not need to specify the parameters, so it should look like:

<asp:Button Click="Searcher" ID="ButtonID" runat="server" Text="Button" />


This post has been edited by Nakor: 14 April 2011 - 01:11 PM

Was This Post Helpful? 1
  • +
  • -

#4 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Simple ASP.NET question about .cs pages

Posted 15 April 2011 - 09:17 AM

View PostNakor, on 14 April 2011 - 01:04 PM, said:

if this is an asp:Button control then the eventhandler expects two parameters, an object and the EventArgs. You do not need to pass the text from your textboxes as parameters as you can access the textbox controls directly from the server side.

Your button code in the .cs file should look like

protected void searcher(object sender, EventArgs e)
{
   string startDate = txtStartDate.Text;
   string endDate = txtEndDate.Text;

   // the rest of your code
}



You'll probably want to print your output to a label control rather than to the console though since ASP.NET will not print to a console unless you're in debug mode, but then you need to use Debug.WriteLine instead of Console.WriteLine. This will print to the output window in Visual Studio.

And in your Button definition, you do not need to specify the parameters, so it should look like:

<asp:Button Click="Searcher" ID="ButtonID" runat="server" Text="Button" />



Wow, you fixed bits of my code that I didn't even worry about fixing yet! I knew I wouldn't be able to use console to print the results later, but all the examples of how to search text documents used it, and I figured I would fix that after I got the search working in the first place. This really helps, thanks a ton!
Was This Post Helpful? 0
  • +
  • -

#5 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Simple ASP.NET question about .cs pages

Posted 15 April 2011 - 09:29 AM

Actually, now that I've implemented all these changes I'm getting a wierd error... Having replaced "c:/myDir/" with the actual directory, whenever I press the search button, it still gives me an error saying "Could not find a part of the path 'c:\MyDir\'." I've saved and re-saved the pages, and refreshed a few times but it won't try the right directory.
Was This Post Helpful? 0
  • +
  • -

#6 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Simple ASP.NET question about .cs pages

Posted 15 April 2011 - 09:43 AM

As far as i know a website cannot read in parent folders on you local disc this would not even work when you deploy you site , it can only read files in it's own directory or directory folders .. For this to happen i suggest a fileupload control .
Was This Post Helpful? 0
  • +
  • -

#7 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Simple ASP.NET question about .cs pages

Posted 15 April 2011 - 11:22 AM

While the site isn't uploaded to a server though, it should work just using it like this right? Would putting the text files it needs to read into the folder with the application help?

This post has been edited by Erber: 15 April 2011 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#8 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Simple ASP.NET question about .cs pages

Posted 15 April 2011 - 11:38 AM

this is feasible.. only by uploading the files to the MapPath\Directoty of the server,or shipping it with the upload package

and reading it like this

using(StreamReader reader  = new StreamReader(Server.MapPath("PathToFile"))

{
//Do stuff
}

This post has been edited by marinus: 15 April 2011 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#9 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Simple ASP.NET question about .cs pages

Posted 16 April 2011 - 06:26 AM

Did you get this working? IF not can you post the code for what you're still having a problem with along with the exact error message you're getting?
Was This Post Helpful? 0
  • +
  • -

#10 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Simple ASP.NET question about .cs pages

Posted 18 April 2011 - 09:53 AM

I've fixed the problem now actually, but I tried executing the search function, and half an hour later, it's still running. I'm pretty sure that this is because of the fact that it's searching through about 10 GB of text documents. Does anyone know a way to optimize text searches? This is my current code:

protected void searcher(object sender, EventArgs e)
        {
            DateTime start = new DateTime();
            start = Convert.ToDateTime(txtStartDate.Text);
            DateTime end = new DateTime();
            end = Convert.ToDateTime(txtStartDate.Text);

            dateDisplay.Text = "Displaying data between the dates of " + txtStartDate.Text + " and " + txtEndDate.Text;

            string[] filePaths = Directory.GetFiles(@"C:\Inetpub\wwwroot\kitchenerMarket\data", "*.log");
            int i = 0;
            
            while (i <= filePaths.Length)
            {
                // maybe need to upload files?

                string fName = @filePaths[i];
                StreamReader testTxt = new StreamReader(fName);
                string allread = testTxt.ReadToEnd();
                testTxt.Close();
                string regMatch = "abcdefghijklmnopqrstuvwxyz";

                if (Regex.IsMatch(allread, regMatch))
                {
                    Console.WriteLine("found\n");
                }
                else
                {
                    Console.WriteLine("not found\n");
                }
            }
        }

Was This Post Helpful? 0
  • +
  • -

#11 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Simple ASP.NET question about .cs pages

Posted 18 April 2011 - 10:34 AM

, to optimize you're code when you are using the StreamReader class can only be done in your own code that is using the StreamReader . So the answer is no , you can only optimize the current code you are usng to search the file.

you can maybe try using ReadLine();
Was This Post Helpful? 1
  • +
  • -

#12 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Simple ASP.NET question about .cs pages

Posted 18 April 2011 - 11:33 AM

Actually I looked back at my code for a while and realized I had it looping indefinitely, so fixing that improved efficiency... Now that that's fixed however, I get an error, saying the following:

Exception of type 'System.OutOfMemoryException' was thrown.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.

Source Error:

Line 49:                 string fName = @filePaths[i];
Line 50:                 StreamReader testTxt = new StreamReader(fName);
Line 51:                 string allread = testTxt.ReadToEnd();
Line 52:                 testTxt.Close();
Line 53:                 string regMatch = ".ico";



Here is the full code, with some updates:

protected void searcher(object sender, EventArgs e)
        {
            DateTime start = new DateTime();
            start = Convert.ToDateTime(txtStartDate.Text);
            DateTime end = new DateTime();
            end = Convert.ToDateTime(txtStartDate.Text);

            dateDisplay.Text = "Displaying data between the dates of " + txtStartDate.Text + " and " + txtEndDate.Text;

            string[] filePaths = Directory.GetFiles(@"C:\Inetpub\wwwroot\kitchenerMarket\data", "*.log");
            int i = 0;
            
            while (i <= filePaths.Length)
            {
                // maybe need to upload files?
                i++;
                string fName = @filePaths[i];
                StreamReader testTxt = new StreamReader(fName);
                string allread = testTxt.ReadToEnd();
                testTxt.Close();
                string regMatch = ".ico";

                if (Regex.IsMatch(allread, regMatch))
                {
                    results.Text = "found";
                }
                else
                {
                    results.Text = "not found";
                }
            }
        }


EDIT: I'm going to try having it read a single line at a time and see if that fixes it.

This post has been edited by Erber: 18 April 2011 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#13 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Simple ASP.NET question about .cs pages

Posted 19 April 2011 - 10:48 AM

OK, I'm getting a new error in my code saying the following:

Server Error in '/' Application.

Cannot read from a closed TextReader.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ObjectDisposedException: Cannot read from a closed TextReader.

Line 59: //above line gave an error for out of memory, try other things
Line 60:
Line 61: //this is the error fixing code, maybe reading 1 line
Line 62: //at a time will make it not run out of memory
Line 63: do


This is my current code, added a lot of comments to keep track of things:

protected void searcher(object sender, EventArgs e)
        {
            DateTime start = new DateTime();
            start = Convert.ToDateTime(txtStartDate.Text);
            DateTime end = new DateTime();
            end = Convert.ToDateTime(txtStartDate.Text);

            dateDisplay.Text = "Displaying data between the dates of " + txtStartDate.Text + " and " + txtEndDate.Text;

            //from here on is the actual code for searching through the documents

            string[] filePaths = Directory.GetFiles(@"C:\Inetpub\wwwroot\kitchenerMarket\data", "*.log");
            //this is where the data is stored, and it will read .log files, the type of file all the data is

            int i = 0; //counts files   
            //don't think I need anything to count lines, gotta check
            //makes sure it stops reading when it's at the last file
            while (i <= filePaths.Length)
            {
                
                
                string fName = @filePaths[i];
                i++; //counts after declaration of fName, since 0 is a spot in the array
                string lineRead = null;
                StreamReader testTxt = new StreamReader(fName);
                //string allread = testTxt.ReadToEnd();
                //above line gave an error for out of memory(because files are 300+mb?), try other things

                //this is the error fixing code, maybe reading 1 line
                //at a time will make it not run out of memory
                do
                {

                    lineRead = testTxt.ReadLine();
                    //need to figure out how to choose which line, or if it goes up automatically
                    
                    string regMatch = "*.ico";

                    if (Regex.IsMatch(lineRead, regMatch))
                    {
                        //this runs if the word/string is found
                        results.Text = "found";
                        break;
                    }
                    else
                    {
                        //this reads if it isn't
                        results.Text = "not found";
                    }

                } while (lineRead != null);

            testTxt.Close();

            }
        }


Does anyone know how I can fix this?
Was This Post Helpful? 0
  • +
  • -

#14 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Simple ASP.NET question about .cs pages

Posted 19 April 2011 - 10:57 AM

The best way use a streamreader is by following this code. And this is how MSDN uses the StreamReader.

 using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string lineRead;
              
                while ((lineRead = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            } 



This might sort out most of you problems

Greetings

Marinus
Was This Post Helpful? 1
  • +
  • -

#15 Erber  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 08-September 10

Re: Simple ASP.NET question about .cs pages

Posted 19 April 2011 - 11:11 AM

I've implemented this code with no errors before compiling, but when I try and run the code I still get the error I was getting before. I'm almost certain I've augmented the code correctly, but I can't think of any other reason why I would still be getting this error. Here is my new code:

protected void searcher(object sender, EventArgs e)
        {
            DateTime start = new DateTime();
            start = Convert.ToDateTime(txtStartDate.Text);
            DateTime end = new DateTime();
            end = Convert.ToDateTime(txtStartDate.Text);

            dateDisplay.Text = "Displaying data between the dates of " + txtStartDate.Text + " and " + txtEndDate.Text;

            //from here on is the actual code for searching through the documents

            string[] filePaths = Directory.GetFiles(@"C:\Inetpub\wwwroot\kitchenerMarket\data", "*.log");
            //this is where the data is stored, and it will read .log files, the type of file all the data is

            int i = 0; //counts files   
            //don't think I need anything to count lines, gotta check
            //makes sure it stops reading when it's at the last file
            while (i <= filePaths.Length)
            {
                
                
                string fName = @filePaths[i];
                i++; //counts after declaration of fName, since 0 is a spot in the array
                
                using (StreamReader sr = new StreamReader("TestFile.txt") ) 
                {
                    string lineRead;
                    while ((lineRead = sr.ReadLine()) != null) 
                    {
                    //string allread = sr.ReadToEnd();
                    //above line gave an error for out of memory(because files are 300+mb?), try other things
                    //this is the error fixing code, maybe reading 1 line
                    //at a time will make it not run out of memory

                        lineRead = sr.ReadLine();
                        //need to figure out how to choose which line, or if it goes up automatically

                        string regMatch = "*.ico";

                        if (Regex.IsMatch(lineRead, regMatch))
                        {
                            //this runs if the word/string is found
                            results.Text = "found";
                            break;
                        }
                        else
                        {
                            //this reads if it isn't
                            results.Text = "not found";
                        }

                    }
                }   
            }
        }


Also, in case it makes a difference, the files I'm reading are .log files.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2