Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
plz solve this how to add datetimepicker



con.Open();
String q = "insert into Development_Detal values('" + Ename.Text + "','" + UserPass.Text + "','" + EmpID.Text + "','" + address.Text + "','" + emaill.Text + "','"+dateTimePicker1.Value.ToShortDateString()+"', '" + mobNo.Text + "','" + city.Text + "' )";

SqlCommand cmd = new SqlCommand(q, con);


if ((Ename.Text == "") || (UserPass.Text == "") || (EmpID.Text == "") || (address.Text == "") || (mobNo.Text == "") || (city.Text == ""))
{
MessageBox.Show("Must be fill all data ");
}
else
{
cmd.ExecuteNonQuery();
MessageBox.Show("Employee Registration Successfully");
}
Posted

1 solution

You will need to format your date with sql default format (YYYY-MM-DD).

VB
/// <summary>
/// ToSqlDate
///
/// converts datetime object to datetime string
/// with format YYYY-MM-DD HH:MM:SS
/// </summary>
/// <param name="dt">datetime object</param>
/// <returns>string with YYYY-MM-DD HH:MM:SS format</returns>
public string ToSqlDate(DateTime dt)
{
    return string.Format
        (
            "{0}-{1}-{2} {3}:{4}:{5}",
            dt.Year.ToString("0000"),
            dt.Month.ToString("00"),
            dt.Day.ToString("00"),
            dt.Hour.ToString("00"),
            dt.Minute.ToString("00"),
            dt.Second.ToString("00")
        );
}


Then you call it like this:

String q = "insert into Development_Detal values('" + Ename.Text + "','" + UserPass.Text + "','" + EmpID.Text + "','" + address.Text + "','" + emaill.Text + "','"+ToSqlDate(dateTimePicker1.Value)+"', '" + mobNo.Text + "','" + city.Text + "' )";
 
Share this answer
 
Comments
Syed Shahabuddin 19-Feb-13 7:20am    
thanx

i done this but still its not working and not showing any error

Thanx
José Amílcar Casimiro 19-Feb-13 8:33am    
Don't you have an exception executing the code?

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