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

时间:2016-07-26 17:55:02

标签: c# asp.net debugging

我应该从我的教授那里拿一些代码并将其添加到我的代码中,但是我遇到了我被告知要包含的功能的问题而且我不太确定我该如何解决这个问题。我已经阅读了已经发布的其他一些问题,但我无法确定这些问题是一样的。

  

错误已解决:括号丢失且输错错误&飞来飞去的方法(由下面的答案提供!)

     

错误1:预期的类,委托,枚举,接口或结构

     

错误2:预期的类,委托,枚举,接口或结构

     

错误3:类型或命名空间定义,或预期的文件结尾

     

错误4:clsDataLayer'不包含' SavePersonnel'的定义

     

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

这应该是一个加入并进行交易 - 不确定我的代码是否是问题或提供的代码。我该如何解决?

代码提供:错误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;     
}

代码提供:错误4

// ERROR AFTER clsDataLayer.
    if (clsDataLayer.SavePersonnel(Server.MapPath("PayrollSystem_DB.accdb"), 
    Session["txtFirstName"].ToString(), 
    Session ["txtLastName"].ToString(),
    Session ["txtPayRate"].ToString(),
    Session ["txtStartDate"].ToString(),
    Session ["txtEndDate"].ToString())) 
    {
    txtVerifiedInfo.Text = txtVerifiedInfo.Text + 
    "\nThe information was successfully saved!";
    }
    else
    {
    txtVerifiedInfo.Text = txtVerifiedInfo.Text +
    "\nThe information was NOT saved."; 
    }

提供的代码:错误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 个答案:

答案 0 :(得分:2)

错误1,2和&amp; 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&amp; 5

  

clsDataLayer&#39;不包含&#39; SavePersonnel&#39;

的定义

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

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