SCM

[#1010983] Data following text field is lost if ExecuteReader is called with CommandBehavior.SequentialAccess

View Trackers | Bugs | Download .csv | Monitor

Date:
2011-01-30 19:27
Priority:
3
State:
Open
Submitted by:
Kestutis Lumbis (node)
Assigned to:
Nobody (None)
Npgsql Version:
None
Category:
Group:
Resolution:
None
Summary:
Data following text field is lost if ExecuteReader is called with CommandBehavior.SequentialAccess

Detailed description
Npgsql version 2.0.11.

Problem happens in the following code:

reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read())
{
string userName = this.GetNullableString(reader, 0);
string email = this.GetNullableString(reader, 1);
string passwordQuestion = this.GetNullableString(reader, 2);
string comment = this.GetNullableString(reader, 3);
bool isApproved = reader.GetBoolean(4);
...

Column 3 (comment) has text type. DBNull.Value is returned for any column read after that. If parameterless version of ExecuteReader is called then this code is working perfectly.

GetNullableString is very simple:
private string GetNullableString(NpgsqlDataReader reader, int col)
{
return !reader.IsDBNull(col) ? reader.GetString(col) : null;
}

Followup

Message
Date: 2011-02-09 23:26
Sender: Gildas

It's because you don't call the GetString when the reader is DbNull that you have the problem. In fact in this case, when you have the DbNull value, you don't call the GetString and in the Npgsql internal the current index is not incremented, and so it's lagged from one index(column) and so, when you arrive to the Boolean, in my case it fails because it cant cast the previous string column content into a boolean.

If you remove the IsDBNull test, then you have an InvalidCastException thrown by the NpgsqlDataReader.GetString(Int32 i) because it can't cast a System.DbNull into a String.

Two solutions :
- the easiest, change GetString in order to don't throw an exception and return a null string in that case
- the hardest, check the currentindex correctness with the position given when calling GetValue(position)

Attached Files:

Changes:

No Changes Have Been Made to This Item

Powered By FusionForge