473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to declaring store and access "global" variables?

347 100+
I have an application that I'm writing and I'm trying to declare two variables from my sql server, store the values and pass them to my forms.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Public Class Main
  3.     Dim instForm2 As New Exceptions
  4.     Dim oDr As SqlDataReader
  5.     Dim payPeriodStartDate = oDr.GetDateTime(1)
  6.     Dim payPeriodEndDate = payPeriodStartDate.AddDays(7)
  7.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click
  8.         Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _
  9.                  "dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _
  10.                   "from dbo.payroll" & _
  11.                   " where payrollran = 'no'"
  12.         Dim oCmd As System.Data.SqlClient.SqlCommand
  13.         Dim oDr As System.Data.SqlClient.SqlDataReader
  14.  
  15.         oCmd = New System.Data.SqlClient.SqlCommand
  16.         Try
  17.             With oCmd
  18.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
  19.                 .Connection.Open()
  20.                 .CommandType = CommandType.Text
  21.                 .CommandText = ssql
  22.                 oDr = .ExecuteReader()
  23.             End With
  24.             If oDr.Read Then
  25.                 payPeriodStartDate = oDr.GetDateTime(1)
  26.                 payPeriodEndDate = payPeriodStartDate.AddDays(7)
  27.                 Dim ButtonDialogResult As DialogResult
  28.                 ButtonDialogResult = MessageBox.Show("      The Next Payroll Start Date is: " & payPeriodStartDate.ToString() & System.Environment.NewLine & "            Through End Date: " & payPeriodEndDate.ToString())
  29.                 If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
  30.  
  31.                     exceptionsButton.Enabled = True
  32.                     startpayrollButton.Enabled = False
  33.  
  34.                 End If
  35.             End If
  36.             oDr.Close()
  37.             oCmd.Connection.Close()
  38.         Catch ex As Exception
  39.             MessageBox.Show(ex.Message)
  40.             oCmd.Connection.Close()
  41.         End Try
  42.  
  43.     End Sub
  44.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exceptionsButton.Click
  45.         Dim sql As String = "SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime)  as duration INTO scratchpad3" & _
  46.           " FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber" & _
  47.           " where [Exceptions].exceptiondate between @payperiodstartdate and @payperiodenddate" & _
  48.           " GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime," & _
  49.           " [Exceptions].code, [Exceptions].exceptiondate"
  50.         Dim oCmd As System.Data.SqlClient.SqlCommand
  51.         Dim oDr As System.Data.SqlClient.SqlDataReader
  52.         oCmd = New System.Data.SqlClient.SqlCommand
  53.         Try
  54.             With oCmd
  55.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
  56.                 .Connection.Open()
  57.                 .CommandType = CommandType.Text
  58.                 .CommandText = sql
  59.                 .Parameters.AddWithValue("@payperiodstartdate", payPeriodStartDate)
  60.                 .Parameters.AddWithValue("@payperiodenddate", payPeriodEndDate)
  61.                 oDr = .ExecuteReader()
  62.             End With
  63.             oDr.Close()
  64.             oCmd.Connection.Close()
  65.         Catch ex As Exception
  66.             MessageBox.Show(ex.Message)
  67.             oCmd.Connection.Close()
  68.         End Try
  69.         Exceptions.Show()
  70.     End Sub
  71.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  72.         EmployeeEditform.Show()
  73.     End Sub
  74.  
  75.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  76.  
  77.     End Sub
  78. End Class
  79.  
The variables that I need are

Dim payPeriodStartDate = oDr.GetDateTime(1)
Dim payPeriodEndDate = payPeriodStartDate.AddDays(7)

and I think that I have to call these values from my load event but I'm not sure. Can anyone offer any assistance as to the best way to do this?

Thank you

Doug
Jan 17 '11 #1
5 2708
Your Dim at the top of main won’t work. You can’t set the value to a SQL reader value before you open the reader.
Declare them as public and they will be available to other classes within your application.

Expand|Select|Wrap|Line Numbers
  1. Dim payPeriodStartDate = oDr.GetDateTime(1)         S/B public payPeriodStartDate as Date = nothing
  2. Dim payPeriodEndDate = payPeriodStartDate.AddDays(7)   S/B public payPeriodEndDate as Date =nothing
  3.  
Jan 18 '11 #2
dougancil
347 100+
David,

