无法获取日期值

时间:2015-08-27 20:26:42

标签: asp.net vb.net parameters

我正在修改网页。网页使用visual basic,我的数据库是SQL数据库。

该网页为我提供了总的病假,比赛和休假时间。现在,我得到登录计算机的登录名,然后在tblEmployees中查找EmployeeID,这样我就可以通过EmployeeID搜索tblTimeAvailable,然后将不同的时间加起来。

我想从tblEmployees获取StartDate,所以我也可以按周年日期搜索(在我从开始日期开始计算之后)。我收到了错误"类型值'整数'无法转换为'日期'在rvsd = command2.ExecuteNonQuery行上。这是我的代码:

    Dim rve As Object
    Dim rvsd As Date
    Dim dt As Date = Today

    'Get network login name (name only)
    split = windowsLoginName.Split("\".ToCharArray)
    vname = split(1)

    'Get employeeid from table that matches login name 
    Dim Connection As String = "Data Source=WillSQL\ict2;Initial Catalog=timesql_testing;Integrated Security=SSPI"
    Using con As New SqlConnection(Connection)
        Dim sqlemp As String = "SELECT EmployeeID FROM tblEmployees where login = @loginname"
        Dim sqlstdt As String = "SELECT StartDate FROM tblEmployees where login = @loginname"
        Dim command As New SqlCommand(sqlemp, con)
        Dim command2 As New SqlCommand(sqlstdt, con)
        con.Open()
        command.Parameters.Add(New SqlParameter With {.ParameterName = "@loginname", .SqlDbType = SqlDbType.NVarChar, .Value = vname})
        command2.Parameters.Add(New SqlParameter With {.ParameterName = "@loginname", .SqlDbType = SqlDbType.NVarChar, .Value = vname})
        rve = command.ExecuteScalar
        rvsd = command2.ExecuteNonQuery
    End Using

    Dim theDateThisYear As Date

    theDateThisYear = DateSerial(Year(Now), Month(rvsd), Day(rvsd))

    If theDateThisYear < Now() Then
        theDateThisYear = DateAdd("yyyy", 1, theDateThisYear)
    End If

    Dim NextAnniversary As Date = theDateThisYear
    MsgBox(NextAnniversary)

        'Get Sick Time - DOES NOT INCLUDE CHECKING TO MAKE SURE LESS THAN ANNIV DATE YET
    Using con As New SqlConnection(Connection)
        Dim sqls1 As String = "Select SUM(NoofHours) as Total from tblTimeAvailable where workcode = 1 and EmployeeID = @emp"
        Dim command As New SqlCommand(sqls1, con)
        con.Open()
        command.Parameters.Add(New SqlParameter With {.ParameterName = "@emp", .SqlDbType = SqlDbType.NVarChar, .Value = rve})
        rvsa = command.ExecuteScalar
    End Using

提前感谢你们给我的任何帮助!

1 个答案:

答案 0 :(得分:1)

假设数据库中的列[StartDate]具有DateTime类型,那么您需要做的就是

rvsd = CDate(command2.ExecuteScalar)

虽然您可以使用一个查询检索这两个值:

SELECT [EmployeeID], [StartDate] FROM [tblEmployees] where [login] = @loginname"

等等

Dim rve As String
Dim rvsd As Date

Dim connStr As String = "Data Source=WillSQL\ict2;Initial Catalog=timesql_testing;Integrated Security=SSPI"
Dim sql = "SELECT [EmployeeID], [StartDate] FROM [tblEmployees] where [login] = @loginname"

Using conn As New SqlConnection(connStr)
    Using cmd As New SqlCommand(Sql, conn)
        cmd.Parameters.Add(New SqlParameter With {.ParameterName = "@loginname", .SqlDbType = SqlDbType.NVarChar, .Value = vname})
        conn.Open()
        Dim rdr = cmd.ExecuteReader()
        If rdr.HasRows Then
            rdr.Read()
            rve = rdr.GetString(0)
            rvsd = rdr.GetDateTime(1)
        End If
    End Using
End Using