I have the stored procedure below:
ALTER PROCEDURE [dbo].[GetGrossIncome]
@Date1 date, @Date2 date, @GrossTotal int OUTPUT
AS
BEGIN
SELECT @GrossTotal = sum(GrandTotal) From ClientInvoicedItems where (InvoicePaidStatus ='FULLY PAID' and (Date >=@date1 and Date <=@Date2)) group by GrandTotal
END
What I want is to calculate the total value in the 'GrandTotal' Column for the date range selected and then save the value in a textbox. Below is the VB code:
Private Sub GetTotalIncome()
Dim conn_String As String
conn_String = "Data Source=Server-Pc;Initial Catalog=AWInformationSystem;Persist Security Info=True;User ID=sa;Password=password1"
Dim Mycon = New SqlConnection(conn_String)
' Dim cmd = New SqlCommand("GetGrossIncome", Mycon)
' Dim dAdapter = New SqlDataAdapter(cmd)
Mycon.Open()
Dim cmd As SqlCommand = New SqlCommand("GetGrossIncome", Mycon)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@date1", TxtDateTo.Text)
cmd.Parameters.AddWithValue("@date2", TxtDateFrom.Text)
cmd.Parameters.Add("@GrossTotal", SqlDbType.Int, 500)
cmd.Parameters("@GrossTotal").Direction = ParameterDirection.Output
cmd.ExecuteNonQuery()
'If Not IsDBNull(cmd.Parameters("@GrossTotal")) Then
TxtGrossIncome.Text = (cmd.Parameters("@GrossTotal").Value())
' End If
Mycon.Close()
End Sub
When i run the code, if i select the same date as the start and end date, i get a value in the text box but its always the wrong value. when I select a different start date and end date i get an error: 'System.InvalidCastException: 'Conversion from type 'DBNull' to type 'String' is not valid.'
A pointer to the right direction is welcome, thanks in advance.