C#for ASP.NET:添加的代码中的错误

C#for ASP.NET:添加的代码中的错误,c#,asp.net,debugging,C#,Asp.net,Debugging,我应该从我的教授那里获取一些代码并将其添加到我的代码中,但是我遇到了与我被告知要包含的函数有关的问题,我不确定如何解决这个问题。我已经阅读了一些已经发布的其他问题,但我不能确定这些是同一件事 错误解决:括号是否缺失,变量和方法是否输入错误(由下面的答案提供!) 错误1:应为类、委托、枚举、接口或结构 错误2:应为类、委托、枚举、接口或结构 错误3:需要类型或命名空间定义,或文件结尾 错误4:clsDataLayer'不包含“savePersonal”的定义 错误5:clsDataLayer'不包

我应该从我的教授那里获取一些代码并将其添加到我的代码中,但是我遇到了与我被告知要包含的函数有关的问题,我不确定如何解决这个问题。我已经阅读了一些已经发布的其他问题,但我不能确定这些是同一件事

错误解决:括号是否缺失,变量和方法是否输入错误(由下面的答案提供!)

错误1:应为类、委托、枚举、接口或结构

错误2:应为类、委托、枚举、接口或结构

错误3:需要类型或命名空间定义,或文件结尾

错误4:clsDataLayer'不包含“savePersonal”的定义

错误5:clsDataLayer'不包含'GetPersonal'的定义

这本应该是一个插件,然后继续进行类似的交易——不确定问题是我的代码还是提供的代码。我该如何解决这个问题

提供的代码:错误1、2和3

// ERROR OCCURS at bool
public static bool SavePersonnel(string Database, string FirstName, string LastName,
string PayRate, string StartDate, string EndDate)
{
bool recordSaved;
try {
// ERROR 2 OCCURS HERE after new     !!!!!
OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + Database);
conn.Open();
OleDbCommand command = conn.CreateCommand();
string strSQL;
// Add your comments here
strSQL = "Insert into tblPersonnel " +
"(FirstName, LastName, PayRate, StartDate, EndDate) values ('" +
FirstName + "', '" + LastName + "', " + PayRate + ", '" + StartDate + 
"', '" + EndDate + "')";
// Add your comments here
command.CommandType = CommandType.Text;
command.CommandText = strSQL;
// Add your comments here
command.ExecuteNonQuery();
// Add your comments here
conn.Close();
recordSaved = true;
} //<-- ERROR 3 is at this curly bracket
catch (Exception ex) {
recordSaved = false;
}
return recordSaved;     
}
   
提供的代码:错误5

        if (!Page.IsPostBack)
        {
//Declare the Dataset
            dsPersonnel myDataSet = new dsPersonnel();
//ERROR AFTER clsDataLayer.
            myDataSet = clsDataLayer.GetPersonnel(Server.MapPath("PayrollSystem_DB.accdb"));
//Set the DataGrid to the DataSource based on the table
            grdViewPersonnel.DataSource = myDataSet.Tables["tblPersonnel"];
//Bind the DataGrid
            grdViewPersonnel.DataBind();
附加代码添加,错误5需要:

// This function retrieves all data from tblPersonnel table
public static dsPersonnel GetPersonnel (string Database, string strSearch)
{
    dsPersonnel DS;
    OleDbConnection SqlConn;
    OleDbAdapter sqlDA;
    
//Opens OleDbConnection
    sqlConn = new OleDBConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=" + Database);
    
//Employee Search (procured from video, add in later?
    if (strSearch == null || strSearch == "")
    {
    sqlDA = new OleDbDataAdapter("Select * from tblPersonnel", sqlConn);
    }
    else
    {
    sqlDA = new OleDbAdapter("Select '' from tblPersonnel where LastName = '" + strSearch + "'", sqlConn);
    }

//Sets Value of DS
    DS = new dsPersonnel();

//Fills Table with Data
    sqlDA_Fill(DS.tblPersonnel);

//Return value
    return DS;
}//End Function: Public static dsPersonnel GetPersonnel
错误1、2和3 应为类、委托、枚举、接口或结构

在C#中,方法应该始终是类的一部分。 在您的例子中,您的方法在没有父对象的情况下四处游荡,因此编译器会抱怨此错误

要解决此问题,请在类中定义方法:

// C# class
public class clsDataLayer
{
    // This functions insert data into tblPersonnel table
    public static bool SavePersonnel(string Database, string FirstName, string LastName, string PayRate, string StartDate, string EndDate)
    {
        bool recordSaved;
        try
        {
           OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Database);
           conn.Open();
           OleDbCommand command = conn.CreateCommand();
           string strSQL;

           // Add your comments here
           strSQL = "Insert into tblPersonnel " +
           "(FirstName, LastName, PayRate, StartDate, EndDate) values ('" + FirstName + "', '" + LastName + "', " + PayRate + ", '" + StartDate +  "', '" + EndDate + "')";

           // Add your comments here
           command.CommandType = CommandType.Text;
           command.CommandText = strSQL;

           // Add your comments here
           command.ExecuteNonQuery();

           // Add your comments here
           conn.Close();
           recordSaved = true;
        }
        catch (Exception ex)
        {
            recordSaved = false;
        }
        return recordSaved;     
    }

    // This function retrieves all data from tblPersonnel table
    public static dsPersonnel GetPersonnel (string Database, string strSearch)
    {
        dsPersonnel DS;
        OleDbConnection SqlConn;
        OleDbAdapter sqlDA;

        //Opens OleDbConnection
        sqlConn = new OleDBConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Database);

        //Employee Search (procured from video, add in later?
        if (strSearch == null || strSearch == "")
        {
            sqlDA = new OleDbDataAdapter("Select * from tblPersonnel", sqlConn);
        }
        else
        {
             sqlDA = new OleDbAdapter("Select '' from tblPersonnel where LastName = '" + strSearch + "'", sqlConn);
        }

        //Sets Value of DS
        DS = new dsPersonnel();

        //Fills Table with Data
        sqlDA_Fill(DS.tblPersonnel);

        //Return value
         return DS;
     }
     //End Function: Public static dsPersonnel GetPersonnel
}
错误4和5 clsDataLayer'不包含“savePersonal”的定义

