1

我正在尝试使用 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; }
    }
}

在 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); 
    } 
} 

似乎系统无法找到用户名,我只是不明白哪里出了问题。

4

0 回答 0