Hello Everyone,
I have this code in SQL that I want to use in my vb.net forms, Here is the code :
SELECT Date,Description,Price,Quantity,Department 'Department' FROM [dbo].[Expenses]
WHERE DATEPART(DAY,Date) = '01' AND DATEPART(Month,Date) = '11' AND LTRIM(RTRIM(Department)) = LTRIM(RTRIM('Grocery'))
which works perfectly fine in my SQL but when I do it in my Vb.net forms it wont show any information.
here is the code in vb.net
Private Function GetDetails(ByVal Year As String, ByVal department As String) As String
Dim data As String = ""
Dim query As String = "SELECT Date,Description,Price,Quantity,Department 'Department' FROM [dbo].[Expenses] "
query += "WHERE DATEPART(DAY,Date) = @Day AND DATEPART(Month,Date) = @Month AND LTRIM(RTRIM(Department)) = LTRIM(RTRIM(@UserName))"
Dim dt As DataTable = New DataTable()
Using con As SqlConnection = New SqlConnection("Data Source=ELVIS\SQLEXPRESS;Initial Catalog=Expenses;Trusted_Connection=yes;")
Using command As New SqlCommand(query, con)
command.CommandType = CommandType.Text
command.Parameters.AddWithValue("@Day", Year.Trim())
command.Parameters.AddWithValue("@Month", DateTime.Now.Month)
command.Parameters.AddWithValue("@UserName", department.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("Department")
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.Trim() & " - " & description & " - " & price & " - " & quantity & Environment.NewLine
Next
Return data
End Function
What I have tried:
I tried to put my
' command.Parameters.AddWithValue("@UserName", (ComboBoxDepartment.Text))
inside but it wont do any difference.
My suggestion is that something is wrong with
command.Parameters.AddWithValue("@Month", DateTime.Now.Month)
but I am not sure, I have done some experiments and googled but nothing came up.
Thank you for your help, much appriciated.