Kanchan Sharma
Kanchan Sharma

Reputation: 51

how to call a function defined in a class file on aspx.cs page

This is the code written on class file Delete.cs and I want to access this code on WebForm1.aspx under a [WebMethod]

  namespace Bootstrap
    {
 public class DeleteData
{
    public static string DeleteData(int rollno)
    {
        string msg = string.Empty;
        string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("spDeleteData", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            cmd.Parameters.AddWithValue("@RollNo", rollno);
            int i = cmd.ExecuteNonQuery();
            con.Close();
            if (i == 1)
            {
                msg = "true";
            }
            else
            {
                msg = "false";
            }
        }

        return msg;
    }
}

}

On my webform1.aspx I have called it like this

 [WebMethod]
    DeleteData delete = new DeleteData();
    delete.DeleteData(int rollno);

Both of them are using Same Namespace and my Delete.cs is in App_Code folder, but it is giving compile time errors. Please Help

Upvotes: 0

Views: 1551

Answers (2)

A_D
A_D

Reputation: 189

No instance is required to call static method.

[WebMethod]
    public returntype MethodName()
    {
     //lines of code
     int rollno=YourValue;    
     DeleteData.DeleteData(rollno);
    }

Upvotes: 0

nvoigt
nvoigt

Reputation: 77285

Wow, you managed to get about every single thing wrong that can be wrong. Did you actually write that DeleteData method? It seems like you forgot everything you already knew.

You need a method, not just code. In that method you need to call your method of the class you wrote. And it's static, so you don't need an instance of it.

[WebMethod]
public string DeleteData(int rollno)
{
    return DeleteData.DeleteData(rollno);
}

You should work on naming though, this is confusing.

Upvotes: 1

Related Questions