Updating date in SQL/VB/Asp.Net

时间:2015-07-31 19:19:00

标签: mysql asp.net sql-server vb.net date

I'm pretty much a novice at all this. I know bits. Just trying to store a date in an SQL database. I've set it to 06/06/2015 temporarily in code below to see if I can get it to update but it updates it as 01/01/0001. When I suss it, The value I actually want to store is todays date plus 6 months. EG: if its 31/07/2015 today, I want it to store 31/01/2016. Can anyone help ? Much appreciated...

ASPX.VB

Protected Sub imgBtnDatechange_Click(sender As Object, e As ImageClickEventArgs) Handles imgBtn.Click
Dim acc As New accounts(Membership.GetUser().ProviderUserKey)
Dim adjustedDate as Date = "06/06/2015"
acc.UpdateVipEndDate(acc.accountID, acc.adjustedDate)
End Sub

ACCOUNTS.VB

Public Property adjustedDate As Date

Public Sub UpdateVipEndDate(ByVal accountID As Guid, ByVal adjustedDate As Date)
    Dim DBConnect As New DBConn
    Using db As DbConnection = DBConnect.Conn("DBConnectionString")
        Dim cmd As SqlCommand = DBConnect.Command(db, "UpdateVipEndDate")
        cmd.Parameters.Add(New SqlParameter("accountID", SqlDbType.UniqueIdentifier,       ParameterDirection.Input)).Value = accountID
        cmd.Parameters.Add(New SqlParameter("newadjustedDate", SqlDbType.Date,     ParameterDirection.Input)).Value = adjustedDate
        db.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        cmd = Nothing
        db.Dispose()
        db.Close()
    End Using
End Sub

STORED PROCEDURE

CREATE PROCEDURE [UpdateVipEndDate]
@accountID          uniqueidentifier,
@newadjustedDate    date
AS
BEGIN

UPDATE tblAccounts SET [vipEndDate] = @newadjustedDate WHERE [accountID] = @accountID

END

1 个答案:

答案 0 :(得分:1)

You set a date here:

Dim adjustedDate as Date = "06/06/2015"

But you never use that variable anywhere. Instead, you're using a parameter on the acc object:

acc.UpdateVipEndDate(acc.accountID, acc.adjustedDate)

So, presumably, the acc.adjustedDate value is otherwise empty or some default MinDate value.

It seems like you're confusing a few things here...

  1. If something is a Date, use it as a Date. Not as a String.
  2. If the UpdateVipEndDate method is on the acc object, why do you need to pass it references to its own parameters? It should be able to access those values internally in the method.

I'm probably getting off point here, though. The simplest thing, it seems, would be to not use a local variable and use the object member that you use elsewhere:

acc.adjustedDate = "06/06/2015"