Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsDBNull???

Status
Not open for further replies.

knight33

IS-IT--Management
Jul 4, 2012
3
DZ
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

Code:
‘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
            '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
thank you.
 
You're getting this error because you have written in wrong way the condition. All the arguments must be an index as integer and not a string. for example
dschapters.Tables(0).Rows(0).item(0)
dschapters.Tables(0).Rows(0).item(1)

Your if statement must me like that
If IsDBNull(ds.Tables(0).Rows(0).Item(0)) Then
end if
The first index represent the number of the table, the second the number of ther row and the last one the column index is the item.
So you just have to determine via integer number the index of the table, the index of the row and the index of item as integer each one and not a string. You can also see it in vb.net help. Hope it helps.
 
Thank you "Antzelinaki" your help, I will try to solve the problem...
 

or you may try to add an empty string at the end:

Code:
Me.TB_Decentralises1.Text = ds.Tables("Decision").Rows(Place_).Item(15) [blue]& ""[/blue]

so if your field happens to be a NULL, you will get an empty string instead. No check for NULL needed

Have fun.

---- Andy
 
Hello, friends
I will try your method "Andrzejak" thank you.
I have solved my problem this way and it works, it's just art?.

Code:
Dim i As Integer
        Dim j 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

A+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top