user1095549
user1095549

Reputation: 1419

DateTimePicker not selected in wpf

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();

Upvotes: 0

Views: 191

Answers (1)

paparazzo
paparazzo

Reputation: 45096

You need to add logic that if no date is selected then.

"INSERT INTO dbo_tasks ( Date_Created, write_From, Task_To ) VALUES ('" + null+ "', " + txt_from.SelectedValue + ", " + txt_to.SelectedValue + ")"

The problem is that DP_date.SelectedDate does not return null if no date is selected.

This will insert a null as in dbnull. Date is a smalldatetime that accepts null

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

Upvotes: 1

Related Questions