7 Replies - 1121 Views - Last Post: 31 August 2009 - 12:48 PM Rate Topic: -----

#1 rainmark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 21-August 09

SELECT SYNTAX ERROR

Posted 31 August 2009 - 10:04 AM

PLEAAAASEEEEEE HELP ME TO DEBUG THIS ONE I DONT KNOW WHAT IM GOING TO DO WITH IT

IM TRYING TO SEARCH FOR THE DATE BETWEEN THE INPUT IN 2 TEXTBOXES

BUT WHAT IM GETTING IS ERROR MESSAGE SAYING INCORRECT SYNTAX

		'for DOB
		If cboxBrowseBy.SelectedItem = "Date of Birth" Then
			sqlcmd.CommandType = CommandType.Text
			sqlcmd.Connection = sqlcon

			If sqlcmd.Connection.State <> ConnectionState.Open Then
				sqlcmd.Connection.Open()
			End If
			sqlcmd.Connection = sqlcon
			Dim strQuery As String
			strQuery = "Select * From tblStudent where Student_ID BETWEEN"
			strQuery &= "Values ('" & txtDateFrm.Text & "','" & txtDateTo.Text & "')"
			sqlcmd = New SqlCommand(strQuery, sqlcon)
			r = sqlcmd.ExecuteReader()
			dt.Load(r)
			r.Close()
			If sqlcmd.Connection.State <> ConnectionState.Closed Then
				sqlcmd.Connection.Close()
			End If
		End If

 


Is This A Good Question/Topic? 0
  • +

Replies To: SELECT SYNTAX ERROR

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1660
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: SELECT SYNTAX ERROR

Posted 31 August 2009 - 10:07 AM

With a SQL SELECT statement the keyword VALUES is not used, that is your syntax error. Also, please stop posting in all caps, it is considered yelling and most view it as quite rude (and the vast majority of your posts are in all caps)

Select * From tblStudent where Student_ID BETWEEN('" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "')"



That should get rid of the syntax error
Was This Post Helpful? 0
  • +
  • -

#3 rainmark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 21-August 09

Re: SELECT SYNTAX ERROR

Posted 31 August 2009 - 10:20 AM

View PostPsychoCoder, on 31 Aug, 2009 - 09:07 AM, said:

With a SQL SELECT statement the keyword VALUES is not used, that is your syntax error. Also, please stop posting in all caps, it is considered yelling and most view it as quite rude (and the vast majority of your posts are in all caps)

Select * From tblStudent where Student_ID BETWEEN('" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "')"



That should get rid of the syntax error





there's an error saying:

Conversion from string "Select * From tblStudent where S" to type 'Integer' is not valid.

there's an error saying:

Conversion from string "Select * From tblStudent where S" to type 'Integer' is not valid.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1660
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: SELECT SYNTAX ERROR

Posted 31 August 2009 - 10:23 AM

Ok show your updated code so we can see what you're doing
Was This Post Helpful? 0
  • +
  • -

#5 rainmark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 21-August 09

Re: SELECT SYNTAX ERROR

Posted 31 August 2009 - 10:56 AM

Here:

	   'for DOB
		If cboxBrowseBy.SelectedItem = "Date of Birth" Then
			myCommand.CommandType = CommandType.Text
			myCommand.Connection = myConnection

			If myCommand.Connection.State <> ConnectionState.Open Then
				myCommand.Connection.Open()
			End If
			myCommand.Connection = myConnection
			myCommand.CommandType = CommandType.Text
			myCommand.CommandType = "Select * From tblStudent where DOB BETWEEN('" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "')"
			r = myCommand.ExecuteReader()
			dt.Load(r)
			r.Close()
			If myCommand.Connection.State <> ConnectionState.Closed Then
				myCommand.Connection.Close()
			End If
		End If


 


THIS IS THE CORRECT ONE SORRY MY BAD

	Dim myConnection As SqlConnection
	Dim myCommand As New SqlCommand
	Dim ra As String
	Dim ds As New DataSet
	Dim da As New SqlClient.SqlDataAdapter
	Dim dt As New DataTable
	Dim r As SqlDataReader = Nothing



		'for DOB
		If cboxBrowseBy.SelectedItem = "Date of Birth" Then
			myCommand.CommandType = CommandType.Text
			myCommand.Connection = myConnection

			If myCommand.Connection.State <> ConnectionState.Open Then
				myCommand.Connection.Open()
			End If
			myCommand.Connection = myConnection
			myCommand.CommandType = CommandType.Text
			myCommand.CommandType = "Select * From tblStudent where DOB BETWEEN('" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "')"
			r = myCommand.ExecuteReader()
			dt.Load(r)
			r.Close()
			If myCommand.Connection.State <> ConnectionState.Closed Then
				myCommand.Connection.Close()
			End If
		End If
 

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1660
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: SELECT SYNTAX ERROR

Posted 31 August 2009 - 11:41 AM

You are setting the CommandType property 2 times, once to CommandType.Text which is fine but under that you try and set it to your select statement, thus the error. It needs to be

 myCommand.CommandText = "Select * From tblStudent where DOB BETWEEN('" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "')"


Was This Post Helpful? 0
  • +
  • -

#7 rainmark   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 21-August 09

Re: SELECT SYNTAX ERROR

Posted 31 August 2009 - 12:21 PM

 

 myCommand.CommandText = "Select * From tblStudent where DOB BETWEEN('" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "')"






there is an incorrect syntax using AND on this code i dont know why :(

View PostPsychoCoder, on 31 Aug, 2009 - 10:41 AM, said:

You are setting the CommandType property 2 times, once to CommandType.Text which is fine but under that you try and set it to your select statement, thus the error. It needs to be

 myCommand.CommandText = "Select * From tblStudent where DOB BETWEEN('" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "')"






 

 myCommand.CommandText = "Select * From tblStudent where DOB BETWEEN('" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "')"






there is an incorrect syntax using AND on this code i dont know why :(
Was This Post Helpful? 0
  • +
  • -

#8 aks29921   User is offline

  • D.I.C Regular

Reputation: 116
  • View blog
  • Posts: 345
  • Joined: 24-August 09

Re: SELECT SYNTAX ERROR

Posted 31 August 2009 - 12:48 PM

try it without using parentheses
myCommand.CommandText = "Select * From tblStudent where DOB BETWEEN '" & txtDateFrm.Text & "' AND '" & txtDateTo.Text & "'"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1