Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Formatting Text Box to Currency - what if its null?
 

Formatting Text Box to Currency - what if its null?

Hey all,

I have a few text boxes on my form that read from the DB. I want to display these values as currency. However, sometimes these values maybe null in the database, so the following will crash:

FarmTrimPriceTextBox.Text = FormatCurrency(FarmsBindingSource.Current("FarmTrimPrice"), 2)

I also tried
If FarmsBindingSource.Current("FarmTrimPrice").IsDBNull(12) = False Then
FarmTrimPriceTextBox.Text = FormatCurrency(FarmsBindingSource.Current("FarmTrimPrice"), 2)
End If
but this also fails with the followign error message:
Public member 'IsDBNull' on type 'DBNull' not found.


Neither of these work either:


If FarmsBindingSource.Current("FarmTrimPrice") <> Nothing Then
FarmTrimPriceTextBox.Text = FormatCurrency(FarmsBindingSource.Current("FarmTrimPrice"), 2)
End If


If FarmsBindingSource.Current("FarmTrimPrice") = Nothing Then
Else
FarmTrimPriceTextBox.Text = FormatCurrency(FarmsBindingSource.Current("FarmTrimPrice"), 2)
End If


How do I determine if they are null, and if not, format them? I would love to just use FormatCurrency in the Query, but I am using SQL CE which errors out when I try using FormatCurrency in the query.

DigitalFusion  Tuesday, December 02, 2008 6:23 PM

Hi DigitalFusion,

Add if statement to check the value to avoid null value is a correct answer. But in database, if the field is null (DBNull), you pick up the data and fill it into a DataTable, it doesn’t show null value. You can try the following code to check the DBNull value.

Code Snippet
If FarmsBindingSource.Current("FarmTrimPrice").ToString() = ""

Please inform me if it works.

Sincerely,

Kira Qian

Kira Qian  Thursday, December 04, 2008 5:52 AM

Hi DigitalFusion,

Add if statement to check the value to avoid null value is a correct answer. But in database, if the field is null (DBNull), you pick up the data and fill it into a DataTable, it doesn’t show null value. You can try the following code to check the DBNull value.

Code Snippet
If FarmsBindingSource.Current("FarmTrimPrice").ToString() = ""

Please inform me if it works.

Sincerely,

Kira Qian

Kira Qian  Thursday, December 04, 2008 5:52 AM

We are changing the issue type to “Comment�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by editing your initial post and changing the radio button at the top of the post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Thank you!

Best Regards

Kira Qian

Kira Qian  Monday, December 08, 2008 9:41 AM
i changed it back to question so I could "mark as answer" on your post, however the forums say an "unknown error has occured"

this is a repeatable issue.
DigitalFusion  Monday, December 08, 2008 5:44 PM

This forum is now being moved to MSDN 3.x forum, its a real large project. Maybe there is some problem when your post. Please understand!

Sincerely,

Kira Qian

Kira Qian  Tuesday, December 09, 2008 1:39 AM

You can use google to search for other answers

Custom Search

More Threads

• how to get previous row value for a column
• Help with DataSet designer!
• Function for insert statement
• Implements my own currencymanager
• DataGridView Text cell
• Providing INotifyPropertyChanged
• ComboBox issue
• When does set method fire?
• How to show a different text for bool (reverse bind)
• new to using datagrids with Windows Forms