Kanchan Sharma Kanchan Sharma - 2 months ago 8
ASP.NET (C#) Question

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

Answer

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.

Comments