Solved

variable loses contents

Posted on 2008-06-10
3
201 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
3 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Brilliant!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now