Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
Hello.

Thanks in Advance.

I am facing serious problem regarding date format.
When i am fetching my expiry date to the expirydate datetime picker.
It showing the two errors commonly.

1. If i used to show as expirydate.text data in date time picker from dataset
String is not recognized as a valid datetime
2. If i used to show as a expirydate.value data in date time picker from dataset
{System.ArgumentOutOfRangeException: Value of '14/12/2011 00:00:00' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'.
Parameter name: Value
at System.Windows.Forms.DateTimePicker.set_Value(DateTime value)
at NWAFH_HIS.frmPatientRegistration.MainSearchResult()}


MY Code is like this way :
C#
//ExpiryDate.Value = Convert.ToDateTime(ds.Tables[0].Rows[0]["reg_expiry"].ToString(), dateFormat);

                  //ExpiryDate.Text = ds.Tables[0].Rows[0]["reg_expiry"]ToString();
                  //DateTime dtime = Convert.ToDateTime(ds.Tables[0].Rows[0]["reg_expiry"]);
                  string text = ds.Tables[0].Rows[0]["reg_expiry"].ToString();
                  DateTime ddate = DateTime.Parse(text, dateFormat);
                  //string text = dtime.ToString("dd/MM/yyyy", dateFormat);
                  ////ExpiryDate.Value = Convert.ToDateTime(DateTime.Parse(String.Format("{0:MM/dd/yyyy}", ddate).Trim()));
                  //ExpiryDate.Text = String.Format("{0:MM/dd/yyyy}", ddate).Trim();
                  //ExpiryDate.Text = text;
                  //ExpiryDate.Text = (ds.Tables[0].Rows[0]["reg_expiry"]).ToString("dd/MM/yyyy");
  ExpiryDate.Value = Convert.ToDateTime(ds.Tables[0].Rows[0]["reg_expiry"].ToString());
                     //ExpiryDate.Text =  ds.Tables[0].Rows[0]["reg_expiry"].ToString();


My Database field is 'reg_expiry' and i used datetime as the format.
And all the data saved as '2016-03-12 00:00:00.000' like this format
I am just retrieving the same data as a datetime
Also i tried with
SQL
convert(varchar(10),reg_expiry,103)
and
SQL
convert(varchar(10),reg_expiry,101)



And Currenlty my datetime picker property is :
C#
this.ExpiryDate.CustomFormat = "dd/MM/yyyy";
         this.ExpiryDate.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.ExpiryDate.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
         this.ExpiryDate.Location = new System.Drawing.Point(7, 125);
         this.ExpiryDate.Name = "ExpiryDate";
         this.ExpiryDate.Size = new System.Drawing.Size(120, 22);
         this.ExpiryDate.TabIndex = 50;



SQL
with Maxdate : 12/31/9998 and Mindate : 1/1/1753



I tried lots of options/ possibilities unitl now , But still i cannot fetch the datetime field(expiry) value to the datetime picker. So please help me in this regard.
Posted
Updated 4-Dec-11 19:21pm
v2
Comments
[no name] 5-Dec-11 1:22am    
EDIT: added "pre" tag
Dalek Dave 5-Dec-11 5:01am    
Nice to see a good question, well laid out and with information!

try to set '12/14/2011' If this will worked fine.Then you have to change the format of the date before setting this .

Your picker taking this format DD/MM/YYYY you have to change the date in to this format
 
Share this answer
 
hi.

chane you date picker formate into MM/DD/YYYY format.
you will not get that taype of exception
 
Share this answer
 
C#
ExpiryDate.Value = Convert.ToDateTime(ds.Tables[0].Rows[0]["reg_expiry"].ToString());

What is the point of converting a DateTime value to a String in order to convert it back to a DateTime? This is a recipe for disaster, as you have found. Use DateTime values in all cases, except where you need to display it on some user readable device.
 
Share this answer
 
Use ParseExact in place of parce
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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