Saturday, March 31, 2012

Why is this variable being reset?

Here is my code. I dont understand why the string varialbe "menuCode" is getting reset when the ddlItemCategories_SelectedIndexChanged is called.


Imports System.Data.SqlClient

Public Class menu
Inherits System.Web.UI.Page

Private conn As SqlConnection
Protected WithEvents dlDinnerCategrories As System.Web.UI.WebControls.DataList
Protected WithEvents dgItems As System.Web.UI.WebControls.DataGrid
Private cmd As SqlCommand
Protected menuCode As String
Protected WithEvents lbtnDinner As System.Web.UI.WebControls.LinkButton
Protected WithEvents lbtnLunch As System.Web.UI.WebControls.LinkButton
Protected WithEvents ddlItemCategories As System.Web.UI.WebControls.DropDownList
Protected WithEvents lblDebug As System.Web.UI.WebControls.Label
Private categorycode As String
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub getItemCategories()
Dim commandText As String
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim item As ListItem

If ConnectToDatabase(conn) Then
If menuCode.Equals("DI") Or menuCode.Equals("LU") Then
commandText = "SELECT Code, Category FROM ItemCategories WHERE Menu = '" & menuCode & "' OR Menu = 'DL'"
Else
commandText = "SELECT Code, Category FROM ItemCategories WHERE Menu = '" & menuCode & "'"
End If

cmd = New SqlCommand(commandText, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

While dr.Read()
item = New ListItem(dr.GetValue(1), dr.GetValue(0))
ddlItemCategories.Items.Add(item)
End While

End If
lblDebug.Text = menuCode
End Sub

Function ConnectToDatabase(ByRef conn As SqlConnection) As Boolean
Dim connectionString As String
Dim ok As Boolean = False

Try
connectionString = "Data Source=mssql07;Initial Catalog=DB_120551;User ID=steamerssto;Password=steamers"

conn = New SqlConnection(connectionString)
conn.Open()
'Response.Write("Opened database")
ok = True
Catch ex As Exception
'Response.Write("" & ex.Message)
conn.Close()
conn.Dispose()
End Try

Return ok

End Function

Private Sub ddlDinnerCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub lbtnDinner_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtnDinner.Click
ddlItemCategories.Items.Clear()
menuCode = "DI"
getItemCategories()
End Sub

Private Sub lbtnLunch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtnLunch.Click
ddlItemCategories.Items.Clear()
menuCode = "LU"
getItemCategories()
End Sub

Private Sub ddlItemCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlItemCategories.SelectedIndexChanged
'getItems()
lblDebug.Text = menuCode
End Sub

Private Sub getItems()
Dim commandText As String
Dim cmd As SqlCommand
If ConnectToDatabase(conn) Then
If menuCode.Equals("DI") Or menuCode.Equals("LU") Then
commandText = "SELECT Title, Description, Price FROM Items WHERE Category = '" & ddlItemCategories.SelectedValue & "' AND Menu = '" & menuCode & "' OR Menu = 'DL'"
Else
commandText = "SELECT Title, Description, Price FROM Items WHERE Category = '" & ddlItemCategories.SelectedValue & "' AND Menu = '" & menuCode & "'"
End If

cmd = New SqlCommand(commandText, conn)
lblDebug.Text = commandText
dgItems.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dgItems.DataBind()
End If

End Sub
End Class

Because you are doing a post back but not saving the value of the string between postbacks

MajorCats
hey

looks like menuCode only gets set after clicking 'Lunch' or 'Dinner' buttons so if you try to select something from the dropdownlist before clicking one of the buttons you'll get a null value.
How do I go about fixing this problem? See, they need to choose what menu they want first. Then menuCode is set and used to get the appropriate subcategories for the drop down list.

Thanks
Jarrod
Nevermind I figured it out. Had to learn how to use ViewState.

Thanks for the hints though.

Jarrod

0 comments:

Post a Comment