SCM

[#1011270] Unable to cast object of type 'System.String' to type 'System.IFormattable'.

View Trackers | Bugs | Download .csv | Monitor

Date:
2012-10-31 16:13
Priority:
3
State:
Open
Submitted by:
Matei Craciun (mematei)
Assigned to:
Nobody (None)
Npgsql Version:
2.0.12beta
Category:
None
Group:
None
Resolution:
None
Summary:
Unable to cast object of type 'System.String' to type 'System.IFormattable'.

Detailed description
StackTrace:

NpgsqlTypes.BasicNativeToBackendTypeConverter.ToSingleDouble(NpgsqlTypes.NpgsqlNativeTypeInfo TypeInfo, object NativeData, bool ForExtendedQuery)
NpgsqlTypes.NpgsqlNativeTypeInfo.ConvertToBackendPlainQuery(object NativeData)
NpgsqlTypes.NpgsqlNativeTypeInfo.ConvertToBackend(object NativeData, bool ForExtendedQuery)
Npgsql.NpgsqlCommand.PassParam(System.Text.StringBuilder query, Npgsql.NpgsqlParameter p)
Npgsql.NpgsqlCommand.GetClearCommandText()
Npgsql.NpgsqlCommand.GetCommandText()
Npgsql.NpgsqlQuery.WriteToStream(System.IO.Stream outputStream)

I found the following error occurs in version 2.0.12.0 when executing a query that contains a string parameter, with the "float8" type (unable to cast the type string to IFormattable)

I changed back from the ToSingleDouble to the ToBasicType<float> method from 2.0.11.92, to keep it running. Any suggestions on how to tackle this issue?

Followup

Message
Date: 2013-02-27 14:58
Sender: Matei Craciun

I actually managed to find the problem. In previous Npgsql revisions if we passed a valid integer in string format when adding Command parameters, an automatic cast would have been invoked. With the latest build, this situation is no longer handled, and thus an exception is thrown for any parameters that are passed in incorrect format.
Date: 2012-11-01 08:40
Sender: Matei Craciun

It's a function that imports an excel spreadsheet into the database. It constructs the query dynamically.

//Construct the query
for (int i = 0; i <= endIndex - startIndex; i++)
stringBuilder.Append(String.Format(_BaseQueryValues, i) + ",");

MyDB command = new MyDB(stringBuilder.ToString().TrimEnd(','));

//Add the parameters
for (int i = 0; i <= endIndex - startIndex; i++)
{
int rowIndex = startIndex + i;

//the excel row index
if (!_DataBaseColumns.ContainsKey(RowIndexColumnName))
command.AddParameter(string.Format(":RowIndex{0}", i), rowIndex, NpgsqlDbType.Integer);

//the excel row values
foreach (KeyValuePair<string, NpgsqlDbType> parameter in _DataBaseColumns)
command.AddParameter(":" + parameter.Key + i, _DataBaseColumnValues[parameter.Key][rowIndex, 1], parameter.Value);
}

command.ExecuteNonQuery();

I was able to fix the problem on our side(apparently the double values were passed as strings when added as parameters), after extracting the values with the appropriate format, the error no longer occurred.

My apologies for not verifying the code on our side in more detail before posting here, but because the previous version of Npgsql handled the string conversion to double implicitly, i think that maybe the new version should also handle such cases.

Thank you for the quick response.
Date: 2012-10-31 16:55
Sender: Francisco Figueiredo jr.

Would you mind to post the code which gives you this error? This would help us to diagnose this problem.

Thank you.
Date: 2012-10-31 16:17
Sender: Matei Craciun

I forgot to mention, the passed in string is "0", so it is in fact a number.

Attached Files:

Changes:

No Changes Have Been Made to This Item

Powered By FusionForge