Solved

variable loses contents

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Help with error in Query 2 39
FInd Image Control Gridview 3 37
Data organization issue 7 37
VB.net Filesystem watcher not working 5 38
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…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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