Re: SqlDataReader and System.InvalidCastException



Some more strange behavior, the following line will cause an exception
to be thrown:
rsIntlOrder.GetString(3).Trim()

If I place a breakpoint there, this line works fine in the Immediate
Window and does not throw an error.

Any ideas out there?

Thanks,

Brian

On Aug 28, 2:30 pm, brian.gabr...@xxxxxxxxx wrote:
I am using trying to get some data out of a SQL 2000 database via
stored procedure, but I am getting some strange results.  It will pull
back the first 3 fields but the rest (7 other fields) seem to be
empty, and not just DBNull, but the objects just aren't there.

Here is the code:

SqlCommand cmd = new SqlCommand("psx_GetIntlAdrress", cn);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter paraOrderID = new SqlParameter("@OrderID",
SqlDbType.Char);
paraOrderID.Value = SourceMessage.SelectSingleNode("/Orders/
OrderHeader/OrderID").InnerText;
cmd.Parameters.Add(paraOrderID);

SqlDataReader rsIntlOrder = cmd.ExecuteReader();

if (rsIntlOrder.HasRows)
     {
          while (rsIntlOrder.Read())
               {...

This gives me the "Object reference not set to an instance of an
object." exception:

string sSomeValue = rsIntlOrder[3] != DBNull.Value ?
rsIntlOrder[2].ToString().Trim() : "";

I have verified that the stored procedure is returning all values.  If
I break one a line that works and look at the datareader it shows that
there are 10 fields, but only the first 3 will not throw an error.
Drilling down, the remaining elements show System.InvalidCastException
for all data types.  Not sure what is going wrong here, hoping someone
can help.

Thanks,

Brian
brian.gabr...@xxxxxxxxx

.



Relevant Pages

  • Re: SqlDataReader and System.InvalidCastException
    ... Miha Markic [MVP C#, ... Some more strange behavior, the following line will cause an exception ... I have verified that the stored procedure is returning all values. ...
    (microsoft.public.dotnet.framework.adonet)
  • SqlDataAdapter.Fill returns results of previous command
    ... This code executes successfully at certain points in the application, ... I don't know what the cause of this exception is yet, ... stored procedure was indeed executed on the database. ... The trace shows that the last action on the db was the stored ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Timeout on populating a datagrid
    ... I'd review the stored procedure SQL in a query tool and see how long ... An unhandled exception occurred during the execution of the ... >Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior ...
    (microsoft.public.dotnet.framework.aspnet)
  • Session_End event, System.NullReferenceException
    ... database table that is used to log user sessions. ... The stored procedure executes successfully, and I can see the updated data in ... an exception is being generated by the code that I'ved added to ... through an execution of the code that led to the exception. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Ignore SQL Server 2000 store proc errors and still get results?
    ... catch (Exception ex) ... > You definitely don't want to do 30 round trips by handling the logic ... > temp table in your stored procedure to handle the collection of valid ... >>> expensive in both server and client code. ...
    (microsoft.public.dotnet.framework.adonet)