Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Trying to convert Date and time but Its not recogonized...

pls help me to fix this error
C#
if (lblProdid.Text != string.Empty || lblProdid.Text != null)
                {
                    dr = dt.NewRow();

                    dr["Column1"] = lblProdid.Text;
                    dr["Column2"] = txtPrice.Text;
                 
                    DateTime dtime = DateTime.ParseExact(txtExpiryDate.Text.Trim(), "yyyy/dd/MM", CultureInfo.InvariantCulture);
                    dr["Column3"] = dtime;
                    dr["Column4"] = txtActQty.Text;
                    dr["Column5"] = txtMinQty.Text;

                    dt.Rows.Add(dr);
                }
            }
Posted
Updated 23-May-14 19:13pm
v2
Comments
Thanks7872 24-May-14 1:14am    
Whats the value of txtExpiryDate.Text?
You should use DatePicker for DateTime? Are you using?
usha C 26-May-14 2:50am    
Am retrieving the value from DB
the value of txtExpiryDate.Text is 26/5/2014

Refer following solved thread: String was not recognized as a valid DateTime.[^]
 
Share this answer
 
You can use TryParseExact to validate the date input by a user, see example:
C#
using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string dateString = "2014/23/05";   // in "yyyy/dd/MM"format
        DateTime result;
        if (DateTime.TryParseExact(dateString, "yyyy/dd/MM", CultureInfo.InvariantCulture, DateTimeStyles.None, out result)){

            Console.WriteLine(result.ToString("yyyy/MM/dd"));
        } else {
            Console.WriteLine("Date invalid");
        }
    }
}

Suggest use some datetime picker control which will automatic returns a date value upon selection, without the hassle of validation.
 
Share this answer
 
v2
Comments
usha C 26-May-14 3:06am    
Am using Date time picker ...

Tried with your code But getting the same error..

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