Answered how to handle null value in cmd.ExecuteReader

  • Tuesday, April 24, 2012 4:21 PM
     
      Has Code

    hi dear

    i used this code in my project...

     Private Sub Total_VoucherItem()
            Using Cmd As New SqlClient.SqlCommand("SELECT   SUM(ISNULL(Debit,0)) AS Debit ,SUM(ISNULL(Credit,0)) AS Credit FROM VoucherItem WHERE VoucherHeaderRef=" & intYearsRef & txtVoucherHeaderNum.Text, Cn)
                If Cn.State = ConnectionState.Closed Then Cn.Open()
                Dim Reader As SqlClient.SqlDataReader = Cmd.ExecuteReader()
                If Reader.HasRows Then
                    Do While Reader.Read
                        lblDebit.Text = FormatNumber(Reader.GetValue(0), 0)
                        lblCredit.Text = FormatNumber(Reader.GetValue(1), 0)
                        lblDifference.Text = FormatNumber(Reader.GetValue(0) - Reader.GetValue(1), 0)
                    Loop
                    Reader.Close()
                End If
                Cn.Close()
            End Using
        End Sub

    but when i have null value i  see this error;

    Conversion from type 'DBNull' to type 'Currency' is not valid.

    please help me and correct my code

    thanks


    Name of Allah, Most Gracious, Most Merciful and He created the human

All Replies

  • Tuesday, April 24, 2012 5:33 PM
     
     Answered

    thanks all

    solved the problem

    http://social.msdn.microsoft.com/Forums/en/transactsql/thread/04f805ce-0c22-4138-8cc5-f61c6e6dccf5


    Name of Allah, Most Gracious, Most Merciful and He created the human

    • Marked As Answer by sh2014 Tuesday, April 24, 2012 5:39 PM
    •  
  • Monday, April 30, 2012 8:40 AM
    Moderator
     
     

    Hi sh2013,

    Welcome to MSDN Forum.

    I'm glad to hear that you have solved the issue, and thanks for your sharing.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us