0

I am trying to convert a date value of YYYY/MM/DD from a textbox to datetime, when the value is correct it is ok but when I tried to input an incorrect data to check with the database, the error return as String was not recognized as a valid DateTime.

Here is my code:

 protected void btnSubmit_Click(object sender, EventArgs e) { string format = "YYYY-MM-DD HH:MM:SS"; DateTime birthday = DateTime.Parse(txtBday.Text); DataSet ds = new DataSet(); ds = (newService.checkAccount()); if (ds.Tables[0].Rows.Count > 0) { foreach (DataRow dRow in ds.Tables[0].Rows) { string accountNo = dRow["ACCTNO"].ToString(); DateTime birthDate = DateTime.Parse(dRow["DATEOFBIRTH"].ToString()); if (accountNo == txtAccountNo.Text.ToString() && birthDate == birthday) { lblMessage.Text = "<br>Account Number Exist. You may now proceed with the registration<br><br>"; HttpCookie lmsCookie = new HttpCookie("id"); lmsCookie.Value = txtAccountNo.Text; Response.Cookies.Add(lmsCookie); Response.Redirect("Step2.aspx"); } else { Image2.Visible = false; lblMessage.Text = "<br>Please check your information and try again." + "<br>Be sure you are entering the correct information.For further assistance, call (+632) 404-2790.<br><br>"; } } } } 

For example I will enter a data that will match in the database, the program will proceed otherwise if I am going to input a data that does not match with any of the existing records in the database, the program will triggers an error, String was not recognized as valid datetime.

3 Answers 3

3

When you parsing can fail for a reason other than a bug, instead of this:

DateTime birthDate = DateTime.Parse(dRow["DATEOFBIRTH"].ToString()) 

(which throws an exception as you've seen), use DateTime.TryParse:

DateTime birthDate; if (DateTime.TryParse(dRow["DATEOFBIRTH"].ToString(), out birthDate)) { // Success case } else { // Handle error case } 

I note that you're not using your format variable, by the way. If you know what the format will be (and your question disagrees with your code, btw) it would be better to use TryParseExact:

if (DateTime.TryParseExact(dRow["DATEOFBIRTH"].ToString(), "YYYY/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out birthDate)) ... 
Sign up to request clarification or add additional context in comments.

4 Comments

Jon, fix the name TryParse -> TryParseExact
can you explain more briefly or give me a sample code to do that?
@Dhenn: Do what, exactly? I've given you all the code I can, really... I don't know how you want to handle the case where the value can't be parsed.
@JonSkeet thanks, I've tried it using the tryparse and it works. Great!
0

Definitely, it will throw an exception when an unsupported format is tried to convert into a datetime. So, before converting you should Parse it by using DateTime.TryParse method.

Comments

0

Sovel ,

Step 1:

this._checkInOutDTO.NgayCham = DateTime.Parse(this.DGVDuLieuVaoRa.Rows[num15].Cells[1].Value.ToString()); // this._checkInOutDTO.NgayCham = Convert.ToDateTime(this.DGVDuLieuVaoRa.Rows[num15].Cells[1].Value.ToString());

Step 2: Format : dd/MM/yyyy

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.