none
global variable in aspx file does not carry over RRS feed

  • Question

  • Hi,
    I am a beginner in asp.net - but I know VB 2005.
    I have an Default2.aspx file with two buttons. upload - import
    The upload button uploads the file from a text box to server (and the file is there)
    I store the location in a global variable StrFilename

    Then on the next step I want to use that variable StrFilename, but it is Null.
    I do not have the right search criteria to find a solution so I hope someone can help me out
    Your help is highly appreciate it (my forehead thanks you)
    I attached part of my code

    Partial

     

    Class Default2

     

    Inherits System.Web.UI.Page

     

    Public StrFileName As String = ""

    ....

     

    Protected Sub Submit1_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick

     

    If Not File1.postedfile Is Nothing And File1.postedfile.contentlength > 0 Then

     

      Dim fn As String = ""

      fn = System.IO.Path.GetFileName(File1.PostedFile.FileName)

      DriveLetter = Mid(Server.MapPath(

    "Report.aspx"), 1, 1)

      StrFileName = DriveLetter &

    ":\AlarmData\" & fn

    ....


     

    Protected Sub cmdImportCSV_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdImportCSV.Click

     

    Dim StrSql As String = ""

     

    Dim strCSVConnStr = ""

     

    Try

     

    Dim strfile As String = StrFileName

     

    ' here strfile = Null
    Wednesday, May 13, 2009 8:27 PM

Answers

  • Hi,

    Well, that recalls me of few initial issues that I faced but in the reverse order, i.e. when I switched from Web Application to Windows Applications. :)

    So, let me tell you few things.
    1. Global variable: are not meant for this purpose that you are doing over here. Global variables means - variables whose variable are constant globally.
    2. Also, there would be a serious problem of using having StrFilename as a Global Variable. All users on all PC(s)/ Web Browser(s) would find this value for StrFilename, once it is updated since it is a Global Variable.

    For purposes like this, where you want to store user specific variables- you have to use Session variables. These are valid for a particular User's Session.


    How to assign & use Session variables in - Microsoft Visual Basic .NET
    'Assign a value to the myvariable session variable.
    Session("myvariable") = "somevalue"
    
    'Retrieve the value of the myvariable session variable.
    If not(Session("myvariable")is nothing) Then
      Dim myString As String = Session("myvariable").ToString()
    End If
    				
    http://support.microsoft.com/default.aspx/kb/307598#2  : Helpful resource. And, now you know- where to explore for more: ASP.NET State Management

    Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
    • Marked as answer by Steffen064 Thursday, May 14, 2009 6:09 PM
    Thursday, May 14, 2009 12:37 AM

All replies

  • Hi,

    Well, that recalls me of few initial issues that I faced but in the reverse order, i.e. when I switched from Web Application to Windows Applications. :)

    So, let me tell you few things.
    1. Global variable: are not meant for this purpose that you are doing over here. Global variables means - variables whose variable are constant globally.
    2. Also, there would be a serious problem of using having StrFilename as a Global Variable. All users on all PC(s)/ Web Browser(s) would find this value for StrFilename, once it is updated since it is a Global Variable.

    For purposes like this, where you want to store user specific variables- you have to use Session variables. These are valid for a particular User's Session.


    How to assign & use Session variables in - Microsoft Visual Basic .NET
    'Assign a value to the myvariable session variable.
    Session("myvariable") = "somevalue"
    
    'Retrieve the value of the myvariable session variable.
    If not(Session("myvariable")is nothing) Then
      Dim myString As String = Session("myvariable").ToString()
    End If
    				
    http://support.microsoft.com/default.aspx/kb/307598#2  : Helpful resource. And, now you know- where to explore for more: ASP.NET State Management

    Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
    • Marked as answer by Steffen064 Thursday, May 14, 2009 6:09 PM
    Thursday, May 14, 2009 12:37 AM
  • Thank you Lakra,
    This answered my question, but brought me to another problem which I will post in a new post
    It is funny as I now switched my web app to a Windows app just to make sure it has nothing to do with asp
    Thursday, May 14, 2009 6:09 PM