I would recommend something along these lines where the following is a mockup focused on the date.
/// <summary>
/// Demo to insert new record, get new primary key
/// (You should have a primary key)
/// </summary>
/// <param name="dateTime">Valid DateTime</param>
/// <returns>Caller deconstructs</returns>
public static (int? id, Exception exception) InsertExample(DateTime dateTime)
{
using var cn = new SqlConnection(ConfigurationHelper.ConnectionString());
using var cmd = new SqlCommand { Connection = cn };
cmd.CommandText = @"INSERT INTO dbo.err (dt) VALUES (@dt);SELECT CAST(scope_identity() AS int);";
cmd.Parameters.Add("@dt", SqlDbType.DateTime).Value = dateTime;
try
{
cn.Open();
return (Convert.ToInt32(cmd.ExecuteScalar()), null);
}
catch (Exception ooopsException)
{
return (null, ooopsException);
}
}
Mock-up where you replace string value
with your source
string value = "08/08/2022";
if (DateTime.TryParse(value, out var dateTime))
{
var (id, exception) = InsertExample(dateTime);
if (exception is not null)
{
// handle exception
}
else
{
// success
}
}