Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
txtmembdate.Text = row.Cells["bdate"].Value.ToString();

this is my code to get date from datagridview to textbox.

but the thing is i just store date in database. but when i fetch from gridview to the text box from above code i get time also.. as 12/29/1990 12:00PM so i just want to avoid the time.
and while i update my whole application gets hanged..

help me plz.. :(

additional information copied from comment below
C#
private void txtmembdate_Validating(object sender, CancelEventArgs e)
{
   DateTime dob;
   // DOB is required and must be a valid date
   if (!DateTime.TryParse(txtmembdate.Text, out dob))
   {
      errorProviderbdate.SetError(txtmembdate, "Must be a date/time value");
      e.Cancel = true;
      return;
   }
   // Must be 18 or older
   if (dob > DateTime.Now.AddYears(-18))
   {
      errorProviderbdate.SetError(txtmembdate, "Must be 18 or older");
      e.Cancel = true;
      return;
   }
   // DOB is valid
   errorProviderbdate.SetError(txtmembdate, "");
}

dis is my code to validate.. i can only see date in datagridview but, while fetching... time.. also appears
Posted
Updated 30-May-14 9:39am
v2
Comments
ZurdoDev 30-May-14 14:58pm    
Convert to date and use a format that doesn't use time.
Member 9671810 30-May-14 15:19pm    
private void txtmembdate_Validating(object sender, CancelEventArgs e)
{
DateTime dob;
// DOB is required and must be a valid date
if (!DateTime.TryParse(txtmembdate.Text, out dob))
{
errorProviderbdate.SetError(txtmembdate, "Must be a date/time value");
e.Cancel = true;
return;
}
// Must be 18 or older
if (dob > DateTime.Now.AddYears(-18))
{
errorProviderbdate.SetError(txtmembdate, "Must be 18 or older");
e.Cancel = true;
return;
}
// DOB is valid
errorProviderbdate.SetError(txtmembdate, "");

}

dis is my code to validate.. i can only see date in datagridview but, while fetching... time.. also appears

1 solution

That is because you are parsing it to the DateTime object.

You can do one thing. You can convert the Date to a string, which would be of only Date format. Send that to Database.

Refer - How to get only the date excluding time in asp.net c#[^]
 
Share this answer
 
Comments
Member 9671810 31-May-14 13:21pm    
cmd.Parameters.AddWithValue("@bdate",string.IsNullOrEmpty(txtbdate.Text) ? DBNull.Value : (object)DateTime.ParseExact(txtbdate.Text,"dd/mm/yyyy",CultureInfo.InvariantCulture));

wrote above code to insert

and

string strdate = row.Cells["Bdate"].Value.ToString();
string date = DateTime.ParseExact( strdate , "d/MM/yyyy", CultureInfo.InvariantCulture).ToString();
txtmembdate.Text = date;

for gettind date to textbox from datagridview..
still getting error of string was not recognized as valid datetime
Mohan Ghawade 27-Mar-18 12:40pm    
i also have a same problem please help...

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