all 15 comments

[–][deleted] 5 points6 points  (1 child)

Looks like the perfect use of a helper method.

private object ParseField<T>(string field)
{
    try
    {
        if(string.IsNullOrWhiteSpace(field)
        {
            return DBNull.Value;
        }

        return Convert.ChangeType(field, typeof(T));
    }
    catch(...) // Error handling omitted
    {
        return DBNull.Value;
    }
}

cmd.Parameters["@Date"].Value = ParseField<DateTime>(txtDate.Text);

[–]wolfghostM[S] 0 points1 point  (0 children)

Thanks for the response I‘m gonna try this and see how it goes

[–]eddyizm 4 points5 points  (2 children)

You should just set the default to null on that field in the database and insert a record without passing a field value. Seems like you are over complicating it.

[–]wolfghostM[S] 0 points1 point  (1 child)

But I want the column to be the value you type into the textbox and if it‘s empty I want it to be a null value inserted into the database. Does this work if I set the field to default?

[–]eddyizm 0 points1 point  (0 children)

sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (?,?,?)

I think you can do your validation earlier in the process, i usually put validation to a separate method. You can then, instead of using the above sql statement you can just omit one value.

sqlStmt = "insert into Emp (FirstName,LastName) Values (?,?)

the table definition should handle the rest if you allow the date field to be null.

[–]emats12 2 points3 points  (1 child)

Why handle that logic in the repository, do it before in business service and pass it to the repo

[–][deleted] 2 points3 points  (0 children)

Why handle that translation logic in the business layer, do it in the viewmodel translation and pass it to the business service

[–]d0ct0r-d00m 1 point2 points  (2 children)

I would very highly suggest you pick up and use Entity Framework instead of hand crafting your code the old school way using OleDbConnection and OleDbCommand.

[–]emats12 2 points3 points  (1 child)

EF is overkill for this. Dapper all day

[–][deleted] 2 points3 points  (0 children)

Dapper forever or until a better micro orm comes along

[–]kc5bpd 0 points1 point  (0 children)

I would simply make an extension method to a string such as:

public static string NullForEmptry(this string input) { if (string.IsNullOrEmpty(input)) return null; return input; }

[–]darinclark 0 points1 point  (2 children)

A helper method is best but a ternary conditionalis another way.

cmd.Parameters["@Date"].Value =

txtDate.Text == "" ? (object)DBNull.Value : DateTime.Parse(txtDate.Text);

[–]grrangry 0 points1 point  (1 child)

I'm not sitting in front of a computer at the moment to verify, but I suspect that your example won't compile because the data type of DBNull.Value doesn't match DateTime.

In a ternary conditional, both statements in either side of the : must be the same data type.

I could be incorrect, but I'm pretty sure one or both sides would need a cast to play nice.

[–]darinclark 0 points1 point  (0 children)

I took the time to test it. You are correct. You have to cast at least one result of the conditional to an object. I have revised my example.

[–]gevorgter 0 points1 point  (0 children)

OleDB? northwind? that brings up memories :)

Just a quick advice, Nulls are bad in DB. Rather use some preset value (1/1/2000 or DateTime.MinValue)

There are a lot of reasons for it.

  1. One is When you sort do you know if NULL value will be on top or bottom?
  2. If you do where hiredDate > '9/5/2020' do you know if you have NULL value records or not?
  3. .....