locked
see my code RRS feed

  • Question

  • hi 

    see my code OF VB.NET, the portion in "IF IsDBNull(table(0)(0) = 0.0) THEN" IS NOT EXECUTED ELSE PORTION EXECUTED, CAN YOU GUIDE ME. DURING

    DEBUG TABLE(0)(0) VALUE IS 0D.  BALANCE DATATYPE IS MONEY IN SQL SERVER.

      Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True")
                Dim str1 As String
                str1 = "select top 1 Balance FROM  dbo.PaymentReceived where dbo.PaymentReceived.CustomerName ='" + TextBox2.Text + "' order by ReceivedDate desc"
    
                Using adapter As New SqlDataAdapter(str1, connection)
                    Using table As New DataTable()
                        Try
                            adapter.Fill(table)
                            If IsDBNull(table(0)(0) = 0.0) Then 
                                str1 = "select SUM(receiveable)from PaymentReceived where Balance = 0.0 and dbo.PaymentReceived.CustomerName ='anzar'"
                                Using adapter1 As New SqlDataAdapter(str1, connection)
                                    Using table1 As New DataTable()
                                        TextBox5.Text = table1(0)(0)
                                        Label11.Text = table(0)(0)
                                        Label13.Text = Label11.Text
                                    End Using
                                End Using
    
                            Else 'case 2
                                TextBox5.Text = table(0)(0)
                                Label11.Text = table(0)(0)
                                Label13.Text = Label11.Text '- Label12.Text
                                ' TextBox8.Text = Label11.Text - TextBox6.Text
    
                            End If
    
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        Finally
                        End Try
                    End Using
                End Using
            End Using
    THANKS


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Monday, April 13, 2020 7:01 PM

Answers

All replies

  • In the following line you are comparing table(0)(0) with 0 and it will be true or false depending on the data. 

       If IsDBNull(table(0)(0) = 0.0) Then 

    It should be:

    If IsDBNull(table(0)(0)) Then 


    www.cihanyakar.com



    Monday, April 13, 2020 7:15 PM
  • HI,

    thanks for reply,  see the picture,  in it during debug table(0)(0) value is 0

    wait for reply


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Monday, April 13, 2020 7:41 PM
  • But 0 is not DBNull. My guess, you want this:

    If IsDBNull(table(0)(0)) OR table(0)(0) = 0  Then 


    www.cihanyakar.com

    • Marked as answer by muhammadanzar Monday, April 13, 2020 11:07 PM
    Monday, April 13, 2020 7:44 PM
  • Hello,

    To avoid ambiguity specify the column name e.g. replace ColumnName below with the column name you are checkinf for a null/nothing value.

    Note I did an integer rather than a double, sorry

    If table(0)("ColumnName") Is DBNull.Value Then
    
    End If

    Mocked example

    Dim table As New DataTable
    table.Columns.Add(New DataColumn() With {
                         .ColumnName = "ColumnName",
                         .DataType = GetType(Integer)})
    
    table.Rows.Add()
    
    If table(0)("ColumnName") Is DBNull.Value Then
        Console.WriteLine("Null")
    Else
        Console.WriteLine("not Null")
    End If
    
    
    table.Rows(0).SetField(Of Integer)("ColumnName", 1)
    
    If table(0)("ColumnName") Is DBNull.Value Then
        Console.WriteLine("Null")
    Else
        Console.WriteLine("not Null")
    End If
    

    Or tack on value check (and this can be optimized)

    Dim table As New DataTable
    table.Columns.Add(New DataColumn() With {
                 .ColumnName = "ColumnName",
                 .DataType = GetType(Integer)})
    
    table.Rows.Add()
    
    If table(0)("ColumnName") Is DBNull.Value Then
        Console.WriteLine("Null")
    Else
        Console.WriteLine("not Null")
    End If
    
    
    table.Rows(0).SetField(Of Integer)("ColumnName", 1)
    
    If table(0)("ColumnName") Is DBNull.Value Then
        Console.WriteLine("Null")
    Else
        If table(0).Field(Of Integer)("ColumnName") = 1 Then
            Console.WriteLine("Is 1")
        End If
    End If
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, April 13, 2020 7:54 PM