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.