Results 1 to 6 of 6

Thread: Verifing is field is NULL

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    49

    Verifing is field is NULL

    Hello,

    I am trying to populate a form from a SQL server table that allows null values. So after I read in a record I am trying to check if the field value is null so that I can assign "" to the textbox on the form. In the example below, if there is a value in the vendor field it works fine. If there is a NULL value in the vendor field it does not assign "", it errors. Can someone tell me why my code is not working here?


    Imports System.Data.SqlClient
    __________________________________

    cmSQL = New SqlCommand(strSQL, cnSQL)
    drSQL = cmSQL.ExecuteReader()

    If drSQL.Read() Then
    If IsDBNull(drSQL.Item("vendor").ToString()) Then
    txtvendor.Text = ""
    Else
    txtvendor.Text = drSQL.Item("vendor").ToString()
    End If
    End If

    Thanks,

    FoxT

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    49

    Disregard. I solved it.

    Disregard. I solved it.

    FoxT

  3. #3
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Please post your solution so we can all benefit from it.

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Here is my solution to it:

    Code:
    cmSQL = New SqlCommand(strSQL, cnSQL)
    drSQL = cmSQL.ExecuteReader()
    
    If drSQL.Read() Then
    If drSQL("vendor") is DBNull Then
    txtvendor.Text = ""
    Else
    txtvendor.Text = drSQL("vendor").ToString()
    End If
    End If

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2004
    Posts
    49

    OK, here is what happened.

    It turned out that the problem was here in this code below. Not my previous code.

    If IsDBNull(drSQL.Item("received").ToString()) Then
    txtreceived.Mask = ""
    txtreceived.CtlText = ""
    txtreceived.Mask = "##/##/####"
    Else

    ****This line here*****
    txtreceived.CtlText = VB6.Format(drSQL.Item("received").ToString(), "MM/DD/YYYY")

    End If

    My solution was to delete this line of code until I find another way to read in a date field and format it to short date in order to pass the value to a MaskEdit TextBox on my form. I would be greatly interested in how others are reading in date values and formatting them for textboxes on data input forms.

    FoxT

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    The DateTime object has a .ToShortDateString() method that does what you want.

    Dim myDate as New DateTime()
    myDate = CType(drSQL.Item("received"), DateTime)
    txtreceived.CtlText = myDate.ToShortDateString()

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