Solved

variable loses contents

Posted on 2008-06-10
3
207 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
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 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