Click here to Skip to main content
15,887,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a maskedtextbox to enter date and two buttons for retrieve and submit of date and a gridview to show the retrieve dates fron sql 2008 database and also a print button to take the print out of the dates enter
CSS
The result in printout is for eexample:-25/09/2013 12:00:00AM which I don't want
I don't want the time 12:00:00AM My result should be only DATE I.E 25/09/2013
why it is taking dafault time with it.


I think the dafault time value(12:00:00AM) came during the conversion process during submit event:-
string str = "insert into date values(@discharge_date,@Discharge_Advice)";
            SqlCommand cmd;
            cmd = new SqlCommand(str, con);
            cmd.Parameters.AddWithValue("@discharge_date", SqlDbType.Date).Value = Convert.ToDateTime(maskedTextBox1.Text);//Here it is converting it in datetime format so how to avoid it so that the conversion would be in DATE only.

           

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            MessageBox.Show("Records inserted");
Posted
Updated 4-Oct-13 2:38am
v3
Comments
Bama Boy 4-Oct-13 9:00am    
you can split value of your data

1 solution

If you want to have only date you can use DateTimeFormatInfo and set the date pattern you want and then insert it into database.
C#
public DateTime StringToDate(string date)
{
    DateTime dt = new DateTime();
    DateTimeFormatInfo format = new DateTimeFormatInfo();
    format.ShortDatePattern = "dd/MMM/yyyy";
    try
    {
        dt = DateTime.Parse(date, format);
    }
    catch (Exception e) { }
    return dt;
}
//Call this function
cmd.Parameters.AddWithValue("@discharge_date", SqlDbType.Date).Value = StringToDate(maskedTextBox1.Text);
 
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