0

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 Answer 1

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"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply David... Oooh I'm a bit lost now. Once I see the solution I'll know exactly what's going on but I've only been learning this a day or two. I know the Stored Procedure works, It's the VB code I can't get my head round yet. If you or anyone can adjust it for me it will be clearer for me to suss out. I'll bake you a cake.. I'm good at that ;-)
@Eggybread: I'm not sure what else you're looking for. Did you try my suggestion? Instead of creating a new local variable to hold the date (what your code is doing), set the date on the object's variable that you're actually passing to the method.
Sorry, didn't follow you first time but I see what you're saying now. Just tried it and its now saying BC30311: Value of type 'Boolean' cannot be converted to 'Date'.
Ah.. Used ToString and it worked. Thanks so much David.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.