Date variable doesn't retain its value in select case statement?

Discussion in 'ASP .Net' started by Andy B, Jan 23, 2009.

  1. Andy B

    Andy B Guest

    I have the following code inside the nextButton_Click event of a wizard
    control. The main thing to be watching is the CreationDate variable. There
    are 2 textboxes on step 1 that let the person type in a date and a time.
    When they click next, everything is validated of course. By the time we get
    to the select case statement, everything is valid. What happens is this:

    1. Enter the date and time in the edit boxes on step 1 and click next.
    2. Assuming they are valid dates and times, they show up in the titleText
    property of a custom control in step 2. This works fine so far.
    3. Click next to go to step 3 and the date is printed in the
    AddNewsResultsLabel. For some wierd reson, the CreationDate variable has
    lost its value in step 3. Does anybody know what is going on here?

    I tried just about everything possible and still get the same results. Here
    is the code for the click event below:

    Protected Sub AddNewsWizard_NextButtonClick(ByVal sender As Object, ByVal e
    As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles
    AddNewsWizard.NextButtonClick

    Static CreationDate As Date

    Select Case e.NextStepIndex

    Case 1

    CreationDate = CDate(DateTextBox.Text + " " + TimeTextBox.Text)

    NewsItem.TitleText = "<h2>" + TitleTextBox.Text + "</h2><br />" +
    CreationDate.ToString("f") + "<br />" + DescriptionTextBox.Text

    Dim BodyLabel As Label =
    DirectCast(NewsItem.FindControl("AddBodyPreviewLabel"), Label)

    BodyLabel.Text = BodyTextBox.Text

    Case 2

    AddNewsResultsLabel.Text = CreationDate.ToString("f")

    'Dim NewsHelper As New NewsManager()

    'Dim AddNewsResults As Integer = NewsHelper.AddNewsArticle(CreationDate,
    TitleTextBox.Text, DescriptionTextBox.Text, BodyTextBox.Text,
    IsArchivedCheckBox.Checked)

    'If (AddNewsResults = 1) Then

    ' AddNewsResultsLabel.Text = "The news article has been saved."

    'End If

    'If (AddNewsResults = 0) Then

    ' AddNewsResultsLabel.Text = "The news article wasn't saved."

    'End If

    End Select

    End Sub
    Andy B, Jan 23, 2009
    #1
    1. Advertising

  2. Andy B

    Andy B Guest

    [solved] Date variable doesn't retain its value in select case statement?

    I was able to fix the problem. It looked like an oversight in my end. Here
    is the new code to make it work:

    Protected Sub AddNewsWizard_NextButtonClick(ByVal sender As Object, ByVal e
    As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles
    AddNewsWizard.NextButtonClick

    '*** Create the CreationDate local to the entire event so it can be seen and
    used anywhere in the event.
    dim CreationDate as Date = CDate(DateTextBox.Text + " " + TimeTextBox.Text)

    Select Case e.NextStepIndex

    Case 1
    '*** Don't use it like this here unless you only need the case 1 block to
    see it.
    'CreationDate = CDate(DateTextBox.Text + " " + TimeTextBox.Text)

    NewsItem.TitleText = "<h2>" + TitleTextBox.Text + "</h2><br />" +
    CreationDate.ToString("f") + "<br />" + DescriptionTextBox.Text

    Dim BodyLabel As Label =
    DirectCast(NewsItem.FindControl("AddBodyPreviewLabel"), Label)

    BodyLabel.Text = BodyTextBox.Text

    Case 2

    AddNewsResultsLabel.Text = CreationDate.ToString("f")

    'Dim NewsHelper As New NewsManager()

    'Dim AddNewsResults As Integer = NewsHelper.AddNewsArticle(CreationDate,
    TitleTextBox.Text, DescriptionTextBox.Text, BodyTextBox.Text,
    IsArchivedCheckBox.Checked)

    'If (AddNewsResults = 1) Then

    ' AddNewsResultsLabel.Text = "The news article has been saved."

    'End If

    'If (AddNewsResults = 0) Then

    ' AddNewsResultsLabel.Text = "The news article wasn't saved."

    'End If

    End Select

    End Sub
    Andy B, Jan 24, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    2
    Views:
    7,765
    John Blair
    Aug 2, 2005
  2. Harinath
    Replies:
    1
    Views:
    516
    Patrice
    Dec 22, 2006
  3. Replies:
    5
    Views:
    299
  4. TS

    why does my control retain its value after postback

    TS, Aug 2, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    158
    Steven Cheng[MSFT]
    Aug 3, 2005
  5. Replies:
    3
    Views:
    95
Loading...

Share This Page