简体   繁体   中英

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

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.

No instance is required to call static method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM