I have one maskedtextbox in windows application 2010 in which i have entered date in dd/mm/yyyy format and converted it in mm/dd/yyyy format(because sql 2008 database accept date in mm/dd/yyyy format) to submit it successfully in sql 2008 database as follows:-for eg 25/09/2013
private void btn_submit_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("INSERT INTO date (discharge_date) values (CONVERT(smalldatetime, @discharge_date, 103))", con);//date is my table name and discharge_date is column name in which i have to save dischargedate.
cmd.Parameters.AddWithValue("@discharge_date", maskedTextBox1.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Records inserted");
}
Now comes the second part i.e I have entered date in dd/mm/yyyy format in the same maskedtextbox and after that i have clicked the retreive button so that the dischargedate entered in maskedtextbox should match with the date saved in sql database and then the matching dischargedates should show in gridview.
But when I enter 25/09/2013 it gaves me an error as follows:-
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
But when I enter 09/25/2013 in maskedtextbox then it succesfully retrieves the saved discharged_dates as 25/09/2013(dd/mm/yyyy) format in gridview.
So MY QUESTION IS WHY I HAVE TO ENTER DATE AS 09/25/2013(MM/DD/YYYY)FORMAT IN MASKEDTEXTBOX TO RETRIEVE DISCHARGEDATES IN GRIDVIEW RATHER THAN 25/09/2013 IN DD/MM/YYYY FORMAT.
↧