Click here to Skip to main content
15,886,847 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone, I have this code which runs perfectly fine and there is no error just it shows me the wrong information, in fact, it's not wrong it just shows me more then it should, i have datachart that Shows me the datas in months, for example I see datachart of August but when i sent my mouse to the dot it Shows me Information of September also, my date in SQL is formated in DATETIME and then in VB.net I have formated in 'yyyy-MM', this is my Code for annotation

VB
Public Sub (day as String, Username as String) as String 
Dim data As String = ""
Dim constr As String = ";"
Dim query As String = "SELECT Date,Description,Price,Quantity,Username FROM [dbo].[ShareCost] WHERE DATEPART(DAY,Date) = @Day AND LTRIM(RTRIM(Username)) = @UserName"
Dim dt As DataTable = New DataTable()
Using con As SqlConnection = New SqlConnection(constr)
Using command As New SqlCommand(query, con)
command.CommandType = CommandType.Text
 
command.Parameters.AddWithValue("@Day", day.Trim())
command.Parameters.AddWithValue("@UserName", userName.Trim())
Using sda As SqlDataAdapter = New SqlDataAdapter(command)
sda.Fill(dt)
End Using
End Using
End Using
 
For Each row As DataRow In dt.Rows
Dim [date] As String = Convert.ToDateTime(Row("Date")).ToString("dd/MM/yyyy")
Dim Username1 As String = Row("Username")
Dim description As String = Row("Description")
Dim price As String = If(Row("Price").ToString = "", 0, Row("Price").ToString)
Dim quantity As String = If(Row("Quantity").ToString = "", 0, Row("Quantity").ToString)
data = data & [date] & " - " & Username1 & " - " & description & " - " & price & " - " & quantity & Environment.NewLine
Next
Return data
end sub
End Function


What I have tried:

I tried to modify this part of the code :

SQL
"SELECT Date,Description,Price,Quantity,Username FROM [dbo].[ShareCost] WHERE (Date BETWEEN @Day AND DATEADD(MONTH,1,@Day)) AND LTRIM(RTRIM(Username)) = @UserName"

C#
command.Parameters.AddWithValue("@Day", day.Trim() & "-" & DateTime.Now.Day.ToString())
but it gives me error in this line
C#
sda.Fill(dt)

Conversion failed when converting date and/or time from character string.

Thank you for your help,
Best Regards
E
Posted
Updated 21-Sep-20 5:58am
v2
Comments
Richard MacCutchan 21-Sep-20 9:57am    
You are converting @Day to a string where it should be a DateTime.
Member 13410460 21-Sep-20 10:00am    
I am converting to a string because my date is formated in this way '2020-09'. I dont know if this makes sense.
Richard MacCutchan 21-Sep-20 12:07pm    
No it does not make sense. Dates in databases should always be stored as DateTime or Date types. Converting them to strings just makes things complicated (as you have discovered).
Member 13410460 22-Sep-20 1:32am    
Thank you for your Reply, in SQL they are stored as DateTime, but when I bring them to Vb.net Dashboard I convert them into that Format beacuse I Need in my Dashboard to Show me the months of the year , not the days. But now when I want to see all the Information stored in specific day of the year I Kind of Need to get to the days of the month.
Richard MacCutchan 22-Sep-20 3:30am    
Well you are still doing it wrong. The only time you need it as a string is when you want to display it (or part of it) for the benefit of the user. All internal manipulation and calculations should be done using DateTime objects.

1 solution

Quote:
VB.NET
command.Parameters.AddWithValue("@Day", day.Trim() & "-" & DateTime.Now.Day.ToString())
You're passing a string to the query which cannot be interpreted as a valid datetime by SQL. For example, if day is "2", you are passing in "2-21", which is obviously not a valid date.

Pass in date parameters as dates:
VB.NET
command.Parameters.AddWithValue("@Day", DateTime.Today)
It's not clear from your question what date range you are actually trying to view. If it's the current month starting on the specified day, for example:
VB.NET
Dim dayNumber As Integer
If Not Integer.TryParse(day, dayNumber) Then
    ' TODO: Display an error to the user
    Return
End If

Dim today As DateTime = DateTime.Today
Dim dayValue As New DateTime(today.Year, today.Month, dayNumber)
command.Parameters.AddWithValue("@Day", dayValue)
 
Share this answer
 
Comments
Member 13410460 22-Sep-20 1:33am    
Thank you for your Reply sir, I will try it today when I go home. It's not for the currenct Month, I have a Datetimepicker text which has values like "yyyy-MM" and I can choose months within the year to Show me datacharts with values.

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