Tuesday, March 20, 2012

Odd Error Upon SqlDataReader.Read() "Invalid length parameter passed to the substring func

An application I developed normally works great, but it seems that when processing a certian record (and none of the others so far), SQL Server throws this error:
"Invalid length parameter passed to the substring function."

Here's the code in question:


orderConnection.Open()
orderReader = orderCommand.ExecuteReader()
setControls(orderReader)

...

Private Sub setControls(ByVal dr As SqlDataReader)
If (dr.Read()) Then '<--*******problem line*******


The SqlDataReader (orderReader) doesn't blow up or anything until I call .Read() (and, as mentioned, this problem only occurs for one order). What could be happening here?Can you post the Sql the reader is running as well as the relevant fields from the record that is crashing?|||It turns out that the stored procedure the DataReader was using expected names to be in first-name, space, last-name format and the record in question had only a single name (my company isn't normalizing their databases for some reason).

As a general question, though, do DataReaders not actually call a stored procedure until .Read() is called? I mean, I would have expected things to error out at the call to ExecuteReader(). I know that a DataReader forges a forward-only direct connection to the database, but I'm confused as to the underlying mechanics of the function calls.|||I'm not at dba but I think that the ExecuteReader creates a cursor and points it before the first record. When you call Read() it fetches the next record and at that time evaluates the calculated fields such as the one using the substring function.

No comments:

Post a Comment