I edited my code as follows:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Public Class Main
  3.     Dim instForm2 As New Exceptions
  4.     Public payrollstartdate As Date = Nothing
  5.     Public payrollenddate As Date = Nothing
  6.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click
  7.         Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _
  8.                  "dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _
  9.                   "from dbo.payroll" & _
  10.                   " where payrollran = 'no'"
  11.         Dim oCmd As System.Data.SqlClient.SqlCommand
  12.         Dim oDr As System.Data.SqlClient.SqlDataReader
  13.         oCmd = New System.Data.SqlClient.SqlCommand
  14.         Try
  15.             With oCmd
  16.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=10.2.1.41;uid=sa;password=chili123")
  17.                 .Connection.Open()
  18.                 .CommandType = CommandType.Text
  19.                 .CommandText = ssql
  20.                 oDr = .ExecuteReader()
  21.             End With
  22.             If oDr.Read Then
  23.                 payPeriodStartDate = oDr.GetDateTime(1)
  24.                 payPeriodEndDate = payPeriodStartDate.AddDays(7)
  25.                 Dim ButtonDialogResult As DialogResult
  26.                 ButtonDialogResult = MessageBox.Show("      The Next Payroll Start Date is: " & payPeriodStartDate.ToString() & System.Environment.NewLine & "            Through End Date: " & payPeriodEndDate.ToString())
  27.                 If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
  28.                     exceptionsButton.Enabled = True
  29.                     startpayrollButton.Enabled = False
  30.                 End If
  31.             End If
  32.             oDr.Close()
  33.             oCmd.Connection.Close()
  34.         Catch ex As Exception
  35.             MessageBox.Show(ex.Message)
  36.             oCmd.Connection.Close()
  37.         End Try
  38.  
  39.     End Sub
  40.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exceptionsButton.Click
  41.         Dim sql As String = "SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime)  as duration INTO scratchpad3" & _
  42.           " FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber" & _
  43.           " where [Exceptions].exceptiondate between @payperiodstartdate and @payperiodenddate" & _
  44.           " GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime," & _
  45.           " [Exceptions].code, [Exceptions].exceptiondate"
  46.         Dim oCmd As System.Data.SqlClient.SqlCommand
  47.         Dim oDr As System.Data.SqlClient.SqlDataReader
  48.         oCmd = New System.Data.SqlClient.SqlCommand
  49.         Try
  50.             With oCmd
  51.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
  52.                 .Connection.Open()
  53.                 .CommandType = CommandType.Text
  54.                 .CommandText = sql
  55.                 .Parameters.AddWithValue("@payperiodstartdate", payPeriodStartDate)
  56.                 .Parameters.AddWithValue("@payperiodenddate", payPeriodEndDate)
  57.                 oDr = .ExecuteReader()
  58.             End With
  59.             oDr.Close()
  60.             oCmd.Connection.Close()
  61.         Catch ex As Exception
  62.             MessageBox.Show(ex.Message)
  63.             oCmd.Connection.Close()
  64.         End Try
  65.         Exceptions.Show()
  66.     End Sub
  67.  
  68.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  69.  
  70.     End Sub
  71. End Class
  72.  
I put a debug on line 27 and looked back at the variables declared .. in the top and see the values as 12:00 AM. They should be (in this case 10/3/2010 and 10/10/2010)
Jan 18 '11 #3
I may have keyed it wrong for you -

Variables as defined

Public payrollstartdate As Date = Nothing
Public payrollenddate As Date = Nothing

Variables you are setting

payPeriodStartDate = oDr.GetDateTime(1)
payPeriodEndDate = payPeriodStartDate.AddDays(7)

These are different variable names

Change the declarations to match the ones you use
Jan 18 '11 #4
dougancil
347 100+
I only have a slight problem with that. I'm not declaring a variable for oDr until the sub starts running.

so here is my code:

Expand|Select|Wrap|Line Numbers
  1. Public Class Main
  2.     Dim instForm2 As New Exceptions
  3.     Public payrollstartdate As Date = oDr.GetDateTime(1)
  4.     Public payrollenddate As Date = payPeriodStartDate.AddDays(7)
  5.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click
  6.         Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _
  7.                  "dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _
  8.                   "from dbo.payroll" & _
  9.                   " where payrollran = 'no'"
  10.         Dim oCmd As System.Data.SqlClient.SqlCommand
  11.         Dim oDr As System.Data.SqlClient.SqlDataReader
  12.         oCmd = New System.Data.SqlClient.SqlCommand
  13.         Try
  14.             With oCmd
  15.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx)
  16.                 .Connection.Open()
  17.                 .CommandType = CommandType.Text
  18.                 .CommandText = ssql
  19.                 oDr = .ExecuteReader()
  20.             End With
  21.             If oDr.Read Then
  22.                 payPeriodStartDate = oDr.GetDateTime(1)
  23.                 payPeriodEndDate = payPeriodStartDate.AddDays(7)
  24.                 Dim ButtonDialogResult As DialogResult
  25.                 ButtonDialogResult = MessageBox.Show("      The Next Payroll Start Date is: " & payPeriodStartDate.ToString() & System.Environment.NewLine & "            Through End Date: " & payPeriodEndDate.ToString())
  26.                 If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
  27.                     exceptionsButton.Enabled = True
  28.                     startpayrollButton.Enabled = False
  29.  
and the problem is that I don't know how to declare the value for oDr in the top.
Jan 18 '11 #5
Sure you are,

Public xxx as date is a declaration (Public means that other forms can see and use it)
Dim xxx as date is a declaration (Dim means that only the procedure that it is in can see it)

