0

I have a database field of dateTime, I'm added a DateTimePicker Control into the window , I want that if not selected any date. Not enter anything into the database I tried the following code And it returns an error ,Does anyone have an idea? I am working on WPF, C # and Access DB

try
{
    string insert_query = "INSERT INTO dbo_tasks ( Date_Created, write_From, Task_To ) " +
                          "VALUES ('" + DP_date.SelectedDate + "', " + txt_from.SelectedValue + ", " + txt_to.SelectedValue + ")";

    con1.Open();
    OleDbCommand cmd = new OleDbCommand(insert_query, con1);
    cmd.ExecuteNonQuery();
}
catch (Exception)
{

    MessageBox.Show("");
}
con1.Close();
4

1 回答 1

1

您需要添加逻辑,如果没有选择日期,那么。

“插入到 dbo_tasks (Date_Created, write_From, Task_To) VALUES ('" + null+ "', " + txt_from.SelectedValue + ", " + txt_to.SelectedValue + ")"

问题是如果没有选择日期,DP_date.SelectedDate 不会返回 null。

这将在 dbnull 中插入一个空值。Date 是一个接受 null 的 smalldatetime

插入 [test].[dbo].[DateRange] ([id] ,[date]) VALUES ('1000',null) GO

于 2012-05-16T19:46:29.030 回答