0

I have two datetimepicker, startDate is stored datetimepicker1 value and endDate is stored datetimepicker2 value.

I want to to get the data between startDate and endDate from database.

Dim bSql As String = "select date, sum(total_price)  from bill  where Date = '" & Format(startDate, "yyyy/MM/dd") & " and Date='" & Format(endDate, "yyyy/MM/dd") & "'"

i tried the code above but it can't work. Anyone can help me?

1
  • This will be vulnerable to sql injection attacks. It's practically begging to get hacked. Commented Nov 15, 2013 at 14:22

6 Answers 6

3

If you're trying to find a string format for a date at all, you've already lost. Try this:

Dim bSql As String = "select date, sum(total_price)  from bill  where Date >= @startDate and Date < @endDate;"
Using cn As New MySqlConnection("connection string here"), _
      cmd As New MySqlCommand(bSql, cn)

    cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = startDate
    cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = endDate.AddDays(1)

    cn.Open()

    '...

End Using

No formatting required or wanted.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this, using the SQL BETWEEN operator, which allows you to specify the lower and upper bounds of a range.

Dim bSql As String = "select date, sum(total_price)  from bill  where Date BETWEEN '" &  startDate.ToString("yyyy/MM/dd") & "' AND '" & endDate.ToString("yyyy/MM/dd") & "' GROUP BY date;"

You will also need to apply a grouping to use the aggregate function "SUM":

-- find all dates with sales and the total prices on each date
SELECT [date], SUM(total_price) AS [TotalPrice]
FROM bill
WHERE [date] BETWEEN '2013-01-01' AND '2013-12-31' -- use appropriate date format here
GROUP BY [date];

8 Comments

Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2013/11/15'' at line 1
Fixed! I don't use MYSQL regularly :)
To be clear, your SQL contains an error because there is not a "GROUP" clause. If you run my SQL snippet in your MySQL Admin window, it should work. Once you see how it works, you can amend your query in VB.NET.
it's work in mySql workbench when i use WHERE [date] BETWEEN '2013/01/01' AND '2013/12/31' but i used datetimepicker to get the date and search the data then it cant work
Great, half of your problem is fixed (you are now using BETWEEN clause!). Next, you need to figure why your date is not formatted correctly... is the VB variable "startDate" a string or a Date object?
|
1

in data base i have DD/MM/YYYY solution is to make in VB MM/DD/YYYY

this is a CODE

oldbc.CommandText = "select * from recette where Date between #10/09/2015# and #10/011/2015#"

Comments

0

Try formatting your dates like this (you will need to use the Value of the DateTimePicker as well):

Format(startDate.Value, "yyyy-MM-dd")

A better option is to use a parameterised query and then you don't have to format the date into any particular format. More info here: How do I create a parameterized SQL query? Why Should I?

Comments

0

Dim bSql As String = "select date, sum(total_price) from bill where Date = " & DateTimePicker1.Text & " and Date=" & DateTimePicker1.Text & ""

Set the datetime picker date format. I hope it will helpful for you...

Comments

0

Another possibility is to make a little function to format DATE variable and return correct date string syntax for MySQL.

Public Function MySQLDate(inDate As Date) As String
    Return "'" & inDate.ToString(format:="yyyy'/'MM'/'dd") & "'"
End Function

Now your query is more readable, easier to create, and looks like this:

Dim bSql As String = "select date, sum(total_price) from bill where Date BETWEEN " & MySQLDate(startDate) & " and " & MySQLDate(endDate)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.