Live Chat Icon For mobile
Live Chat Icon

I get the error message ‘Cast from type DBNull to type String is not valid.’ when I try to display DataReader values on form

Platform: ASP.NET| Category: ADO.NET

Try VB.NET

txtCountry.Text = dr('FieldName').ToString()

C#

txtCountry.Text = dr['FieldName'].ToString();

or Try VB.NET

If dbReader('fieldname').ToString= DBnull.Value.ToString()
	’Empty field value
Else
	’Display value
End if

C#

if (dbReader['fieldname').ToString() == DBNull.Value.ToString() )
{
	//Empty field value
}
else
{
	//display Value
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.