VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Declaring global variables and passing them to a sql query Posted by dougancil on 17 Jan 2011 at 3:02 PM
I am building an application that I have to declare 2 variables for every form and pass them to my SQL Stored procedures. I have the following code:

Imports System.Data.SqlClient
Public Class Main
    Dim instForm2 As New Exceptions
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click
        Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _
                 "dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _
                  "from dbo.payroll" & _
                  " where payrollran = 'no'"
        Dim oCmd As System.Data.SqlClient.SqlCommand
        Dim oDr As System.Data.SqlClient.SqlDataReader
        oCmd = New System.Data.SqlClient.SqlCommand
        Try
            With oCmd
                .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
                .Connection.Open()
                .CommandType = CommandType.Text
                .CommandText = ssql
                oDr = .ExecuteReader()
            End With
            If oDr.Read Then
                payPeriodStartDate = oDr.GetDateTime(1)
                payPeriodEndDate = payPeriodStartDate.AddDays(7)
                Dim ButtonDialogResult As DialogResult
                ButtonDialogResult = MessageBox.Show("      The Next Payroll Start Date is: " & payPeriodStartDate.ToString() & System.Environment.NewLine & "            Through End Date: " & payPeriodEndDate.ToString())
                If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
                    exceptionsButton.Enabled = True
                    startpayrollButton.Enabled = False
                End If
            End If
            oDr.Close()
            oCmd.Connection.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            oCmd.Connection.Close()
        End Try

    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exceptionsButton.Click
        Dim sql As String = "SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime)  as duration INTO scratchpad3" & _
          " FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber" & _
          " where [Exceptions].exceptiondate between @payperiodstartdate and @payperiodenddate" & _
          " GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime," & _
          " [Exceptions].code, [Exceptions].exceptiondate"
        Dim oCmd As System.Data.SqlClient.SqlCommand
        Dim oDr As System.Data.SqlClient.SqlDataReader
        oCmd = New System.Data.SqlClient.SqlCommand
        Try
            With oCmd
                .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
                .Connection.Open()
                .CommandType = CommandType.Text
                .CommandText = sql
                .Parameters.AddWithValue("@payperiodstartdate", payPeriodStartDate)
                .Parameters.AddWithValue("@payperiodenddate", payPeriodEndDate)
                oDr = .ExecuteReader()
            End With
            oDr.Close()
            oCmd.Connection.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            oCmd.Connection.Close()
        End Try
        Exceptions.Show()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class


My two variables you can see are coming from this statement in my code:

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


What I need to do is to declare those on the Load event and pass them to my sql stored procedures BUT I need to declare them globally because each form needs to have those values passed to it. Can someone offer some guidance as to the best way to accomplish this?

Thank you

Doug
Report
Re: Declaring global variables and passing them to a sql query Posted by seancampbell on 21 Jan 2011 at 2:32 PM
Hi doug,

There are SQL Server functions that accomplish what you are trying to do, you can slip them directly into your SQL.

Look up:
SQL GetDate()
and
SQL DateAdd(unit, number, date)

For your case
replace @payperiodstartdate with GetDate()
and
replace @payperiodenddate with Dateadd(day,7,GetDate())

Hope that helps.

Otherwise you'd be having to manage changing that DateTime object in VB every time the function is called, and if you are doing that, it seems illogical to declare a global variable (since it technically is not going to have a value that is held statically for other operations to use, rather just memory space that is constantly reserved)

Perhaps I misunderstood your question, please correct me if I am wrong,
Sean Campbell - Firesickle
Report
Re: Declaring global variables and passing them to a sql query Posted by dougancil on 21 Jan 2011 at 2:49 PM
Sean,

What I did for my case was to create a module with the values in it. That seems to work fine for my purposes.
Report
Re: Declaring global variables and passing them to a sql query Posted by seancampbell on 21 Jan 2011 at 2:52 PM
Normally, I would have suggested that. That creates variables at the Application level (a module works like a Static Class if that means anything to you)...

However, I was providing you a SQL Solution so you could get the DateTime of the SQL Server rather than the Date Time of the computer that is executing the code.

Happy Coding sir,
Sean Campbell - Firesickle



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.