对象被传递给Function,但没有收到......为什么?

时间:2009-11-12 09:31:42

标签: .net asp.net vb.net oop

我没有从这些代码中得到任何错误,它们每次都是空的。我想知道我是否错误地创建了它们。一如既往的帮助,非常感谢;

Dim l As New Log()
l.Log = "Attempted staff login with username [" & txtUsername.Text & "]"
l.LogId = 0
l.StaffId = 4
l.LogDate = Date.Now()
l.Insert()

。插入()通过这两个函数在我的BLL图层中拾取;

Public Function Insert() As Integer
            Return InsertLog(Me.LogId, Me.Log, Me.StaffId, Me.LogDate)
        End Function

        Public Shared Function InsertLog(ByVal logid As Integer, ByVal log As String, ByVal staffid As Integer, ByVal logdate As DateTime) As Integer
            Using scope As New TransactionScope()
                Dim record As New LogDetails(logid, log, staffid, logdate)
                Dim ret As Integer = SiteProvider.Avalon.InsertLog(record)
                scope.Complete()
                Return ret
            End Using
        End Function

在DAL中,InsertLog是;

 Public Overrides Function InsertLog(ByVal log As LogDetails) As Integer
            Using cn As New SqlConnection(Me.ConnectionString)
                Dim cmd As New SqlCommand("sp_log_Insert", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@log", log.Log)
                cmd.Parameters.AddWithValue("@staff_id", log.StaffId)
                cmd.Parameters.AddWithValue("@log_date", log.LogDate)

                Dim param As New SqlParameter
                param.Direction = ParameterDirection.ReturnValue
                cmd.Parameters.Add(param)

                cn.Open()
                Dim ret As Integer = ExecuteNonQuery(cmd)
                Return CInt(Convert.ToInt32(param.Value))
            End Using
        End Function

我从最终函数得到了正确的返回(db row id) - 但是没有插入正确的数据,我得到了我为LogDetails中的Property设置的默认数据。谁能看到我在这里做错了什么?

非常感谢帮助:)

根据要求: DAL:LogDetails

Imports Microsoft.VisualBasic
Namespace Harmony.Zizz.DAL
    Public Class LogDetails
        Protected _logid As Integer = 0
        Protected _log As String = ""
        Protected _staffid As Integer = 0
        Protected _logdate As DateTime = Date.Now

        Public Sub New()

        End Sub

        Public Sub New(ByVal logid As Integer, ByVal log As String, ByVal staffid As Integer, ByVal logdate As DateTime)

        End Sub

        Public Property LogId() As Integer
            Get
                Return _logid
            End Get
            Set(ByVal value As Integer)
                _logid = value
            End Set
        End Property
        Public Property Log() As String
            Get
                Return _log
            End Get
            Set(ByVal value As String)
                _log = value
            End Set
        End Property
        Public Property StaffId() As Integer
            Get
                Return _staffid
            End Get
            Set(ByVal value As Integer)
                _staffid = value
            End Set
        End Property
        Public Property LogDate() As DateTime
            Get
                Return _logdate
            End Get
            Set(ByVal value As DateTime)
                _logdate = value
            End Set
        End Property
    End Class
End Namespace

2 个答案:

答案 0 :(得分:2)

您需要将私有字段设置为发送到LogDetails的第二个构造函数的值:

Public Sub New(ByVal logid As Integer, ByVal log As String, ByVal staffid As Integer, ByVal logdate As DateTime)
    _logid = logid
    _log = log
    _staffid = staffid
    _logdate = logdate
End Sub

答案 1 :(得分:1)

这是什么?

Public Sub New(ByVal logid As Integer, ByVal log As String, 
            ByVal staffid As Integer, ByVal logdate As DateTime)

End Sub

将其更改为:

Public Sub New(ByVal logid As Integer, ByVal log As String, 
                   ByVal staffid As Integer, ByVal logdate As DateTime)

    _logid = logid
    _staffid = staffid
    _logdate = logdate

End Sub