Request.Form in WebMethod Error: an object reference is required for non-static field, method

Last Reply 22 days ago By dharmendr

Posted 23 days ago
After Insert the record at index.aspx page redirect to ajax success function and show the message alert("success");
 
I wrote function addProject() to do the task. i didn't get any error. record is added successfully in the database. but alert("success") message is not displayed.
 
i tried the code below
 
error displayed an object reference is required for non-static field, method
 
error was displayed this lines get error
 
string fname = Request.Form["fname"];
 
string age = Request.Form["age"]
 
insert.aspx
public static string doSomething(int id)
{
    string fname = Request.Form["fname"];
    string age = Request.Form["age"];
    string sql = "insert into record values('" + fname + "','" + age + "')";
    SqlCommand cmd = new SqlCommand(sql, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    return "Sucess";
}

Ajex

function addProject() {
    $.ajax({
        type: 'POST',
        url: 'insert.aspx/doSomething',
        dataType: 'JSON',
        data: {fname: $('#fname').val(), age: $('#age').val()},
        success: function (data) {
            alert("success");
        },
        error: function (xhr, status, error) {
            console.log(xhr.responseText);
        }
    });
}

 

You are viewing reply posted by: dharmendr 22 days ago.
Posted 22 days ago Modified on 22 days ago

Hi kobinath,

You can't use a non-static property in a static context. If your method is static it means it shouldn't depend on the instance being called (here you are using Request which is a property of this page).

The correct way is to send your data as parameters to the function.

Check this example. Now please take its reference and correct your code.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function addProject() {
        var id = 1;
        $.ajax({
            type: 'POST',
            url: 'Insert.aspx/doSomething',
            dataType: 'JSON',
            data: "{ id: '" + id + "', fname: '" + $('#fname').val() + "', age: '" + $('#age').val() + "' }",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert("success");
            },
            error: function (xhr, status, error) {
                alert(xhr.responseText);
            }
        });
    }
</script>
<asp:TextBox runat="server" ID="fname" name="fname" />
<asp:TextBox runat="server" ID="age" name="age" />
<asp:Button Text="Save" runat="server" OnClientClick="addProject(); return false;" />

C#

[WebMethod]
public static string doSomething(int id, string fname, int age)
{
    string sql = "insert into record values('" + fname + "','" + age + "')";
    SqlCommand cmd = new SqlCommand(sql, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    return "Sucess";
}