Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Checking for NULL Values upoon SQL Statment Call

  1. #1

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Resolved [RESOLVED] [2005] Checking for NULL Values upoon SQL Statment Call

    Hello community...

    I have a VB.net app that makes a sql call filling in a form.

    I want to handle null values but i keep getting errors.

    here is the block of code.


    Code:
        Private Sub searchItem()
            Dim serialSelect As String = "equipment_serial_number"
            Dim cfSelect As String = "equipment_cf_tag"
            Dim sqlRowSelect As String = ""
            Dim searchCriteria As String = ""
    
            Dim classValue As String = CStr(cboClass.SelectedValue)
            Dim num As Integer = 1
    
        
    
            If cboCriteria.SelectedIndex = 0 Then
                sqlRowSelect = serialSelect
            Else
                sqlRowSelect = cfSelect
            End If
            searchCriteria = txtCriteriaValue.Text.ToUpper
    
            Dim mySqlConnection As SqlConnection = New SqlConnection(getConnectionString)
            Dim mySqlCommand As SqlCommand = New SqlCommand
            Dim mySqlReader As SqlDataReader = Nothing
    
            mySqlConnection.Open()
            mySqlCommand.Connection = mySqlConnection
            mySqlCommand.CommandText = ( _
            "SELECT equipment_serial_number, equipment_cf_tag, equipment_description, equipment_warranty, item_class_id, location_room_id, status_id, user_id " & _
            "FROM EQ_Equipment " & _
            "WHERE " & sqlRowSelect & " = '" & searchCriteria & "'")
            '**Read and apply values from DB**
            Try
                mySqlReader = mySqlCommand.ExecuteReader
                If mySqlReader.HasRows Then
                    While mySqlReader.Read
                        txtSerialNumber.Text = mySqlReader.GetString(0)
    
    
                        txtCfTag.Text = mySqlReader.GetString(1)
    
     --------------> If IsDBNull(mySqlReader.GetString(2)) Then
                            MessageBox.Show("yes im null")
                        Else
                            'txtNotes.Text = mySqlReader.GetString(2)
                        End If
    
                        dtpWarranty.Value = mySqlReader.GetDateTime(3)
                        cboClass.SelectedValue = mySqlReader.GetInt32(4)
                        cboLocation.SelectedValue = mySqlReader.GetInt32(5)
                        cboStatus.SelectedValue = mySqlReader.GetInt32(6)
                        cboUserName.SelectedValue = mySqlReader.GetInt32(7)
                        lblBoxDept.Text = getDepartment(mySqlReader.GetInt32(7))
                    End While
                    previousSerial = txtSerialNumber.Text
    
                    If classValue = num.ToString Then
                        getBigBrother()
                    End If
    
                    searchfound()
                    variableUpdate()
    
                Else
    
                    If cboCriteria.SelectedIndex = 0 Then
                        MessageBox.Show("No item found with serial number: " & txtCriteriaValue.Text, "no record", MessageBoxButtons.OK)
                    Else
                        MessageBox.Show("No item found with property decal " & txtCriteriaValue.Text, "no record", MessageBoxButtons.OK)
                    End If
                    clearNonEditContent()
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            mySqlConnection.Close()
        End Sub
    THE area in bold is what im trying to handle but i suppose if i get it working i can use it for any field. the error im getting is:

    "Data is Null. This method or property cannot be called on null values."

    So im stumped.. I tested the code with a check to see what it was pulling from the database using getsqlvalue and it says "Null".. so im stuck...

    Any help would be appreciated.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Checking for NULL Values upoon SQL Statment Call

    The SQL reader has it's own IsDBNull method. Use that instead

    Code:
        If mySqlReader.IsDBNull(2) Then
    
        End If

  3. #3

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    Re: [2005] Checking for NULL Values upoon SQL Statment Call

    DOH!.. thanx...

    I actualy used that but i did not put my column refrence "(2)" at the end of the method.

    Again thanx...

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] [2005] Checking for NULL Values upoon SQL Statment Call

    you can also check a value to see if it equals system.dbnull.value, but the method I already posted is your best bet in this scenario when using a datareader.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width