neomage23
asked on
Syntax error converting character string to smalldatetime data type
Hello experts@!@
I have this large datagrid...which displays about 1000 items from a SQL DB...as of right now, when the visitor comes to the page the whole sql table is produced before them.
What I would like to do is make a UI that will allow the user to display portions of the SQL-table based on different criteria.
Right now all I'm concerned with is getting it so the user can put a date range into two texboxes, but i'm having trouble getting around the error:
Syntax error converting character string to smalldatetime data type
Why is the code below generating that error? What am I doing wrong? -thanks -neo
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim highdate As DateTime
Dim lowdate As DateTime
lowdate = Format(Convert.ToDateTime( Me.txtDate 1.Text), "MM/dd/yyyy")
highdate = Format(Convert.ToDateTime( Me.txtDate 2.Text), "MM/dd/yyyy")
Const strConnString As String = "connection string omitted"
Dim objConn As New SqlConnection(strConnStrin g)
Dim strSQL As String
strSQL = "select * from webdocuments where (((datefiled) > 'lowdate') and ((datefiled) < 'highdate'))"
Dim mycommand As New SqlCommand(strSQL, objConn)
objConn.Open()
DataGrid1.DataSource = mycommand.ExecuteReader(Co mmandBehav ior.CloseC onnection)
DataGrid1.DataBind()
Catch ex As Exception
Dim strScript As String
strScript = "<script language=javascript>"
strScript &= "alert('something went wrong');"
strScript &= "</script>"
If (Not Page.IsStartupScriptRegist ered("clie ntScript") ) Then
Page.RegisterStartupScript ("clientSc ript", strScript)
End If
End Try
End Sub
I have this large datagrid...which displays about 1000 items from a SQL DB...as of right now, when the visitor comes to the page the whole sql table is produced before them.
What I would like to do is make a UI that will allow the user to display portions of the SQL-table based on different criteria.
Right now all I'm concerned with is getting it so the user can put a date range into two texboxes, but i'm having trouble getting around the error:
Syntax error converting character string to smalldatetime data type
Why is the code below generating that error? What am I doing wrong? -thanks -neo
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim highdate As DateTime
Dim lowdate As DateTime
lowdate = Format(Convert.ToDateTime(
highdate = Format(Convert.ToDateTime(
Const strConnString As String = "connection string omitted"
Dim objConn As New SqlConnection(strConnStrin
Dim strSQL As String
strSQL = "select * from webdocuments where (((datefiled) > 'lowdate') and ((datefiled) < 'highdate'))"
Dim mycommand As New SqlCommand(strSQL, objConn)
objConn.Open()
DataGrid1.DataSource = mycommand.ExecuteReader(Co
DataGrid1.DataBind()
Catch ex As Exception
Dim strScript As String
strScript = "<script language=javascript>"
strScript &= "alert('something went wrong');"
strScript &= "</script>"
If (Not Page.IsStartupScriptRegist
Page.RegisterStartupScript
End If
End Try
End Sub
ASKER
Hey MikeMCSD...!
Thanks...I couldn't get your string to work, but this worked like a charm...
strSQL = "select * from webdocuments where (((datefiled) > '" & lowdate & "') and ((datefiled) < '" & highdate & "'))"
I appeciate the stimulus.
I'll give you the points if you provide me with a little additional information:
1. Is there a better, more advisable way to do this? Or is this (my) method just as good as any?
2. Was the MCSD hard to get? Was it worth the trouble?
-neo
Thanks...I couldn't get your string to work, but this worked like a charm...
strSQL = "select * from webdocuments where (((datefiled) > '" & lowdate & "') and ((datefiled) < '" & highdate & "'))"
I appeciate the stimulus.
I'll give you the points if you provide me with a little additional information:
1. Is there a better, more advisable way to do this? Or is this (my) method just as good as any?
2. Was the MCSD hard to get? Was it worth the trouble?
-neo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again MIke!
thanks for the points neo
strSQL = "select * from webdocuments
WHERE convert(varchar(30), datefiled, 101) BETWEEN convert(varchar(30), lowdate, 101) AND convert(varchar(30), highdate, 101)"