First of all make sure your column has the right data type (DateTime)
Then keep dates as
DateTime
. No need to convert it to
string
before sending it to your database.
Then use a
SqlParameter
to send data. The code you are using now is vulnerable for sql injections!
protected void btnAdd_Click(object sender, EventArgs e)
{
if(cnn.State==ConnectionState.Closed)
cnn.Open();
DateTime dt = DateTime.Parse(txtDate.Text);
cmd = new SqlCommand("insert into tbl_holiday values(@date, @holyday)",cnn);
cmd.Parameters.AddWithValue("@date", dt);
cmd.Parameters.AddWithValue("@holiday", txtHoliday.Text);
cmd.ExecuteNonQuery();
}
Also when you parse dates like you do, you should use TryParse to see if you actually can parse the input from the user.
string d = txtDate.Text;
DateTime dt;
if(DateTime.TryParse(d, out dt))
{
}
else{
}