473,378 Members | 1,468 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,378 software developers and data experts.

SQL Variable Declaration error

347 100+
I have the following form:
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Public Class Main
  3.     Protected WithEvents DataGridView1 As DataGridView
  4.     Dim instForm2 As New Exceptions
  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.  
  14.         Try
  15.             With oCmd
  16.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
  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 adapter As System.Data.SqlClient.SqlDataAdapter
  42.         Dim connection As System.Data.SqlClient.SqlConnection
  43.         Dim connectionString As String = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx"
  44.         Dim ds As New DataSet
  45.         connection = New SqlConnection(connectionString)
  46.         Try
  47.             connection.Open()
  48.             adapter = New SqlDataAdapter("SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime)  as duration INTO scratchpad3" & _
  49.       " FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber" & _
  50.       " where [Exceptions].exceptiondate between @payperiodstartdate and @payperiodenddate" & _
  51.       " GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime," & _
  52.       " [Exceptions].code, [Exceptions].exceptiondate", connection)
  53.             If (ds Is Nothing) Then
  54.                 'it's empty
  55.                 MsgBox("There was no data for this time period, press Ok to continue", "No Data")
  56.                 connection.Close()
  57.                 Exceptions.Hide()
  58.             Else 
  59.                 adapter.Fill(ds)
  60.                 connection.Close()
  61.             End If
  62.  
  63.         Catch ex As Exception
  64.             MessageBox.Show(ex.ToString)
  65.             connection.Close()
  66.         End Try
  67.         Exceptions.Show()
  68.     End Sub
  69.     Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
  70.  
  71.     End Sub
  72.  
  73.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  74.  
  75.     End Sub
  76. End Class
  77.  
and when I run my app, I get a SQL error that tells me that I need to declare a value for '@payperiodstartdate' but I know that that value is declared because the first query runs fine. I am told that the line where the error is is line 59, which is this line:

Expand|Select|Wrap|Line Numbers
  1.   adapter.Fill(ds)
  2.  
Can someone please assist me with this error?

Thank you
Jan 19 '11 #1
0 877

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
12
by: Michael B Allen | last post by:
Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) {...
7
by: seamoon | last post by:
Hi, I'm doing a simple compiler with C as a target language. My language uses the possibility to declare variables anywhere in a block with scope to the end of the block. As I remembered it this...
21
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value...
9
by: Denis Petronenko | last post by:
i can write something like this int foo() { return 100; } if( int x=foo() ) { .... }else{
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
15
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here...
1
by: Raman | last post by:
Hi All, I have two libs (libFirst.a and libSecond.a). Both libs contains a common function func(). Now I want to link an application "app" with these two libs as gcc -o app libFirst.a...
1
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.