2016-09-06 39 views
0

這是寫在類文件Delete.cs的代碼,我想下[的WebMethod]如何調用在類文件中定義的aspx.cs頁面上的功能

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; 
    } 
} 
來訪問WebForm1.aspx的驗證碼

}

在我WebForm1.aspx的我把它稱爲這樣

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

他們都使用相同的命名空間,我的Delete.cs是在App_Code文件夾中,但它給編譯時錯誤。 請幫忙

+0

對不起,類文件名是DeleteData.cs –

回答

1

哇,你設法得到每一個錯誤的東西都可能是錯的。你真的寫了DeleteData方法嗎?看起來你忘了你已經知道的一切。

您需要一種方法,而不僅僅是代碼。在這種方法中,你需要調用你寫的類的方法。它是靜態的,所以你不需要它的一個實例。

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

你應該努力命名,雖然這是令人困惑的。

+0

ohh是的抱歉,我不需要該實例。謝謝 –

0

沒有實例需要調用靜態方法。

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