• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

handling DB Nulls, sytem.indexoutofrangeexception:

Dim leavecmd As New OracleCommand
        Dim leaveadp As New OracleDataAdapter
        Dim leaveds As New DataSet

 leavecmd.CommandText = searchtravellleavatypesql
            leavecmd.Connection = oraConn
            leaveadp.TableMappings.Add("Table", "LeaveTA")
            leavecmd.ExecuteNonQuery()
            leaveadp.SelectCommand = leavecmd
            leaveadp.Fill(leaveds)

If IsDBNull(leaveds.Tables("LeaveTA").Rows(1)("columna")) Then
  leavetyp1.text  =""

i have the above code...i am trying to read the value in row 1 of the datatable, when there is now record in that row, i am getting an exception

"system.indexoutofrangeexception: there is no row at position 1"


i am displaying the value returmned from the datatable on the form, so if there is no record at particular row, i want to display no text in the particular text  box...

How can i handle this exception..
0
yuvaratna
Asked:
yuvaratna
1 Solution
 
ChloesDadCommented:
You can use a try catch block to catch the error

It is good practice to handle expected specific errors rather that just using catch ex as exception which will catch all errors. Throw will throw the error back to the calling procedure, or cause a break on an unhandled error

Hope this helps
TRY
 
  If IsDBNull(leaveds.Tables("LeaveTA").Rows(1)("columna")) Then
    leavetyp1.text  =""
  ELSE
     ' whatever code you need if it is not null
  End If
 
Catch ex as system.indexoutofrangeexception
 
  ' code here to do something if there is no row 1
 
Catch ex as exception
 
  ' code here to handle some other error that has been caught, usually just the following line
  Throw
 
end try

Open in new window

0
 
JosephGermanOcenaCommented:
You receive this error because there is no existing row in the datatable...
To avoid this error you should check first if there is an existing row..

To do so, try this...

Change this line of code:

If leaveds.Tables("LeaveTA").Rows.Count = 0 then
   If IsDBNull(leaveds.Tables("LeaveTA").Rows(1)("columna")) Then
        leavetyp1.text  =""
   End If
End If

Cheers,

Joseph German Ocena...

Jehova is Great...
0
 
JosephGermanOcenaCommented:
oopps... it sould be like this...

If leaveds.Tables("LeaveTA").Rows.Count <> 0 then
   If IsDBNull(leaveds.Tables("LeaveTA").Rows(1)("columna")) Then
        leavetyp1.text  =""
   End If
End If

Cheers,

Joseph German Ocena...

Jehova is Great...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DreamsTechCommented:
You can use a more efficiet syntax ,with DataReader



if the particular column will have null value ,then "" will get assigned to the Lable otherwise ,it will take the value of the column.

dbreader = cmd.ExecutenonQuery();
 
leavetyp1.text  = IIf(IsDBNull(dbDataReader("ColumnName")), "", dbDataReader("ColumnName"))

Open in new window

0
 
yuvaratnaAuthor Commented:
nope!, it is stiull returnoing the same error message, out of range exception...i am using this right noiw..


Dim leavecmd As New OracleCommand
        Dim leaveadp As New OracleDataAdapter
        Dim leaveds As New DataSet

 leavecmd.CommandText = searchtravellleavatypesql
            leavecmd.Connection = oraConn
            leaveadp.TableMappings.Add("Table", "LeaveTA")
            leavecmd.ExecuteNonQuery()
            leaveadp.SelectCommand = leavecmd
            leaveadp.Fill(leaveds)
 _leavetype2 = (IIf(IsDBNull(leaveds.Tables("LeaveTA").Rows(1)("columna")), "", leaveds.Tables("LeaveTA").Rows(1)("columna"))).ToString()
0
 
ChloesDadCommented:
If you can't prevent the code from being executed (I cant see this code supplied by Joseph

If leaveds.Tables("LeaveTA").Rows.Count <> 0 then
...
endif

then you have to use the try catch end try structure to handle it when it does happen
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now