Click here to Skip to main content
15,905,590 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello,please i am working on a project.In one of the web page there is a textbox to select the total amount between two given periods which the user must give.The code works fine if the given dates are present in the database.But if those date given are not present it throws an exception saying "Object cannot be cast from DBNull to other types". Don't know what to do to make it work.i am still a baby in vb and sql.Any help is highly appreciated.Below is my code



VB
Protected Sub SearchButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchButton.Click



        If TxtEndDate.Text = String.Empty Then
        ElseIf TxtStartDate.Text = String.Empty Then
            Exit Sub
        Else

            Try


                Dim connectionstring As String = ConfigurationManager.ConnectionStrings("LordsBannerMotorsConnectionString1").ConnectionString
                Dim insertsql As String = "SELECT SUM(Amount) As [Total Amount] FROM MoneyPaidToBank WHERE " &
                                         " Bank_Location=@Location AND Date BETWEEN @Start AND @End"
                Using connection As New SqlConnection(connectionstring)
                    connection.Open()

                    Dim mycommand As New SqlCommand(insertsql, connection)
                    mycommand.Parameters.Add("@Location", SqlDbType.VarChar, 50)
                    mycommand.Parameters("@Location").Value = TotalMoneyDropDownList1.SelectedValue
                    mycommand.Parameters.Add("@Start", SqlDbType.SmallDateTime)
                    mycommand.Parameters("@Start").Value = CType(TxtStartDate.Text, Date)
                    mycommand.Parameters.Add("@End", SqlDbType.SmallDateTime)
                    mycommand.Parameters("@End").Value = CType(TxtEndDate.Text, Date)
                    Dim str As String = ""
                    Dim reader As SqlDataReader = mycommand.ExecuteReader
                    If reader.HasRows Then
                        While reader.Read
                            str &= Convert.ToDouble(reader.Item("Total Amount"))

                        End While
                    Else
                        SearchLabel.Text = "Record not found"
                    End If

                    SearchLabel.Text = "The total Amount is " & str

                    connection.Close()
                End Using
            Catch ex As Exception
                SearchLabel.Text = ex.Message

            End Try
        End If
Posted
Updated 9-Aug-12 10:45am
v2
Comments
Kenneth Haugland 9-Aug-12 16:49pm    
I think your problem is this:
TxtEndDate.Text = String.Empty

I thing youll have to send Nothing if it is empty...
Sergey Alexandrovich Kryukov 9-Aug-12 16:58pm    
No. Nothing is nothing (null), empty is empty.
--SA
[no name] 9-Aug-12 16:56pm    
I think this is what you want to do If Not DbNull.Value.Equals(reader.Item("Total Amount")) Then
str &= Convert.ToDouble(reader.Item("Total Amount"))
End if
jamiebones 9-Aug-12 17:23pm    
Thanks Wes it worked May God bless you for your time.I am really grateful
[no name] 9-Aug-12 17:24pm    
Great! Glad you got it working.

1 solution

If the Amount was not found for the specified location and date range, SUM function returns NULL.
Use COLEASCE()[^] or ISNULL()[^] function, like this:
SQL
SELECT SUM(ISNULL(Amount,0)) As [Total Amount]
FROM MoneyPaidToBank
WHERE Bank_Location=@Location AND Date BETWEEN @Start AND @End
 
Share this answer
 
v2
Comments
Abdul Quader Mamun 9-Aug-12 17:37pm    
good answer!
Maciej Los 9-Aug-12 17:59pm    
Thank you, Abdul ;)

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