Bug 324025 (MONO81345) - OdbcDataReader fails to convert data types to string with .GetString() method
Summary: OdbcDataReader fails to convert data types to string with .GetString() method
Status: RESOLVED FIXED
Alias: MONO81345
Product: Mono: Class Libraries
Classification: Mono
Component: Sys.Data.OleDb (show other bugs)
Version: 1.2
Hardware: Other Linux
: P3 - Medium : Major
Target Milestone: ---
Assignee: Veerapuram Varadhan
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-04-10 16:41 UTC by Nathaniel
Modified: 2008-03-03 21:25 UTC (History)
0 users

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wiest 2007-09-15 20:35:06 UTC


---- Reported by nate@nutty.ca 2007-04-10 09:41:05 MST ----

Important Note:
This bug report is for the ODBC class (which doesn't exist here) and not
for the OleDB, but the problem may also be relevant.

Description of Problem:
When I call the method OdbcDataReader::GetString() on a field that is not
natively a string (ex: Int32, DateTime), this method will fail. It will
throw an exception that the source and destination type are incompatible.

Steps to reproduce the problem:
1. Create a database table with various types: char, int, DateTime
2. Use an OdbcDataReader to read in each row
3. For each field in the OdbcDataReader object, call the GetString(int i)
method.
4. It will work for string based fields, but it will throw an exception for
everything else.

Actual Results:
All fields should automatically be converted to the string without throwing
an exception.

Expected Results:
Schema:  Char       Int         DateTime
Data:    Test1      0           01/01/2007 00:00
Result:  "Test1"    "0"         "01/01/2007 00:00"
Actual:  "Test1"    Exception   Exception

How often does this happen? 
Always.

Workaround:
Temporarily, developers can check the data type of the field and assign it
to the respected object, from which you can later convert to string via the
ToString() method; however this is extra labour that is not necessary.

Additional Information:
Language:  C#, ASP.NET
Runtime:   Apache v2.2.4 (/w mod_mono) with XSP v1.2.3 and Mono v1.2.3.1
Database:  MySQL v4.1.22
ODBC:      MySQL Connector ODBC v3.51.14 x86
Driver:    unixODBC v2.2.12

Example Code:
public void query (string connectString, string SQLCommand)
{
   // Obtain data from the database using an OdbcDataReader
   // Open a connection to the database
   OdbcConnection connection = new OdbcConnection(connectString);
   connection.Open();

   // Obtain a reader with the given SQL command
   OdbcConnection command = new OdbcCommand(SQLCommand, connection);
   OdbcDataReader reader = command.ExecuteReader();

   // Fetch the first row and display the fields
   if ( reader.Read() )
   {
      for (int i = 0; i < reader.FieldCount; i++)
      {
         try
         {
            // This should work for all fields
            Response.Write( reader.GetString(i) + "<br>");
         }
         catch (Exception e)
         {
            // Called when the field data type is not string based
            throw e;
         }
      }
      reader.Close();
   }
}


Unknown bug field "cf_op_sys_details" encountered while moving bug
   <cf_op_sys_details>Slackware Linux v11.0</cf_op_sys_details>

Comment 1 Veerapuram Varadhan 2008-03-03 21:25:05 UTC
Fixed in revision:97230