|
-
May 6th, 2004, 10:41 AM
#1
Thread Starter
Member
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
-
May 6th, 2004, 12:31 PM
#2
Thread Starter
Member
Disregard. I solved it.
Disregard. I solved it.
FoxT
-
May 6th, 2004, 12:52 PM
#3
Frenzied Member
Please post your solution so we can all benefit from it.
-
May 6th, 2004, 12:56 PM
#4
PowerPoster
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
-
May 6th, 2004, 02:26 PM
#5
Thread Starter
Member
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
-
May 6th, 2004, 09:58 PM
#6
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|