TL;DR
String.IsNullOrEmpty()
checks only for Empty ([blank]
i.e. ''
or ""
) or Null
values it does not check DBNull
, if any field coming from database has its value DBNull it will raise an error.
IsDBNull()
checks for DBNull
(it is not same as Null
)
.ToString
will convert DBNull
to Empty String i.e. ''
Details
Consider following SQL table example (using SQL Server as base)
Table Structure:
Column_Name Type and Size Other Properties
---------------- ------------- ----------------------
Company_ID int IDENTITY(1,1) NOT NULL
Company_Name nvarchar (50) NOT NULL
Company_Address nvarchar (50) NULL
INSERT statements:
INSERT [tbl_company] ([Company_Name], [Company_Address]) VALUES ('ABC', 'QWERT')
INSERT [tbl_company] ([Company_Name], [Company_Address]) VALUES ('ASD', ' ')
INSERT [tbl_company] ([Company_Name], [Company_Address]) VALUES ('XYZ', '')
INSERT [tbl_company] ([Company_Name]) VALUES ('PQR')
Table Data:
Company_ID Company_Name Company_Address
----------- ---------------- ---------------
1 ABC QWERT
2 ASD [SPACE]
3 XYZ [BLANK]
4 PQR NULL
Testing Company_Address with IsNullOrEmpty() and IsDBNull() using a SqlDataReader (r):
Company_ID IsNullOrEmpty(r("Company_Address")) IsDBNull(r("Company_Address"))
---------- ----------------------------------- ------------------------------
1 False False
2 False False
3 True False
4 ERROR True
And now specific to the Question
What the OP is trying here, lets consider all statements one by one
The IIF statement with IsNullOrEmpty (wrong)
txtCompany.Text = IIf(String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString, "", dtSample.Rows(grdInfo.SelectedIndex).Item("Company"))
In this statement OP is accessing the value as dtSample.Rows(grdInfo.SelectedIndex).Item("Company")
and checking it with IsNullOrEmpty()
and then converting the result of IsNullOrEmpty to string using .ToString
i.e. IsNullOrEmpty(value).ToString()
. If the value is DBNull it will always return an error. The correct way to use it is
IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString)
See last part Company")).ToString
vs Company").ToString)
, just a case of MISPLACED ")"
Second (IIF with IsDBNull) and third (IF with IsNullOrEmpty) statements of OP are correct
txtCompany.Text = IIf(IsDBNull(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString, "", dtSample.Rows(grdInfo.SelectedIndex).Item("Company"))
If String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString) = True Then
txtCompany.Text = ""
Else
txtCompany.Text = dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString
End If
As regards to the second statement OP is correctly ordering the parameters i.e. first Company
field is converted to string using dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString
. This converts any DBNull to Empty string and then it checked for IsNullOrEmpty
. Now as the value has already been converted to EmptyString it will not give any error.
Old discussions with OP
The difference is clear in your text. Your first statement is:
txtCompany.Text = IIf(String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString, "", dtSpecifierRebate.Rows(grdInfo.SelectedIndex).Item("Company"))
and the second one is
If String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString) = True Then
Now break them apart, first statement (IIF)
String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company")).ToString
'Item("Company")).ToString
And second part (IF)
String.IsNullOrEmpty(dtSample.Rows(grdInfo.SelectedIndex).Item("Company").ToString)
'Item("Company").ToString)
Found any difference?
In first statement you are converting the result of IsNullOrEmpty to String
In second one you are converting .Item("Company") ToString and then comparing it.
If .Item("Company")
returns DBNull
then IsNullOrEmpty failed because .Item("Company") returned type DBNull whereas IsNullOrEmpty checks for null
IsDBNull worked because it checks for DBNull
All point of a misplaced bracket ")" It's a typo
And regarding your usage of these statements:
If and IIF need to check the results as booleans and not as strings
It is better recommended to remove the ToString portion of your statements