Click here to Skip to main content
15,896,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai friends,In the following code i want to get return value '_fromDate' as string for comparing it to a varchar field 'date'

C#
private DateTime _fromDate = DateTime.MinValue;
    public DateTime FromDate
    {
        get {
            if (txtFrmDate.Text.ToString() != "")
            {

                _fromDate = Convert.ToDateTime(txtFrmDate.Text, _cInfo);

            }
            else
            {
                txtFrmDate.Text = DateTime.Now.AddMonths(-3).ToString("dd-MM-yyyy");
                _fromDate = Convert.ToDateTime(txtFrmDate.Text, _cInfo);
            }
            return _fromDate;
        }
        set
        {
            txtFrmDate.Text = value.ToString("dd-MM-yyyy");
        }

    }
Posted

use this

C#
public string FromDate
   {
       get {
           if (txtFrmDate.Text.ToString() != "")
           {

               _fromDate = Convert.ToDateTime(txtFrmDate.Text, _cInfo);

           }
           else
           {
               txtFrmDate.Text = DateTime.Now.AddMonths(-3).ToString("dd-MM-yyyy");
               _fromDate = Convert.ToDateTime(txtFrmDate.Text, _cInfo);
           }
           return _fromDate.ToString();
       }
       set
       {
           txtFrmDate.Text = value;
       }

   }

assign the value as below

C#
FromDate = DateTime.Now.ToString();



You need to write capital T and S in tostring.
i thought you know that
now i modified you try it.
 
Share this answer
 
v3
Comments
Nimisha Mary John 14-Oct-11 1:44am    
Sorry to say ,Three errors occured

Error1 'System.DateTime' does not contain a definition for 'tostring' and no extension method 'tostring' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)
Error2 The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments
Error3 Argument 1: cannot convert from 'string' to 'System.IFormatProvider'
the code is here
http://www.java2s.com/Code/ASP/Development/ConvertdatetostringC.htm[^]

and its working i have tried this on my machine.
 
Share this answer
 
Comments
Nimisha Mary John 14-Oct-11 2:03am    
I used that but got error on set part, are following
Error1 The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments Error2 Argument 1: cannot convert from 'string' to System.IFormatProvider'


This is the line Which have error
---------------------------------
set
{
txtFrmDate.Text = value.ToString("dd-MM-yyyy");
}
Its working try
private DateTime _fromDate = DateTime.Now;
   static  public   string  cultureName = "en-US";
   System.Globalization.CultureInfo culture = new CultureInfo(cultureName);
   public DateTime FromDate
   {

       get
       {
           if (txtFrmDate.Text.ToString() != "")
           {

               _fromDate = Convert.ToDateTime(txtFrmDate.Text);

           }
           else
           {
               txtFrmDate.Text = DateTime.Now.AddMonths(-3).ToString();
               _fromDate = Convert.ToDateTime(txtFrmDate.Text,culture);
           }
           return _fromDate;
       }
       set
       {
           txtFrmDate.Text = value.ToString("dd-MM-yyyy");
       }


   }
 
Share this answer
 
Comments
Nimisha Mary John 14-Oct-11 2:47am    
Error in line 15:String was not recognized as a valid DateTime.

The particular line is:
------------------------
_fromDate = Convert.ToDateTime(txtFrmDate.Text,culture);

Nimisha Mary John 14-Oct-11 2:51am    
Hai,In my system date format is of 'english(malaysia)'
I dont know is error becuase of this or not .This is just my doubt.Kindly Ignore it if not
Anuja Pawar Indore 14-Oct-11 3:18am    
txtFrmDate.Text = value.ToString();
use this way it works
Anuja Pawar Indore 14-Oct-11 3:55am    
I just used this line
_fromDate = Convert.ToDateTime("2009/02/03", culture);

Its working fine with any culture. Problem is with your AddMonths
You should use DatetimeDateTime..::.TryParseExact Method[^] for converting. Also make sure you specify the format you are expecting.

This method has 2 variants, both are well explained in msdn link. Refering should help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900