Hi VAer-4038,
First, you can use String.IsNullOrEmpty(String) method to check if Field3 or Field4 is null.
Then you can use the conditional operator ?: instead of if else statement.
If it is null you can pass string.Empty into datatable. Otherwise, you can pass Field3 and Field4's value.
You can enter DateTime picker value into database via dateTimePicker1.Value.Date.
Sql statemet looks like:(Id is PRIMARY KEY in my test)
string sqlQuery =("insert into TableRecord (Id,Field1,Filed2,Filed3,Filed4) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.SelectedItem.ToString() + "','"+(string.IsNullOrEmpty(comboBox2.SelectedItem.ToString()) ? string.Empty : comboBox2.SelectedItem.ToString())+"', '"+ (string.IsNullOrEmpty(dateTimePicker1.Value.Date.ToShortDateString()) ? string.Empty : dateTimePicker1.Value.Date.ToShortDateString()) +"') ");
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.