我在将数据从 datepicker 保存到我的 ms sql 数据库时遇到问题。我在 MSSQL 中的列设置为 [datetime]。我有这段代码,但我在保存时出错,因为它无法将日期/时间转换为正确的格式。你能帮助我吗。谢谢你。
错误是:从字符串转换日期和/或时间时转换失败。
protected void RadButton1_Click(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
string str = "insert into Leave_Application values(@Badge_Number, @Name, @Section, @Position, @Date_Hired, @Leave_Type, @Date_From, @Date_To, @Time_From, @Time_To, @Reason, '0', getdate(), 'NULL', ' ' )";
con.Open();
cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@Badge_Number", lblBadgeNo.Text);
cmd.Parameters.AddWithValue("@Name", lblName.Text);
cmd.Parameters.AddWithValue("@Section", lblSect.Text);
cmd.Parameters.AddWithValue("@Position", lblPos.Text);
cmd.Parameters.AddWithValue("@Date_Hired", lblDateEmp.Text);
cmd.Parameters.AddWithValue("@Leave_Type", RadioButtonList1.SelectedValue);
cmd.Parameters.AddWithValue("@Date_From", Convert.ToDateTime(RadDatePicker1.SelectedDate));
cmd.Parameters.AddWithValue("@Date_To", Convert.ToDateTime(RadDatePicker2.SelectedDate));
cmd.Parameters.AddWithValue("@Time_From", Convert.ToDateTime(RadTimePicker1.SelectedDate));
cmd.Parameters.AddWithValue("@Time_To", Convert.ToDateTime(RadTimePicker1.SelectedDate);
cmd.Parameters.AddWithValue("@Reason", txtReason.Text);
cmd.ExecuteNonQuery();
con.Close();
}
}