Re: SqlDataReader showing Empty columns until after error.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Just had another thought

The return statement of your GetInt() function should be:

return reader[ColumnNum]

NOT Reader[ColumnName] as you have it above!

The casting error you are getting which is throwing the exception may
be the attempt to cast the string 'ColumnName' to an integer for
indexing (which should be *ColumnNum*).

That at least would explain why the Catch() doesn't show anything wrong
with the actual data in reader.

Am I right?

Phil H wrote:
I'm sure someone is going to ask, so here's the GetInt()

----------------------------------------------------------------------------------------------------------
public static int GetInt(IDataReader reader, string columnName)
{
int columnNum = reader.GetOrdinal(columnName);
if (reader.IsDBNull(columnNum))
{
return 0;
}
else
{
return (int)reader[columnName];
}
}
----------------------------------------------------------------------------------------------------------

Please forgive me if this is a dumb question but is there any
particular reason why you don't use:

return (int) reader.GetInt32(columnNum)

to read off what you expect to be a 4 byte integer?

.



Relevant Pages

  • Re: SqlDataReader showing Empty columns until after error.
    ... at the value of the reader column. ... dealing with DBNulls which don't convert directly to .Net nulls, ... Does return (int) reader.GetInt32; handle the DBNull issue? ... string columnName) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: SqlDataReader showing Empty columns until after error.
    ... public static int GetInt(IDataReader reader, string columnName) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: SqlDataReader showing Empty columns until after error.
    ... it jumps to the catch, reader's columns then show the proper data. ... The next line, int tid, throws an error {"Specified cast is not ... SqlDataReader reader; ... And this exact same code snippet format is working fine in another ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: definition of u_int32
    ... typedef unsigned long int u_int32; ... implementations/architectures, so you'd actually introduce subtle bugs ... Saving typing is not a valid reason, but if defined properly telling the reader it is 32 bits is, assuming that really is what the programmer intends. ... That was only added in C99, for some strange reason I can't find that header anywhere on my MS VC++ install despite it being the latest version! ...
    (comp.lang.c)
  • Re: [patch] cpufreq: mark cpufreq_tsc() as core_initcall_sync
    ... while (srcu_readers_active_idx(sp, idx)) ... This requires a nested reader, ... deadlock scenario. ... int completed; ...
    (Linux-Kernel)