Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Fellows,
I want to give date format like dd/MM/yyyy in two textboxes. But it is giving this excpetion ("String was not recognized as a valid DateTime"). I am giving follwing code. Plz help me out.

private void frmOrdersAcknowledgement_Load(object sender, EventArgs e)
        {
            txtFromDate.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
            txtToDate.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
                   }

private void btnSearchOrders_Click(object sender, EventArgs e)
        {
           
    try {
DateTime dtFrom = Convert.ToDateTime(txtFromDate.Text);
DateTime dtTo = Convert.ToDateTime(txtToDate.Text);
gvOrderAcknowledge.DataSource = orderBL.GetAllOrders(dtFrom, dtTo, cbxShops.SelectedValue.ToString());
   }
            
   catch { }

        }
Posted
Updated 27-Jul-10 2:58am
v2

1 solution

Try removing the ".Date" bit:
txtFromDate.Text = DateTime.Now.ToString("dd/MM/yyyy");


And then use DateTime.Parse or DateTime.TryParse (preferred) to convert back from a string to a DateTime.

BTW: Don't keep using DateTime.Now, it is re-generated each time you do that.
Get into the habit of reading it once, as it can eliminate strange errors later on - the time can change between sucessive reads.
DateTime now = DateTime.Now;
txtFromDate.Text = now.ToString("dd/MM/yyyy");
txtToDate.Text = now.ToString("dd/MM/yyyy");
 
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