Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
dateTimePicker1.Value = dss.Tables["ITdata"].Rows[0]["pudate"].ToString();
Posted
Updated 14-Jun-12 22:59pm
v3
Comments
Upniwesh 15-Jun-12 4:49am    
what is the value of the dss.Tables["ITdata"].Rows[0]["pudate"].ToString();
Master Vinu 15-Jun-12 4:50am    
datetime
2012-06-07 10:53:33.093

C#
dateTimePicker1.Value = dss.Tables["ITdata"].Rows[0]["pudate"].ToString();

If it is already a DateTime, then just take the ToString off - it is a bad idea to start casting dates to strings unless you are about to display them, and it can create localisation problems later.
C#
dateTimePicker1.Value = dss.Tables["ITdata"].Rows[0]["pudate"];
If it is a DateTime, but it is returned as a generic object then cast it:
C#
dateTimePicker1.Value = (DateTime) dss.Tables["ITdata"].Rows[0]["pudate"];
If it is a string, then you need to Parse the string, and hope like heck that the string format is the same as your local system! :laugh: If it is a specific string format, then DateTime.ParseExact or DateTime.TryParseExact will be the way to go.
 
Share this answer
 
Comments
markovl 15-Jun-12 4:59am    
Better than mine :) Have a five.
You are trying to assign a string to a DateTime value and that won't happen :)

Try this instead:

C#
DateTime val;
if (DateTime.TryParse(dss.Tables["ITdata"].Rows[0]["pudate"].ToString(), out val)) 
{
    dateTimePicker1.Value = val;
}
else
{
    /** 
    assign some fallback value to dateTimePicker1.Value, or raise an exception, or
    do whatever needs to be done.
    **/
}
 
Share this answer
 
Comments
Manas Bhardwaj 15-Jun-12 4:52am    
correct +5!
markovl 15-Jun-12 4:57am    
Thanks :)
Master Vinu 15-Jun-12 4:53am    
Dear I dont wan use if condition
markovl 15-Jun-12 4:55am    
Any particular reason to not want to use an if? Just curious.
Master Vinu 15-Jun-12 4:56am    
My code is:
textBox3.Text=dss.Tables["ITdata"].Rows[0]["Itemno"].ToString();
textBox1.Text=dss.Tables["ITdata"].Rows[0]["Make"].ToString();
comboBox1.SelectedItem = dss.Tables["ITdata"].Rows[0]["ProductDetail"].ToString();
textBox2.Text = dss.Tables["ITdata"].Rows[0]["Modelno"].ToString();
dateTimePicker1.Value = dss.Tables["ITdata"].Rows[0]["pudate"].ToString();
textBox4.Text = dss.Tables["ITdata"].Rows[0]["VenderDetail"].ToString();
textBox5.Text = dss.Tables["ITdata"].Rows[0]["Approved"].ToString();
dateTimePicker2.Value = dss.Tables["ITdata"].Rows[0]["Warrenty"].ToString();
textBox6.Text = dss.Tables["ITdata"].Rows[0]["Issuedto"].ToString();
textBox7.Text = dss.Tables["ITdata"].Rows[0]["Department"].ToString();
dateTimePicker3.Value = dss.Tables["ITdata"].Rows[0]["Issueddate"];
comboBox2.SelectedItem = dss.Tables["ITdata"].Rows[0]["Reason"].ToString();
comboBox3.SelectedItem = dss.Tables["ITdata"].Rows[0]["Lifecycle"].ToString();
you can write
C#
dateTimePicker1.Value = Convert.ToDateTime(dss.Tables["ITdata"].Rows[0]["pudate"].ToString());
 
Share this answer
 
v2
Comments
Master Vinu 15-Jun-12 5:00am    
thanks upniwesh
Upniwesh 15-Jun-12 5:02am    
Welcome.........
check this snippet, might be helpful

C#
string input = "15-06-2012"; // dd-MM-yyyy    
DateTime d;
if (DateTime.TryParseExact(input, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out d))
{
    // use d
}
 
Share this answer
 
v2
Comments
Master Vinu 15-Jun-12 4:55am    
any code without if condition
C#
DateTimeConverter c = new DateTimeConverter();
            DateTime dt = (DateTime)c.ConvertFromString("2012-05-10");

OR 

            DateTime dt2 = (DateTime)TypeDescriptor.GetConverter(dt).ConvertFrom("2012-05-21");


HTH
 
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