Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 23-Nov-20 7:25am

1 solution

...AddWithValue("@Day", Year.Trim())

is mysterious


Furthermore:
why do you use LTRIM(RTRIM(...)) rather than TRIM(...)?
why do you trim literal strings ('Grocery') that don't need trimming?
why do you use @UserName where a department name should go?

:)
 
Share this answer
 
v2
Comments
Richard Deeming 24-Nov-20 1:15am    
"why do you use LTRIM(RTRIM(...)) rather than TRIM(...)?"

Because not everyone has upgraded to SQL Server 2017 yet? :)
Member 13410460 25-Nov-20 3:27am    
That's one reason for it :D
Member 13410460 25-Nov-20 3:29am    
@UserName is just an example. I have done so many experiments with different fields to see the changes and it's stuck that one. But I assure you that is not the problem. Thank you for repying
Member 13410460 25-Nov-20 3:29am    
If you have any other suggestions please free to share, I would be more than happy to see a different approach.

Thank you

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