0

I am having 2 textboxes and applied timepickers. Here I am leaving textboxes to null i.e not giving any date and sending to db as a fromdate and todate parameters. in msql storedprocedure if Fromdate and todate are empty, one action is performed.inorder to do that task I have taken 2 variables

DateTime fromdate;
DateTime todate;

and declared as a Datetime in codebehind.

fromdate = Convert.ToDateTime(txtfromdate.Text);
todate = Convert.ToDateTime(txttodate.Text);

When I am sending to db

fromdate = Convert.ToDateTime(txtfromdate.Text);
todate = Convert.ToDateTime(txttodate.Text);

it is getting error.formate exception. Is it possible to apply null to datetime variables?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321

2 Answers2

5

Yes, use Nullable<DateTime>:

DateTime? fromDate = null;

To answer the question in your comment, this is how you can return a Nullable<DateTime> (by creating an Extension Method):

public static class DateTimeExtensions
{
    public static DateTime? ToNullableDateTime(this string val)
    {
        DateTime temp;
        return DateTime.TryParse(val, out temp) ? (DateTime?) temp : null
    }
}

and use it:

DateTime? fromDate = txtfromdate.Text.ToNullableDateTime();
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • you mean dateTime? Fromdate=null; and dateTime? ToDate=null.......fromdate=Convert.ToDateTime(txtfromdate.Text);...todate =Convert.ToDateTime(txttodate.Text);...if i give like this is it accepts null?? – sureshchowdary05 Jul 25 '14 at 09:44
  • Doesn't matter what the variable name is. – Yuval Itzchakov Jul 25 '14 at 09:44
  • Note variables in C# are lowerCamelCase – Yuval Itzchakov Jul 25 '14 at 09:45
  • hi..i am not able to convert to string to datetime?..fromdate=convert.todatetime(txt.text)..getting error here..how can i do this casting?? – sureshchowdary05 Jul 25 '14 at 09:57
  • DateTime? fromdate=null; DateTime? todate=null; fromdate =Convert.ToDateTime(txtfromdate.Text); todate =Convert.ToDateTime(txttodate.Text); DataTable l_Assessment = new DataTable(); //DataTable l_Assessment = objAssessmentFacade.GetAlltransactions(fromdate, todate, 0); l_Assessment = objAssessmentFacade.GetAlltransactions(fromdate, todate, _status); if (l_Assessment.Rows.Count >= 0) { gvtransactions.DataSource = l_Assessment; gvtransactions.DataBind(); } – sureshchowdary05 Jul 25 '14 at 10:01
  • You can't do that, as `Convert.ToDateTime` returns a DateTime object. See my edited answer. – Yuval Itzchakov Jul 25 '14 at 10:05
1

You can use

DateTime? Fromdate=null

The "DateTime?" type allow you to use it as null, but it is a value type struct.

Rangesh
  • 728
  • 2
  • 12
  • 27
  • hi..i am not able to convert to string to datetime?..fromdate=convert.todatetime(txt.text)..getting error here..how can i do this casting?? – sureshchowdary05 Jul 25 '14 at 09:59
  • here is my content.... DateTime? fromdate=null; DateTime? todate=null; fromdate =Convert.ToDateTime(txtfromdate.Text); todate =Convert.ToDateTime(txttodate.Text); DataTable l_Assessment = new DataTable(); //DataTable l_Assessment = objAssessmentFacade.GetAlltransactions(fromdate, todate, 0); l_Assessment = objAssessmentFacade.GetAlltransactions(fromdate, todate, _status); if (l_Assessment.Rows.Count >= 0) { gvtransactions.DataSource = l_Assessment; gvtransactions.DataBind(); } – sureshchowdary05 Jul 25 '14 at 10:01