1

Getting this error when I submit my form to savetext.aspx action file:

Compiler Error Message: CS0120: An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Request.get'

On this line:

string path = "/txtfiles/" + Request.Form["file_name"];

Whole code:

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">

class Test 
{
    public static void Main() 
    {
        string path = "/txtfiles/" + Request.Form["file_name"];
        if (!File.Exists(path)) 
        {
            using (StreamWriter sw = File.CreateText(path)) 
            {
                sw.WriteLine(request.form["seatsArray"]);
            sw.WriteLine("");
            }   
        }

        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(s);
            }
        }
    }
}
</script>

How do I fix it?

Thanks!

6
  • 4
    public static void Main() in an ASPX page?! Hmm... Maybe you should explain what you are trying to do as I sense disturbance in the force. Commented Jul 14, 2010 at 14:50
  • Wouldn't you need <%@ Import Namespace="System.Web.UI.Page" %> Commented Jul 14, 2010 at 14:51
  • To take the contents of an array (hidden form object "seatsArray") and put it into a text file named what the user put in "file_name". @MikeTWebb, that just generates another error: CS0138: 'System.Web.UI.Page' is a type not a namespace. Thanks Commented Jul 14, 2010 at 14:54
  • @Darin, @IceDragon....thanks. I was curious... Commented Jul 14, 2010 at 14:55
  • @Darin Dimitrov - Looking at the File.CreateText Method on MSDN (msdn.microsoft.com/en-us/library/system.io.file.createtext.aspx), the example has "public static void Main()"? Thanks Commented Jul 14, 2010 at 14:56

2 Answers 2

5

Remove this Test class as well as the static Main method and replace it with a Page_Load instance method like so:

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e) 
    {
        string path = "/txtfiles/" + Request.Form["file_name"];
        if (!File.Exists(path)) 
        {
            using (StreamWriter sw = File.CreateText(path)) 
            {
                sw.WriteLine(Request.Form["seatsArray"]);
                sw.WriteLine("");
            }   
        }

        using (StreamReader sr = File.OpenText(path)) 
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) 
            {
                Response.Write(s);
            }
        }
    }
</script>

Also you probably want to output to the HttpResponse instead of a console in a web application. Another remark is about your file path: "/txtfiles/", NTFS usually doesn't like such patterns.

Sign up to request clarification or add additional context in comments.

Comments

1

Darin Dimitrov gave you a hint in right direction, but i want just to give answer to question why does this error happen. Normal error should be:

The name 'Request' does not exist in the current context

This happen because for each aspx file a class is created that inherits from Page by default. All new classes defined inside aspx file become nested classes of that one. Request is member of class Page and this particular error happen because you try to access it from static method of nested type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.