ASP.NET的C#:添加的代码中出现错误
本文关键字:代码 错误 添加 NET ASP | 更新日期: 2023-09-27 17:58:58
我应该从教授那里获得一些代码,并将其添加到我的代码中,但我在被告知要包含的函数中遇到了问题,我真的不确定如何解决这个问题。我已经阅读了其他一些已经发布的问题,但我真的不能确定这些是同一件事。
已解决错误:括号是否丢失和变量类型错误&飞行方法(由下面的答案提供!)
错误1:应为类、委托、枚举、接口或结构
错误2:应为类、委托、枚举、接口或结构
错误3:类型或命名空间定义,或预期的文件结尾
错误4:clsDataLayer"不包含"SavePersonnel"的定义
错误5:clsDataLayer"不包含"GetPersonnel"的定义
这应该是一个附加组件和移动类型的交易——不确定问题是我的代码还是提供的代码。我该如何解决这个问题?
提供的代码:错误1、2和amp;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,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"不包含"SavePersonnel"的定义
这显然与之前的错误有关。由于SavePersonnel
被错误地声明,编译器会抱怨它不存在。
一旦我们解决了错误1、2&3、错误4&5也应该消失。