Accessing data values using methods

You can access data values in a DataWindow using GetItem and SetItem methods.

You call GetItem methods to obtain the data that has been accepted into a specific row and column. You can also use them to check the data in a specific buffer before you update the database. You call SetItem methods to set the value of a specific row or column. You must use the method appropriate for the column’s datatype.

GetItem methods that return primitive types

These methods obtain the data in a specified row and column in a specified buffer and return a primitive datatype: GetItemDate, GetItemDateTime, GetItemDecimal, GetItemDouble, GetItemString, GetItemTime. All these methods are overloaded. You can specify either the number or the name of the column, and you can optionally specify whether to obtain the original data in the column or the current data, and which DataWindow buffer to get the data from.

For example, the following statement assigns the value from the empname column of the first row to the variable strName in the Primary buffer:

strName = dw1.GetItemString (1, "empname")

GetItem methods that return SqlTypes datatypes

GetItemSqlDateTime, GetItemSqlDecimal, GetItemSqlDouble, and GetItemSqlString work in the same way, but return a SqlTypes datatype.

The methods that return primitive types have the advantage that you do not need to cast the return value to another type, as you do if you use both SqlTypes and primitive types in your code.

However, some primitive datatypes cannot handle null values, so your code must either test whether a value is null before processing it, or include a try-catch block to handle the exception that is thrown if the value is null. You can call a method that returns a SqlTypes datatype and then test whether the return value is null.

Testing for null

Two methods, IsItemNull and SetItemNull, test whether an item is null and set an item to null. This example uses IsItemNull to test whether a column’s value is null before processing it using GetItemDouble:

If dw.IsItemNull(1,1) then
   // handle null value
Else
	Dim val as Integer
	val = CInt(dw.GetItemDouble(1,1))
	// do non-null processing
End If

This example catches the exception that is thrown if the column’s value is null:

Dim val as Integer
Try
   val = CInt(dw.GetItemDouble(1,1))
   // do non-null processing
Catch ex As Exception
	// handle null data
End try

This example uses a method that returns a SqlTypes datatype, so the test for null can be performed after the method call:

Dim val as new System.Data.SqlTypes.SqlDouble
val = dw.GetItemSqlDouble(1,1)
If val.IsNull Then
   // handle null value
Else
   // do non-null processing
   Dim valInteger as Integer
   valInteger = CInt(val.Value)
End if

SetItem methods

These methods set the data in a specified row and column using primitive datatypes: SetItemDate, SetItemDateTime, SetItemDecimal, SetItemDouble, SetItemString, SetItemTime.

These methods use SqlTypes datatypes: SetItemSqlDateTime, SetItemSqlDecimal, SetItemSqlDouble, SetItemSqlString.

For example, the following statement sets the value of the empname column in the first row to the string “Waters”:

dw1.SetItemString(1, "empname", "Waters")