0
if (objChildFees.childid != null)
{
        objChildFees.classstudentid = 0;
        objChildFees.classid = Convert.ToInt16(ddlClass.SelectedValue);
        objChildFees.centerid = Convert.ToInt32(Session[CommonVariables.gCentreId].ToString());
        objChildFees.roomno = 1;
        objChildFees.startdate = Convert.ToDateTime(txtStartDate.Text.ToString());
        objChildFees.enddate = Convert.ToDateTime(txtEndDate.Text.ToString());
        objChildFees.newfees = Convert.ToDecimal(txtfeesamt.Text.ToString());
        objChildFees.feestype = chkFeesPay.Checked == true ? 1 : 2;
        objChildFees.childdaystype = chkfulltime.Checked == true ? 1 : 2;
        objChildFees.feepermonthforsubsidized = 0;
        objChildFees.feepermonthforpartime = 0;
        objChildFees.feepermonthforparttimesubsidized = 0;
        objChildFees.activestatus = true;
        objChildFees.withdrawaldate = Convert.ToDateTime(txtwtdate.Text);
}

How to add null withdrawal date in this code I am facing the error: String was not recognized as a valid DateTime

1
  • Specify the IFormatProvider. Or use the DateTime.ParseExact method with the exact format specified. Commented Aug 20, 2020 at 14:42

2 Answers 2

1

If objChildFees.withdrawaldate can be null, you can set it as a nullable DateTime. Check the value of txtwtdate.Text before attempting to convert it to a DateTime?

public class ChildFees 
{
  // The ? after DateTime indicates this variable should be a nullable datatype
  DateTime? withdrawldate {get; set;}
  ...
}

objChildFees.withdrawaldate = string.IsNullOrWhiteSpace(txtwtdate.Text) ? null : (DateTime?)Convert.ToDateTime(txtwtdate.Text);

In a production system, you would likely want to use DateTime.TryParse to make sure the value of txtwtdate.Text contains a valid date string when converting to avoid exceptions thrown during conversion.

Sign up to request clarification or add additional context in comments.

4 Comments

I have attempted this but showing error : Type of conditional expression can not be determine because there is no implicit conversion between <null> and <datetime>
@HarshPatel That's because the output of Convert.ToDateTime() is a DateTime, not a DateTime?. You need to cast the result to DateTime? like this: (DateTime?)Convert.ToDateTime(txtwtdate.Text).
Tried this code too but no result error is:SqlException (0x80131904): Procedure or function 'Sp_InsertUpdateChildEnrollmentFeesDetails' expects parameter '@withdrawaldate', which was not supplied.]
@HarshPatel You're getting that error because you didn't provide @withdrawldate as a parameter to your stored procedure. You can either give @withdrawldate a default value of null in your stored procedure or make sure you always pass @withdrawldate as a parameter with a value of either null or an actual date. You'll want to make sure your database field is also set to allow null values.
1
public static class Extenstions
{
    public string ToNullableString(this string value)
    {
         if (string.IsNullOrEmpty(value))
         {
             return null;
         }
         
         return value;
     }
}

Make sure that withdrawaldate property is has a nullable type (DateTime?)

Then used like this Convert.ToDateTime(txtwtdate.Text.ToNullableString())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.