0

Please see the case 1 and 2 below.

In case 2 : reader.GetString(reader.GetOrdinal("dmic_only")) line return "1" successfully

In case 1 : same code part in string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_only"))) == false throws exception.

Exception : Unable to cast object of type 'System.Byte' to type 'System.String'.

dmic_only is a tinyint in database, not byte. It is really interesting. What is difference in cases?

Case 1:

 if ((!reader.IsDBNull(reader.GetOrdinal("dmic_disallowed")) 
 && string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_disallowed"))) == false)
 && (!reader.IsDBNull(reader.GetOrdinal("dmic_only")) 
 && string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_only"))) == false))
{
   retVal.Add("dmic_disallowed", reader.GetString(reader.GetOrdinal("dmic_disallowed")));
   retVal.Add("dmic_only", reader.GetString(reader.GetOrdinal("dmic_only")));
}

Case 2 :

 //if ((!reader.IsDBNull(reader.GetOrdinal("dmic_disallowed")) 
 //&& string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_disallowed"))) == false)
 //&& (!reader.IsDBNull(reader.GetOrdinal("dmic_only")) 
 //&& string.IsNullOrEmpty(reader.GetString(reader.GetOrdinal("dmic_only"))) == false))
 //{
   retVal.Add("dmic_disallowed", reader.GetString(reader.GetOrdinal("dmic_disallowed")));
   retVal.Add("dmic_only", reader.GetString(reader.GetOrdinal("dmic_only")));
 //}
htcdmrl
  • 194
  • 1
  • 10
  • 2
    `SQL` datatype of `tinyint` "is a" `byte` see [this](http://stackoverflow.com/questions/425389/c-sharp-equivalent-of-sql-server-2005-datatypes) – Squirrel5853 Jan 16 '14 at 12:36
  • good news. thank you. But if there is a cast error, it must be exist in two cases. Why they are acting different – htcdmrl Jan 16 '14 at 12:41
  • 2
    Are you sure about your test cases? `reader.GetString` will throw an exception if the value being returned isn't a string so if you are saying it is a tinyint/byte then that should really throw an exception too... Can you confirm that your tests are indeed what you say they are? – Chris Jan 16 '14 at 12:47
  • yes Chris, im sure. trying case 1 and getting exception than commenting the above lines, trying case 2, there is no exception. Also, i must say that dmic_disallowed also is a tinyint and as you see above it is also in same cases and there is no exception. I really confused. – htcdmrl Jan 16 '14 at 12:59
  • @SecretSquirrel, Not yet :( – htcdmrl Jan 16 '14 at 13:01
  • 1
    @htcdmrl is `dmic_only` the first column in your recordset as `reader.GetOrdinal` would return 1, but this is just the column position in the reader. If you then try and use the `reader.GetString()` this will then try and fetch the data within that column to try and cast it – Squirrel5853 Jan 16 '14 at 13:14
  • sorry should have said second column, obviously index based :) – Squirrel5853 Jan 16 '14 at 13:19
  • Slightly off topic, but I do wonder why you are storing these as `tinyint`s and not `bit`s as to me they read like Boolean variables... – Squirrel5853 Jan 16 '14 at 13:24
  • @SecretSquirrel, I have no right to create or make changes on this database, don't know the answer why they are storing like that :( I got rid of the exception with getting data as reader["dmic_only"].ToString(). thank you for helpfullness ;) – htcdmrl Jan 16 '14 at 13:50

1 Answers1

1

The exception you are getting is telling you exactly what you need to know...

reader.GetString(reader.GetOrdinal("dmic_only")) // will throw an error 

as you have said dmic_only is a tinyint

if you really want it as a string, you will have to unbox it first as a byte then to a string

string strValue = Convert.ToString((byte)reader["dmic_only"))
Squirrel5853
  • 2,376
  • 1
  • 18
  • 34
  • Second line won't work.`string strValue = (string)(byte)reader["dmic_only")` – Sriram Sakthivel Jan 16 '14 at 12:41
  • 1
    Your first statement disagrees with what is said in the quesiton: " `reader.GetString(reader.GetOrdinal("dmic_only"))` line return `1` successfully" – Chris Jan 16 '14 at 12:44
  • *.ToString()* would also be an option (maybe more secure cause if dmic_only is *null* the cast into a byte will fail) – BudBrot Jan 16 '14 at 12:45
  • I should add that it may be the original user is wrong in his statements of testing but you should address this in your answer really. – Chris Jan 16 '14 at 12:45
  • @Pengu I always try to avoid `.ToString()` [link](http://stackoverflow.com/questions/1170756/casting-vs-converting-an-object-tostring-when-object-really-is-a-string) – Squirrel5853 Jan 16 '14 at 12:46
  • @SecretSquirrel, i have no problem with this line. Value is "1", as i want. It is giving error with string.IsNullOrEmpty(). – htcdmrl Jan 16 '14 at 12:47
  • @htcdmrl: if that really is the case then refactor it into a string variable and use from there. It might at least help to highlight where your problem is since I strongly believe that your tests aren't working as described to us. – Chris Jan 16 '14 at 12:48
  • reading the remarks here [link](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring(v=vs.110).aspx) the data must be a string otherwise you will get the `InvalidCastException` – Squirrel5853 Jan 16 '14 at 12:49
  • @SecretSquirrel: I just said something similar in a comment on the question itself. :) – Chris Jan 16 '14 at 12:50
  • @Chris sorry I missed that :) – Squirrel5853 Jan 16 '14 at 12:51