System.FormatException: String was not recognized as a valid DateTime
-
When I don't pass certain value in textbox. It gives me above exception. Here's the code.
cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = txtDOB.Text;
I want to ignore NULL values. How to tackle this problem. I know it should have been easy. But anyhow, I stuck . Regards Asif Rehman
-
When I don't pass certain value in textbox. It gives me above exception. Here's the code.
cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = txtDOB.Text;
I want to ignore NULL values. How to tackle this problem. I know it should have been easy. But anyhow, I stuck . Regards Asif Rehman
If the value in txtDOB.Text is a null or whitespace character, you need to set Value to
DBNull.Value
.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
If the value in txtDOB.Text is a null or whitespace character, you need to set Value to
DBNull.Value
.I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
thanks for quick response. So to my best understanding, I can replace old phrase
cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = txtDOB.Text;
with this one
cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = (string.IsNullOrEmpty( txtDOB.Text)) ? DBNull.Value.ToString() : txtDOB.Text;
Is there any way shorter to do this. Regards Asif Rehman
-
thanks for quick response. So to my best understanding, I can replace old phrase
cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = txtDOB.Text;
with this one
cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = (string.IsNullOrEmpty( txtDOB.Text)) ? DBNull.Value.ToString() : txtDOB.Text;
Is there any way shorter to do this. Regards Asif Rehman
I normally use a utility method like this:
public static object GetValueForParameter(string value)
{
if (!string.IsNullOrWhitespace(value))
return value;
return DBNull.Value;
}Then you can replace your code with
cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = Utility.GetValueForParameter(txtDOB.Text);
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
I normally use a utility method like this:
public static object GetValueForParameter(string value)
{
if (!string.IsNullOrWhitespace(value))
return value;
return DBNull.Value;
}Then you can replace your code with
cmd.Parameters.Add("DOB", SqlDbType.SmallDateTime).Value = Utility.GetValueForParameter(txtDOB.Text);
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
Thanks. It works. Regards Asif Rehman
-
Thanks. It works. Regards Asif Rehman
You're welcome. Thanks for letting me know.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads