我正在尝试使用Web服务将数据插入数据库,我遇到了以下错误:
'Remarks' does not contain a definition for 'UserName' and no extension method 'UserName' accepting a first argument of type 'Remarks' could be found (are you missing a using directive or an assembly reference?)
在IService.cs文件中,我有以下内容:
[ServiceContract]
public interface IService
{
[OperationContract]
int UpdateRemark(Remarks updateRemark);
}
[DataContract]
public class Remarks
{
string _content = string.Empty;
int _poNum;
string _UserName = string.Empty;
[DataMember]
public string remarkContent
{
get { return _content; }
set { _content = value; }
}
[DataMember]
public int poNum
{
get { return _poNum; }
set { _poNum = value; }
}
[DataMember]
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
}
<.c>在service.cs文件中:
public class service : Iservice
{
private String errMsg;
private DlDbConn dbConn;
public int UpdateRemark(Remarks updateRemark)
{
SqlConnection conn;
StringBuilder sql;
SqlCommand sqlCmd;
int result;
result = 0;
dbConn = new DlDbConn();
conn = dbConn.GetConnection();
sql = new StringBuilder();
sql.AppendLine("INSERT INTO Remarks(poNum, remarkContent, UserName, date)");
sql.AppendLine("VALUES (@poNum, @remarkContent, @UserName, getdate())");
try
{
conn.Open();
sqlCmd = new SqlCommand(sql.ToString(), conn);
sqlCmd.Parameters.AddWithValue("@poNum", updateRemark.poNum);
sqlCmd.Parameters.AddWithValue("@remarkContent", updateRemark.remarkContent);
sqlCmd.Parameters.AddWithValue("@UserName", updateRemark.UserName);
result = sqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
errMsg = ex.Message;
}
finally
{
conn.Close();
}
return result;
}
}
并在aspx文件中,(使用onClick事件按钮调用该方法):
protected void SendBtn_Click(object sender, EventArgs e)
{
foreach (ListViewDataItem item in ListView1.Items)
{
// accessing Control
Label poNum = (Label)item.FindControl("poLabel");
TextBox remark = (TextBox)item.FindControl("BodyLabel");
String currentUserId = HttpContext.Current.User.Identity.Name;
Remarks updateRemark = new Remarks();
updateRemark.poNum = Int32.Parse(poNum.Text); // the error occur here
updateRemark.remarkContent = remark.Text; // the error occur here
updateRemark.UserName = currentUserId.ToString(); // the error occur here
IServiceClient client = new IServiceClient();
client.UpdateRemark(updateRemark);
}
}
似乎系统无法找到wad是UserName,我只是不明白它出错的地方。