?
Solved

variable loses contents

Posted on 2008-06-10
3
Medium Priority
?
216 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 1000 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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