I am facing this issue in asp.net webform from the day one when i started working as a developer .
Anyone suggest me what is the best practise to avoid this error any more please...
i used many different method to deal it but not successfull (Convert.TODatetime,Tryparse ,culcture etc)
protected void Button1_Click(object sender, EventArgs e)
{
{
string EmpName = txtname.Text;
string EmpContact = txtcontact.Text;
string IDDept = DDDept.SelectedValue;
string IDSubDept = DDSubDeptID.SelectedValue;
string IDDesig = DDDesigID.SelectedValue;
string status = DDStatus.SelectedValue;
string JoinDate = txtjdate.Text;
string LeftDate = txtldate.Text;
string Empcur = DDCur.SelectedValue;
string Lentitled = CheckBox1.Text;
string address = txtaddress.Text;
string OTEntitled = DDOTSt.SelectedValue;
string PL = txtPL.Text;
string Dhour = txtdhour.Text;
string LTime = (txtlTime.Text).ToString();
string OTRate = txtOTRate.Text;
// string namDept = DDDept.SelectedValue;
string query = "INSERT INTO EmployeeDetails VALUES(@EmpName,@EmpContact,@IDDept,@IDSubDept,@IDDesig,@status,@JoinDate,@LeftDate,@Empcur,@Lentitled,@Address,@OTEntitled,@PL,@Dhour,@LTime,@OTRate) ";
con = new SqlConnection("Data Source=IT;Integrated Security=SSPI;Initial Catalog=AttendanceManagement");
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@LTime", LTime);
cmd.Parameters.AddWithValue("@OTRate", OTRate);
if ((txtjdate.Text == ""))
{
cmd.Parameters["@JoinDate"].Value = DBNull.Value;
}
else
{
cmd.Parameters["@JoinDate"].Value = DateTime.Parse(txtjdate.Text);
}
if ((txtldate.Text == ""))
{
cmd.Parameters["@LeftDate"].Value = DBNull.Value;
}
else
{
cmd.Parameters["@LeftDate"].Value = DateTime.Parse(txtldate.Text);
}
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(Request.RawUrl);
}
}
}
Thanks in advance