IsDBNull ?

knight33

New member
Joined
Jul 4, 2012
Messages
2
Programming Experience
1-3
Hello, I do not speak English, it's Google who made the translation into French.

My problem is that I can not make a en.vb.net 2010 or a Do While loop that checks the FOR (IsDBNulle) of 30 TextBox, that's the loop that I wrote but si'il a field vacuum in the Access database following error "the error is always" the exception has been handled InvalideCastException "
"Conversion from type 'DBNull to type' string 'is invalid"
With you I hope a solution to my problem and thank you in advance



I have many text boxes on one form (about 30).
Is there anyway to check them all at once to see if they are « IsDBNull » or empty instead of writing out a massive line of code to check each one individually such as


‘1
If IsDBNull(ds.Tables("Decision").Rows(Place_Enregistrement).Item("Decentralises")) Then

Me.TB_Decentralises1.Text = ""
Else
Me.TB_Decentralises1.Text = ds.Tables("Decision").Rows(Place_).Item(15)
End If
‘2

If IsDBNull(ds.Tables("Decision").Rows(Place_Enregistrement).Item("Decentralises")) Then
Me.TB_Decentralises2.Text = ""

Else

Me.TB_Decentralises2.Text = ds.Tables("Decision").Rows(Place_).Item(16)

End If

‘3

'…


'Example my "do while", but does not empty fields.



Dim i As Integer
Dim j As Integer
i = 0
j = 16
Do While IsDBNull(ds.Tables("Decision").Rows.Count - 1 <> i) = False

If IsDBNull(ds.Tables("Decision").Rows(i).Item(j)) Then
Me. TB_Decentralises1.Text = ""
Me. TB_Decentralises2.Text = ""
Me. TB_Decentralises3.Text = ""

Else
Me. TB_Decentralises1.Text = (ds.Tables("Decision").Rows(Place_Enregistrement).Item(16))
Me. TB_Decentralises2.Text = (ds.Tables("Decision").Rows(Place_Enregistrement).Item(17))
Me. TB_Decentralises3.Text = (ds.Tables("Decision").Rows(Place_Enregistrement).Item(18))
Exit Do
End If
i = i + 1
Loop and big thank you
 
I am not a very much experienced programer, but every time I need to fill a TextBox.Text with data which comes from a field that could be empty/null, I do this way:


TextBox1.Text = "" : If not IsDBNull(DataTable1.Rows(i).Item(1)) then TextBox1.Text = DataTable1.Rows(i).Item(1).ToString
TextBox2.Text = "" : If not IsDBNull(DataTable1.Rows(i).Item(2)) then TextBox1.Text = DataTable1.Rows(i).Item(2).ToString
TextBox3.Text = "" : If not IsDBNull(DataTable1.Rows(i).Item(3)) then TextBox1.Text = DataTable1.Rows(i).Item(3).ToString


I think it is best because it is easier to read the code, and you are sure that TextBox will remain empty if the field has null content.


However, there is a strange thing in your code. I'll bet it is the very reason you are having problems. You must test, in each and every case, the very same field containing of the data you intend to copy.

Please observe that in numer 1 you test
ds.Tables("Decision").Rows(Place_Enregistrement).Item("Decentralises")
but stores
ds.Tables("Decision").Rows(Place_).Item(15)
then in number 2 you test
ds.Tables("Decision").Rows(Place_Enregistrement).Item("Decentralises")
(that is, the same field that you tested in number 1) and stores
Me.TB_Decentralises2.Text = ds.Tables("Decision").Rows(Place_).Item(16)
You also uses two diferent variables as row pointers,
.Rows(Place_Enregistrement) and .Rows(Place_)
There must be something wrong here.

I hope this helps you.
 
Last edited:
There's actually no need to check for DBNull if you want to use an empty string. You simply call ToString on your value. If it's a String then you will get that String and if it's a DBNull you will get String.Empty:
myTextBox.Text = myDataTable.Rows(rowIndex)(columnIndex).ToString()
 
There's actually no need to check for DBNull if you want to use an empty string. You simply call ToString on your value. If it's a String then you will get that String and if it's a DBNull you will get String.Empty:
myTextBox.Text = myDataTable.Rows(rowIndex)(columnIndex).ToString()

This is a nice thing to know. It will ease my life a lot. I recently migrated from VBA to VB.NET and my brain is used to make detours around dificulties from that older language, and one of them is testing for null database fields before assigning its value to a variable.

Thank you very much!
 
Hello, friends
I have solved my problem this way and it works.


Dim i As Integer         i = 0         Do While IsDBNull(ds.Tables("Decision").Rows.Count - 1 <> i) = False             If IsDBNull(ds.Tables("Decision").Rows(i).Item(0)).ToString Then                 Me.txtRub1Detail.Text = ""                 Me.txtRub12Detail.Text = ""                 Me.txtRub13Detail.Text = ""             Else                 Me.txtRub1Detail.Text = (ds.Tables("Decision").Rows(Place_Enregistrement).Item(16)).ToString                 Me.txtRub12Detail.Text = (ds.Tables("Decision").Rows(Place_Enregistrement).Item(17)).ToString                 Me.txtRub13Detail.Text = (ds.Tables("Decision").Rows(Place_Enregistrement).Item(18)).ToString                 Exit Do             End If             i = i + 1             Loop 


thank you.
 
Back
Top