Solved

variable loses contents

Posted on 2008-06-10
3
212 Views
Last Modified: 2010-04-23
Hi,

I am using Vs 2005 ASP/VB and have the following issue:

I declare a variable 'myqty' and assign a value from LblQty.text to it.

When I click on a button I want to subtract 1 from the value held in the variable but for some reason the value has been removed. I have inserted "*****" next to the relevant code.

I have done a bit of research and think I need to use a global or session variable. Any advice will be much appreciated.


   Protected Sub BtEnter_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtEnter.Click
        Dim myconn As SqlConnection
        Dim mycmd As SqlCommand
        Dim mydr As SqlDataReader
 ***       Dim myqty As Integer  ****

        Dim connectionstring As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString

        myconn = New SqlConnection(connectionstring)
        myconn.Open()
        mycmd = New SqlCommand("SELECT * FROM Tblstock " & "where serialnumber='" & TxtBarcode.Text & "'", myconn)
        mydr = mycmd.ExecuteReader
        While mydr.Read
            LblId.Text = mydr.Item("Id")
            LblCategory.Text = mydr.Item("category")
            LblItem.Text = mydr.Item("items")
            LblQty.Text = mydr.Item("qty")
            LblPurchaseNo.Text = mydr.Item("orderno")
            LblStoreLocation.Text = mydr.Item("location")
            LblDescription.Text = mydr.Item("description")

        End While
        mydr.Close()

  ****      myqty = LblQty.Text   *****



    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   ***     myqty = myqty - 1  ****



    End Sub
0
Comment
Question by:neoice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 21749127
Your variable is cleared throught the postback-event of your page.
http://www.xefteri.com/articles/show.cfm?id=18

You'll need to save your value in a session-variable
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   Session("myqty") = Session("myqty") - 1


myqty = Session("myqty")
0
 
LVL 5

Assisted Solution

by:rstomar
rstomar earned 250 total points
ID: 21749151
or if you don't want it to be saved for the entire session...you can just save it at the page level by using ViewState. Same format as Session but the scope is Page (including postbacks)

E.g.
While mydr.Read
            LblId.Text = mydr.Item("Id")
            LblCategory.Text = mydr.Item("category")
            LblItem.Text = mydr.Item("items")
            LblQty.Text = mydr.Item("qty")
            LblPurchaseNo.Text = mydr.Item("orderno")
            LblStoreLocation.Text = mydr.Item("location")
            LblDescription.Text = mydr.Item("description")

        End While
        mydr.Close()

  ViewState("myqty") = LblQty.Text  

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        myqty = ViewState("myqty") - 1  


0
 

Author Closing Comment

by:neoice
ID: 31465640
Brilliant!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question