473,811 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Incorrect Syntax issue with SQL Query

347 Contributor
I have the following code:

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

"Line1: Incorrect syntax near 'Employees'"

Which is part of my second SQL query. I can debug up to that point without my application breaking. I have gone back to my SQL management studio and ran (and re-ran) my sql query and it works fine. Can someone please help me as to why when I run a working query in VB, it throws this error?

Thank you

Doug
Jan 14 '11 #1
6 3506
yarbrough40
320 Contributor
take a look at your INNER JOIN. you have
Expand|Select|Wrap|Line Numbers
  1. INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber  
you may want to correct this. But I think your issue is what Rabbit is saying...
Jan 14 '11 #2
Rabbit
12,516 Recognized Expert Moderator MVP
Expand|Select|Wrap|Line Numbers
  1. strVar = "Hello" & _
  2.      "World"
The results is HelloWorld.
Jan 14 '11 #3
dougancil
347 Contributor
Rabbit,

So then how I have my query formatted is incorrect?
Jan 14 '11 #4
yarbrough40
320 Contributor
is this any clearer?
Expand|Select|Wrap|Line Numbers
  1.  strVar = "Hi" & _ 
  2.      "my" & _ 
  3.      "name" & _ 
  4.      "is" & _ 
  5.      "slim" & _ 
  6.      "shady" & _ 


The results is Himynameisslims hady
Jan 14 '11 #5
Rabbit
12,516 Recognized Expert Moderator MVP
Yes, put a space at the end or beginning of the lines.
Expand|Select|Wrap|Line Numbers
  1. strVar = "Hello" & _ 
  2.      " World"
Result is "Hello World"
Jan 14 '11 #6
dougancil
347 Contributor
Thanks,

that's what it was.
Jan 14 '11 #7

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

Similar topics

11
7114
by: Mark Findlay | last post by:
Hello Experts! I am attempting to use the OleDbCommand.ExecuteScaler() function within my ASP.NET C# web page to perform a simple validation, but receive the following error: "Incorrect syntax near the keyword 'DEFAULT'" The form has 2 fields on it, called tb_username and tb_password. (see code snippet below).
6
2993
by: martin1 | last post by:
I just use DataSet to bind DataSetGrid and display from SQL DB. when starting run in Visual Studio 2005, get "Line 1: Incorrect syntax near '1'" error message from below fill line, objDataAdapter.Fill(objDataSet, "mindata") any help is greatly appriciated.
0
1896
by: netone | last post by:
I am developing site using dot NET.... when I use this sql query Dim mydata As String = "SELECT director.applno, district.distName, disabappl.recvddate_sdo, disabappissue.IDCardNo, director.dreceived_director" _ & " FROM director INNER JOIN disabappl ON director.applno = disabappl.applno INNER JOIN " _ & " district ON disabappl.submitted_dist = district.dist_cd INNER JOIN" _ &...
1
2358
by: iporter | last post by:
In the following code, the two Response.Write statements output exactly the same - I can copy and paste both into Query Analyzer, and run them fine. However, if I comment out line 3, the assignment of "SELECT T..." to the variable query, the last line produces the error: Line 1: Incorrect syntax near '<'. Many thanks in advance for any explanation - the problem is tearing my hair out!! Iain
3
28937
by: wallic | last post by:
Hello, This is my first post and I am a beginner with SQL code. The code below is supposed to update a new table (loctable) with a calculated value based on the original table (hra_data). There are 3 possibilities in the hra_data table that will cause the loctable to be updated with the calculated value. After playing with it, I thought it was in the correct format but I keep getting an "Incorrect syntax..." error. Any guidence would be...
0
8335
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new records there is an error "Incorrect syntax near '-'. Must declare the scalar variable "@UserName". I worked out in design view,code is automatically generated.Iam not able fix the error. Iam working with Visual Web Developer-2005 Express Edition
5
5493
by: Chris Kennedy | last post by:
Hi all I'm running SQL Server 2005 Express Edition. One database One table called Sites Fields as follows: id (bigint, Identity, Primary Key) SiteName (varchar(50), allows nulls) Generation (float) LastUpdated (varchar(50), allows nulls)
2
4846
by: mahendra dubey | last post by:
I have SQl Server 2005 SP1.I am running following query ALTER USER WITH LOGIN = I have created both user and login with name myuser. but this query returns error Msg 102, Level 15, State 1, Server ..., Line 1 Incorrect syntax near 'LOGIN'. Same query can be run on SQL server 2005 SP3,SQL Server 2008...
5
5484
NeoPa
by: NeoPa | last post by:
As the title implies, I am getting the following error message with some SQL I'm trying to use. I'm using MS Management Studio 2008 with a link to a 2005 server. What confuses me is that when I change the first line from a DELETE to a SELECT, the SQL works exactly as I would expect it to. DELETE FROM .. AS INNER JOIN .. AS ON SUBSTRING(.,2,3)=SUBSTRING(.,2,3) AND .=. WHERE SUBSTRING(.,1,1)='1' GO
13
13894
by: santhanalakshmi | last post by:
Hi, I am working on SQL 2008 database. In Micosoft SQL server Management studio, i am trying to insert multiple records at a time in a table, using this query insert into test1 values(6,'san6',1),( 7,'san7',1); Error: Msg 170, Level 15, State 1, Line 1 Line 1: Incorrect syntax near ','.
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10384
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9204
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6887
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4338
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.