alan kumar

alan kumar

  • NA
  • 31
  • 16.6k

Print only date in windows form application in c#

Oct 7 2013 4:03 AM
I have one maskedtextbox for date entry in sql database 2010 (database name date). when I click on retrieve button, it shows the date in gridview but when i click on print button it prints date and default time also for example I entered 25/09/2013 in maskedtextbox and clicked on submit button to save it in sql 2008 database, I have date named table and discharge_date column whose datatype is also in DATE format. when I clicked on retrieve button it shows me date 25/09/2013 in gridview BUT WHEN I CLICK ON PRINT BUTTON IT PRINT 25/09/2013 12:00:00AM DATE WITH DAFAULT TIME WHY IT SHOWS DEFAULT TIME AS i DON'T WANT TO PRINT TIME, I WANT TO PRINT ONLY DATE.
 
My code is as follows:-
 private void Form1_Load(object sender, EventArgs e)
        {
 
            
            maskedTextBox1.Mask = "00/00/0000";
 
            maskedTextBox1.ValidatingType = typeof(System.DateTime);
            maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
           
            toolTip1.IsBalloon = true;
 

        }
private void btn_submit_Click(object sender, EventArgs e)
       {
             string str = "insert into dates values(@discharge_date,@Discharge_Advice)";
           SqlCommand cmd;
           cmd = new SqlCommand(str, con);
            cmd.Parameters.AddWithValue("@discharge_date", SqlDbType.Date).Value = Convert.ToDateTime(maskedTextBox1.Text);
 
            
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close();
 
           MessageBox.Show("Records inserted");
 
       }
 
private void btn_retreive_Click(object sender, EventArgs e)
{
dtusers.Clear();

SqlDataAdapter da = new SqlDataAdapter("select * from dates where discharge_date= CONVERT(smalldatetime, '" + maskedTextBox1.Text + "', 103) ", con);
 
SqlCommandBuilder cmd = new SqlCommandBuilder(da);
da.Fill(dtusers);
dataGridView1.DataSource = dtusers;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
 
}

Answers (5)