这显然与前面的错误有关。 由于
savepersonal
被错误声明,编译器抱怨它不存在

一旦我们解决了错误1、2和3,错误4和5也应该消失。

错误1、2和3 应为类、委托、枚举、接口或结构

在C#中,方法应该始终是类的一部分。 在您的例子中,您的方法在没有父对象的情况下四处游荡,因此编译器会抱怨此错误

要解决此问题,请在类中定义方法:

// C# class
public class clsDataLayer
{
    // This functions insert data into tblPersonnel table
    public static bool SavePersonnel(string Database, string FirstName, string LastName, string PayRate, string StartDate, string EndDate)
    {
        bool recordSaved;
        try
        {
           OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Database);
           conn.Open();
           OleDbCommand command = conn.CreateCommand();
           string strSQL;

           // Add your comments here
           strSQL = "Insert into tblPersonnel " +
           "(FirstName, LastName, PayRate, StartDate, EndDate) values ('" + FirstName + "', '" + LastName + "', " + PayRate + ", '" + StartDate +  "', '" + EndDate + "')";

           // Add your comments here
           command.CommandType = CommandType.Text;
           command.CommandText = strSQL;

           // Add your comments here
           command.ExecuteNonQuery();

           // Add your comments here
           conn.Close();
           recordSaved = true;
        }
        catch (Exception ex)
        {
            recordSaved = false;
        }
        return recordSaved;     
    }

    // This function retrieves all data from tblPersonnel table
    public static dsPersonnel GetPersonnel (string Database, string strSearch)
    {
        dsPersonnel DS;
        OleDbConnection SqlConn;
        OleDbAdapter sqlDA;

        //Opens OleDbConnection
        sqlConn = new OleDBConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Database);

        //Employee Search (procured from video, add in later?
        if (strSearch == null || strSearch == "")
        {
            sqlDA = new OleDbDataAdapter("Select * from tblPersonnel", sqlConn);
        }
        else
        {
             sqlDA = new OleDbAdapter("Select '' from tblPersonnel where LastName = '" + strSearch + "'", sqlConn);
        }

        //Sets Value of DS
        DS = new dsPersonnel();

        //Fills Table with Data
        sqlDA_Fill(DS.tblPersonnel);

        //Return value
         return DS;
     }
     //End Function: Public static dsPersonnel GetPersonnel
}
错误4和5 clsDataLayer'不包含“savePersonal”的定义

这显然与前面的错误有关。 由于
savepersonal
被错误声明,编译器抱怨它不存在


一旦我们解决了错误1、2和3,错误4和5也应该消失。

听起来像是遗漏了}@LoekD是的,你是对的。我未能将文件的公共类的末尾移到末尾,尽管这会导致很多错误。如果我听不懂,我会尽快回复。@LoekD谢谢你的帮助,这对我非常有益。:)听起来像是失踪的}@LoekD是的,你是对的。我未能将文件的公共类的末尾移到末尾,尽管这会导致很多错误。如果我听不懂,我会尽快回复。@LoekD谢谢你的帮助,这对我非常有益。:)非常感谢,非常有帮助。现在一切都正常了——除了我忘记的代码之外,我还需要从我添加的其他行中添加代码。:)我感谢你的帮助。非常感谢,非常有帮助。现在一切都正常了——除了我忘记的代码之外,我还需要从我添加的其他行中添加代码。:)我感谢你的帮助。