When I try to enter a null value for DataTime in Database I get error message ‘String was not recognized as a valid DateTime’ or ‘Value of type ‘System.DBNull’ cannot be converted to ‘String”

Platform: ASP.NET| Category: ADO.NET

Use namespace


System.Data.SqlTypes

VB.NET


Dim sqldatenull As SqlDateTime
sqldatenull = SqlDateTime.Null

If (txtDate.Text = '') Then
	cmd.Parameters('@Date').Value = sqldatenull
	’cmd.Parameters('@Date').Value = DBNull.Value
Else
	cmd.Parameters('@Date').Value = DateTime.Parse(txtDate.Text)
End If

C#


if (txtDate.Text == '') 
{
	cmd.Parameters ['@Date'].Value =sqldatenull ;
	//cmd.Parameters['@Date'].Value = DBNull.Value;
}  
else
{
	cmd.Parameters['@Date'].Value = DateTime.Parse(txtDate.Text);
}

Share with

Related FAQs

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

Please submit your question and answer.