Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting error while converting string to datetime here my code.

C#
public  class CustomersBusiness
    {
         SqlCommand cmd; SqlDataReader dr;
         string constring = " Data Source=(local);Initial Catalog=Company;Integrated Security=True";
        public List<customers> getcustomers()
        {

            try
            {
                List<customers> lstcustomers = new List<customers>();
                SqlConnection con = new SqlConnection(constring);
                cmd = new SqlCommand("SELECT empid, empname,departmentid, joiningdate, locationid, salary, jobid from employee", con);
                con.Open();
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Customers cst = new Customers();
                        cst.employeeid = Convert.ToInt32(dr[0].ToString());
                        cst.empname = dr[1].ToString();
                        cst.departmentid = Convert.ToInt32(dr[2].ToString());
                        cst.joiningdate = DateTime.ParseExact("YYYY-MM-DD",dr[3]);
                        cst.locationid = Convert.ToInt32(dr[3].ToString());
                        cst.salary = Convert.ToInt32(dr[1].ToString());
                        cst.jobid = Convert.ToInt32(dr[6].ToString());
                        cst.isworking = Convert.ToBoolean(dr[4].ToString());
                        lstcustomers.Add(cst);
                    }
                }
                return lstcustomers;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }

in cst.joiningdate=datetime.parseExact, Here i am getting error please help me

thank you.
Posted
Updated 14-Feb-12 17:07pm
v5
Comments
Varun Sareen 14-Feb-12 22:41pm    
Edit For: Applied pre tag

Just one thing: stop "converting" everything to string, and success will come to you. :-)

—SA
 
Share this answer
 
Dear Friend,

Rather applying
DateTime.ParseExact("YYYY-MM-DD",dr[3])


you should apply it as
DateTime.ParseExact(dr[3].ToString(),"YYYY-MM-DD",CultureInfo.InvariantCulture)


Please follow this link for more details:-

http://www.dotnetperls.com/datetime-parseexact[^]

I hope this will help you out.

Thanks
 
Share this answer
 
Comments
sreekanth12 14-Feb-12 23:31pm    
Here i want to display only date not time. In my Business object i have given like this.
ICustomers
DateTime joiningdate
{
get;
set;
}
public DateTime joiningdate
{
get
{
return _joiningtime;
}
set
{
_joiningtime = value;
}
}

here should i change to date. how could i do that please help me
thank you
Varun Sareen 15-Feb-12 1:58am    
Use DateTime.ToString("Format"); In order to get only the date.

Follow the link:-

http://www.dotnetperls.com/datetime

http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

I Hope this will help you out.
Just use this..

If you are a real developer then you can do anything in programming..
It's too simple

<pre lang="c#">string s = "02/02/2012";
DateTime d = Convert.ToDateTime(s);</pre>

Accept my answer if you like it..
All the best.. :)
 
Share this answer
 
DateTime dt;
if(DateTime.TryParse(  Convert.ToString(dr[3]) ,out dt))
{
    cst.joiningdate = dt;
       
}
 
Share this answer
 
v2
hi you can try this

DateTime dt = Convert.ToDateTime("02-12-12", System.Globalization.CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat);

Note:
en-US is the datetime formate of us
if india
en-IN like this...........

Happy Coding!.......
 
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