Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem getting results from SQL database for two dates in DatePicker .. noting that i'm using SQL Server in local machine and the field i'm referring to its data type in SQL Server is "datetime2(0)"

I have tried this still gives me this error "Incorrect syntax near '?'."

Dim ds As DataSet = New DataSet
'da = New SqlDataAdapter(sqlQRY, cnnOLEDB)

Const sqlQRY As String = "Select * From Customers WHERE DateAdded >= ?"
Dim da As New SqlDataAdapter(sqlQRY, cnnOLEDB)
da.SelectCommand.Parameters.AddWithValue("@p0", Today)


What I have tried:

Private Sub CMD_Go_Date_Click(sender As Object, e As EventArgs) Handles CMD_Go_Date.Click
        Try
            Dim sqlQRY As String
            sqlQRY = "Select * From Customers WHERE DateAdded Between #" + Pick_DateFrom.Value + "# And #" + Pick_DateTo.Value + "#"
            Dim da As SqlDataAdapter
            Dim ds As DataSet = New DataSet
            da = New SqlDataAdapter(sqlQRY, cnnOLEDB)
            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
            da.Fill(ds, "Customers")
            DataGridViewCustomer.DataSource = ds
            DataGridViewCustomer.DataMember = "Customers"
            lbl_RowCount.Text = DataGridViewCustomer.RowCount
            Dim row As Integer
            If DataGridViewCustomer.RowCount > 0 Then
                row = DataGridViewCustomer.FirstDisplayedCell.RowIndex
                DataGridViewCustomer.Rows(row).Selected = True
                DataGridViewCustomer.Sort(DataGridViewCustomer.Columns(6), ListSortDirection.Ascending)
                If RowIndex_Cust_TXT.Text = vbNullString Then
                    Exit Sub
                Else
                    Me.DataGridViewCustomer.ClearSelection()
                    Me.DataGridViewCustomer.Rows(RowIndex_Cust_TXT.Text).Selected = True
                    Me.DataGridViewCustomer.FirstDisplayedScrollingRowIndex = RowIndex_Cust_TXT.Text
                End If
            Else
                row = 0
            End If
        Catch ex As SqlException
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Posted
Updated 22-Sep-19 5:02am
v2

1 solution

You're using an Access query with SQL Server. Obviously, that doesn't work. Also, NEVER use string concatenation to build an SQL query. ALWAYS use parameters.

In Access, parameters are denoted with a ? and no name. In SQL Server, they are named parameters denoted with a @:
sqlQry = "SELECT <fieldList> FROM Customers WHERE DateAdded BETWEEN @StartDate AND @EndDate"
da.SelectCommand.Parameters.Add("@StartDate", Pick_DateFrom.Value);
da.SelectCommand.Parameters.Add("@EndDate", Pick_DateTo.Value);

Of course, this assumes you've checked your DatePicker values to make sure they are valid and in the correct order.
 
Share this answer
 
v2
Comments
BassamKassem 22-Sep-19 11:59am    
your solution is working and i thank you for your help, but final question or help please. how to make my solution work on network drive to be used by different users , before migrating my database from access to SQL i used to point to the mdb file using below command "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Environment.CurrentDirectory & "\FileName.mdb"
Dave Kreskowiak 22-Sep-19 13:59pm    
SQL Server is not a "file-based" database engine. It doesn't use a shared drive. Clients connect to the database, typcially, over TCP/IP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900