The two statements that I gave you before declare the variable for you to use.
" Public payPeriodStartDate As Date = Nothing"
" Public payPeriodEndDate As Date = Nothing"

You declare the SQL Reader for local use with
" Dim oDr As System.Data.SqlClient.SqlDataReader"

You then use the variables by setting their value from something you read with the reader
" payPeriodStartDate = oDr.GetDateTime(1)"

And then set the value based on the first variables value
" payPeriodEndDate = payPeriodStartDate.AddDays(7)"

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient 
  2. Public Class Main 
  3.     Dim instForm2 As New Exceptions 
  4.     Public payPeriodStartDate As Date = Nothing 
  5.     Public payPeriodEndDate As Date = Nothing 
  6.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click 
  7.         Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _ 
  8.                  "dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _ 
  9.                   "from dbo.payroll" & _ 
  10.                   " where payrollran = 'no'" 
  11.         Dim oCmd As System.Data.SqlClient.SqlCommand 
  12.         Dim oDr As System.Data.SqlClient.SqlDataReader 
  13.         oCmd = New System.Data.SqlClient.SqlCommand 
  14.         Try 
  15.             With oCmd 
  16.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=10.2.1.41;uid=sa;password=chili123") 
  17.                 .Connection.Open() 
  18.                 .CommandType = CommandType.Text 
  19.                 .CommandText = ssql 
  20.                 oDr = .ExecuteReader() 
  21.             End With 
  22.             If oDr.Read Then 
  23.                 payPeriodStartDate = oDr.GetDateTime(1) 
  24.                 payPeriodEndDate = payPeriodStartDate.AddDays(7) 
  25.                 Dim ButtonDialogResult As DialogResult 
  26.                 ButtonDialogResult = MessageBox.Show("      The Next Payroll Start Date is: " & payPeriodStartDate.ToString() & System.Environment.NewLine & "            Through End Date: " & payPeriodEndDate.ToString()) 
  27.                 If ButtonDialogResult = Windows.Forms.DialogResult.OK Then 
  28.                     exceptionsButton.Enabled = True 
  29.                     startpayrollButton.Enabled = False 
  30.                 End If 
  31.             End If 
  32.             oDr.Close() 
  33.             oCmd.Connection.Close() 
  34.         Catch ex As Exception 
  35.             MessageBox.Show(ex.Message) 
  36.             oCmd.Connection.Close() 
  37.         End Try 
  38.  
  39.     End Sub 
  40.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exceptionsButton.Click 
  41.         Dim sql As String = "SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime)  as duration INTO scratchpad3" & _ 
  42.           " FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber" & _ 
  43.           " where [Exceptions].exceptiondate between @payperiodstartdate and @payperiodenddate" & _ 
  44.           " GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime," & _ 
  45.           " [Exceptions].code, [Exceptions].exceptiondate" 
  46.         Dim oCmd As System.Data.SqlClient.SqlCommand 
  47.         Dim oDr As System.Data.SqlClient.SqlDataReader 
  48.         oCmd = New System.Data.SqlClient.SqlCommand 
  49.         Try 
  50.             With oCmd 
  51.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx") 
  52.                 .Connection.Open() 
  53.                 .CommandType = CommandType.Text 
  54.                 .CommandText = sql 
  55.                 .Parameters.AddWithValue("@payperiodstartdate", payPeriodStartDate) 
  56.                 .Parameters.AddWithValue("@payperiodenddate", payPeriodEndDate) 
  57.                 oDr = .ExecuteReader() 
  58.             End With 
  59.             oDr.Close() 
  60.             oCmd.Connection.Close() 
  61.         Catch ex As Exception 
  62.             MessageBox.Show(ex.Message) 
  63.             oCmd.Connection.Close() 
  64.         End Try 
  65.         Exceptions.Show() 
  66.     End Sub 
  67.  
  68.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
  69.  
  70.     End Sub 
  71. End Class
  72.  
Jan 18 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
1
by: Chris Stromberger | last post by:
This doesn't seem like it should behave as it does without using "global d" in mod(). d = {} def mod(): d = 3 mod() print d
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
9
by: Javaman59 | last post by:
I saw in a recent post the :: operator used to reach the global namespace, as in global::MyNamespace I hadn't seen this before, so looked it up in MSDN, which explained it nicely. My question...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
5
by: dave | last post by:
If I have a class that hold, for instance, user settings that should be accessible to the entire program logic, what is a good paradigm to use? In C++, I would have made it a global object,...
2
by: Sir Psycho | last post by:
Is there a way to set a global variable without using the Session object? I basically want to be able to track if a visitor is logged in without starting a Session. Is there such a thing as a...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
1
by: sap0321 | last post by:
This should be kindergarten stuff but for some reason I am having trouble with it. I do 99.9% web programming and this is the first windows app i've done in years - probably the first ever in C# ......
4
ChrisWang
by: ChrisWang | last post by:
Hi, I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.