简体   繁体   English

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

[英]C# for ASP.NET: Errors from added code

I'm supposed to take some code from my professor and add it to the code I have, but I'm experiencing issues with the function I was told to include and I'm not really sure how I can resolve this issue. 我应该从教授那里获取一些代码,并将其添加到我拥有的代码中,但是我被告知要包含的功能遇到问题,我不确定如何解决此问题。 I've read some of the other questions that were posted already, but I can't really be sure those are the same thing. 我已经阅读了一些其他已发布的问题,但是我不能确定这些问题是同一回事。

ERROR RESOLVED: Were brackets missing and mistyped variables & method flying around (provided by answer below!) 解决了错误:括号丢失和变量输入错误,方法飞散(由下面的答案提供!)

Error 1: Expected class, delegate, enum, interface, or struct 错误1:预期的类,委托,枚举,接口或结构

Error 2: Expected class, delegate, enum, interface, or struct 错误2:预期的类,委托,枚举,接口或结构

Error 3: Type or namespace definition, or end-of-file expected 错误3:类型或名称空间定义,或预期的文件结尾

Error 4: clsDataLayer' does not contain a definition for 'SavePersonnel' 错误4:clsDataLayer'不包含'SavePersonnel'的定义

Error 5: clsDataLayer' does not contain a definition for 'GetPersonnel' 错误5:clsDataLayer'不包含'GetPersonnel'的定义

This was supposed to be an add in and move on kind of deal--not sure if it's my code that's the issue or the code supplied. 这本来应该是一种补充并继续进行下去-不确定是我的代码是问题还是所提供的代码。 How do I fix this? 我该如何解决?

Code Provided: Error 1, 2, & 3 提供的代码:错误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;     
}

Code Provided: Error 4 提供的代码:错误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."; 
    }

Code Provided: Error 5 提供的代码:错误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();

Additional Code adding, required for Error 5: 错误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

Error 1, 2 & 3 错误1、2和3

Expected class, delegate, enum, interface, or struct 预期的类,委托,枚举,接口或结构

In C#, methods should always be part of a class. 在C#中,方法应始终是类的一部分。 In your case, you have your method flying around without a parent, so the compiler will complain with this error. 在您的情况下,您的方法在没有父项的情况下四处飞扬,因此编译器将抱怨此错误。

To fix this, define your method inside a class: 要解决此问题,请在类中定义您的方法:

// 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
}

Error 4 & 5 错误4和5

clsDataLayer' does not contain a definition for 'SavePersonnel' clsDataLayer'不包含'SavePersonnel'的定义

This is clearly related to the previous error. 这显然与先前的错误有关。 Since SavePersonnel was wrongly declared, the compiler complains it does not exist. 由于SavePersonnel声明错误,编译器会抱怨它不存在。

Once we solve errors 1, 2 & 3, the errors 4 & 5 should disappear too. 解决错误1、2和3后,错误4和5也应消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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