I am using the following bit of code to insert some data into a database:
internal int InsertSample(Sample sample)
{
using (var db = new OleDbConnection(connectionString))
{
var query = Constants.InsertNewSample;
return db.Execute(query, sample);
}
}
And it works fine when I run it locally through Visual Studio.
However, when I publish the site and try inserting, I get an error:
Data type mismatch in criteria expression.
Stack trace
[OleDbException (0x80040e07): Data type mismatch in criteria expression.]
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) +1138392
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +247
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +208
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +58
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +162
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +107
Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) +93
Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) +758
MVCQCPage.Helpers.Access.InsertSample(Sample sample) +208
But this only happens if the value of Sample.MassOff
is in decimal format (e.g. 1.6
).
If this value is passed as an integer (e.g. 5
), it works without any issue when debugging and after publishing.
How is this possible????
The data type in the database is numeric
, and the data type of Sample.MassOff
is string
.
Let me know if I can provide any more details.
I was able to resolve this by doing the following:
MassOff
to double
<globalization uiCulture="en" culture="en-US" />
to web.config
Culture of the system must be an issue here. Make sure Culture of your development system and deployment system is same.
If you cannot control/change Culture of deployment system, you can set Culture of your application as below:
Set globalization
attribute in web.config
<configuration>
<system.web>
<globalization culture="en-US" uiCulture="en-US" />/*Whatever your default culture you want to use irrespective of system*/
</system.web>
</configuration>
If you are using DotNet Framework 4.5, following will do:
CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");//Whatever your default culture you want to use irrespective of system
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
If you are using DotNet Framework 4 or earlier, following is the trick:
CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture("en-US");//Whatever your default culture you want to use irrespective of system
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Type type = typeof(CultureInfo);
type.InvokeMember("s_userDefaultCulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
cultureInfo,
new object[] { cultureInfo });
type.InvokeMember("s_userDefaultUICulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
cultureInfo,
new object[] { cultureInfo });
Call above code at the start of your application.