igor-tkachev / bltoolkit

Business Logic Toolkit for .NET
MIT License
297 stars 112 forks source link

Inserting only First letter of a string or Null instead of 0 when using Multiple insertion #369

Closed poudigne closed 9 years ago

poudigne commented 9 years ago

After inserting a null or Empty in a specific column, the rest of the insertion in that same column is broken. Like... if it's a String it keep the first letter only, if it's a int and the value is 0 it will insert null.

See my question on Stack overflow for screenshot and more detail http://stackoverflow.com/questions/30979713/bltoolkit-save-null-instead-of-0

poudigne commented 9 years ago

After looking at your code I have found in your class SqlDataProviderBase.SetParameterValue. You have this line of code if (parameter.DbType == DbType.String && ((string)value).Length == 0) parameter.Size = 1;

This mean, as soon as you have an empty string you set the parameter size that you reuse after that to 1 and it breaks everthing. I don't know what's the best fix for that... but for now ill just comment the line for my personnal use.

ili commented 9 years ago

Can you give minimal test sample?

poudigne commented 9 years ago

Commenting the line, worked for me. My co-worker is coding the test sample right now, will send right as soon as it's ready.

ili commented 9 years ago

OK

poudigne commented 9 years ago

About the int being NULL instead of 0. I found it was in this piece of code in DbManager.AssignParameterValue() :

_dataProvider.SetParameterValue(
    Parameter(name),
    value == null || mm.MapMemberInfo.Nullable && _mappingSchema.IsNull(value)?
    DBNull.Value: value)

public virtual bool IsNull(object value)
{
    return TypeAccessor.IsNull(value);
}

private static bool IsNullInternal(object value) // value = 0
{
    if (value == null)
         return true;

    var nullValue = GetNullValue(value.GetType()); // nullValue = 0

    return nullValue != null && value.Equals(nullValue); // 0 != null && 0 == 0 ==> true
}

_mappingSchema.IsNull(value) Return true. Again, not sure if it was intended to. but I need to insert 0 instead of Null.

(i'll include a demo of this bug in the demo too)

poudigne commented 9 years ago

Here's the demo (click the download link on top of the page) https://drive.google.com/file/d/0BzznvANFApmVYU55UUxBS0FncTA/view?usp=sharing

poudigne commented 9 years ago

Are you gonna make a Fix for this?

poudigne commented 9 years ago

Awesome, just updated BLToolkit. You seem to have fix the bug with the string, but not with the int. Thanks .

ili commented 9 years ago

That's strange, nulls for ints should be also fixed, here is the test for your case https://github.com/igor-tkachev/bltoolkit/commit/ae7c14655f45b7ed436da965c460d9541602768a#diff-889a50237b1dd8e31c081c923d848f05R175

poudigne commented 9 years ago

Thanks, ill take a deeper look on monday at work. Thank for the fix.

poudigne commented 9 years ago

BLToolkit did not update correctly when I tested 2 days ago. But it works flawlessly now. You can close this issue. Thanks man.