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

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

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
 
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
 
Back
Top