Link to home
Start Free TrialLog in
Avatar of neomage23
neomage23Flag for United States of America

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.txtDate1.Text), "MM/dd/yyyy")
            highdate = Format(Convert.ToDateTime(Me.txtDate2.Text), "MM/dd/yyyy")
            Const strConnString As String = "connection string omitted"

            Dim objConn As New SqlConnection(strConnString)
            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(CommandBehavior.CloseConnection)
            DataGrid1.DataBind()


        Catch ex As Exception
            Dim strScript As String
            strScript = "<script language=javascript>"
            strScript &= "alert('something went wrong');"
            strScript &= "</script>"
            If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                Page.RegisterStartupScript("clientScript", strScript)
            End If
        End Try
    End Sub
Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

see if this works:

strSQL = "select * from webdocuments
WHERE convert(varchar(30), datefiled, 101)  BETWEEN  convert(varchar(30), lowdate, 101)  AND convert(varchar(30), highdate, 101)"
Avatar of neomage23

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
ASKER CERTIFIED SOLUTION
Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again MIke!
thanks for the points neo