CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2010
    Posts
    2

    ODBCDataReader Crash Issue

    I have a very simple app that reads from backoffice accounting systems and transfers text files to my server. My problem is with a System based on an Interbase DB. Everything has gone fine except for 4 of my 120 customers. When pulling a 'ClientRemarks' field my ODBCDataReader competely crashes the app. In most cases, I don't even get an catch exception message. I have seen on occassion a message saying, "Attempted to read or write protected memory".

    Here is my stripped down code:
    Code:
    using System;
    using System.Text.RegularExpressions;
    using System.Data.Odbc;
    
    namespace ConsoleAppForTesting
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                promptContinue();
                getData();
                promptContinue();
    
            }
            
            static void getData()
            {
                OdbcDataReader AgencyODBCdr;
                
                string ODBCConnString = "DSN=TRAMS ODBC Datasource;";
                      ODBCConnString += "UID=*******;";
                      ODBCConnString += "PWD=*******;";
                
                string SelectSales = @"
                    SELECT B.BOOKINGNO,I.INVOICENUMBER,B.CLIENTREMARKS
                    FROM INVOICE I INNER JOIN BOOKING B ON I.INVOICENO=B.INVOICE_LINKNO
                    WHERE (I.ISSUEDATE>='2/23/11' AND I.ISSUEDATE <= '2/23/11')
                        AND I.INVOICENUMBER < 4000
                    ORDER BY I.INVOICENUMBER
                ";
    
                try
                {
                    OdbcConnection AgencyODBCConn = new OdbcConnection(ODBCConnString);
                    AgencyODBCConn.ConnectionTimeout = 60;
    
                    OdbcCommand SaleODBCComm = new OdbcCommand(SelectSales, AgencyODBCConn);
                    SaleODBCComm.CommandTimeout = 1180;
                    
                    AgencyODBCConn.Open();
                    AgencyODBCdr = SaleODBCComm.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
    
                    try
                    {
                        if (AgencyODBCdr.HasRows)
                        {
                            Console.Clear();
                            while (AgencyODBCdr.Read())
                            {
                                Console.WriteLine(AgencyODBCdr.GetValue(0).ToString());
                                Console.WriteLine(AgencyODBCdr.GetValue(1).ToString());
    
                                string mine = "";
                                Console.WriteLine(AgencyODBCdr.GetDataTypeName(2));
                                Console.WriteLine(AgencyODBCdr.GetFieldType(2));
                                Console.WriteLine(AgencyODBCdr.GetName(2));
                                if (!AgencyODBCdr.IsDBNull(2))
                                {
                                    try
                                    {
                                        Console.WriteLine(AgencyODBCdr.GetValue(2));
                                    }
                                    catch (Exception err002)
                                    {
                                        Console.WriteLine("Error: " + err002.Message);
                                    }
                                }
                                Console.WriteLine();
                            }
                        }
                        AgencyODBCdr.Close();
                        AgencyODBCConn.Close();
                    }
                    catch (Exception err000)
                    {
                        
                        Console.WriteLine(Environment.NewLine + Environment.NewLine +"Sales Error");
                        Console.WriteLine("Unable to Create Sales Data File... Please Try Again." + Environment.NewLine + Environment.NewLine +
                            "Data: "       + err000.Data       + Environment.NewLine + Environment.NewLine +
                            "Message: "    + err000.Message    + Environment.NewLine + Environment.NewLine +
                            "Source: "     + err000.Source     + Environment.NewLine + Environment.NewLine +
                            "StackTrace: " + err000.StackTrace + Environment.NewLine + Environment.NewLine +
                            "TargetSite: " + err000.TargetSite + Environment.NewLine + Environment.NewLine);
                    }
                }
                catch (Exception err001)
                {
                    Console.WriteLine("ODBC Error");
                    Console.WriteLine("Unable to Extract from " + ODBCConnString + " Database... Please Check Your Settings and Try Again." + Environment.NewLine + "Error Details: " + Environment.NewLine + err001.Message);  
                }
            }
    
            static void promptContinue()
            {
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("===============================================================================");
                Console.WriteLine("Press any key to continue.");
    
                ConsoleKeyInfo mine2 = Console.ReadKey(true);
                Console.Clear();
                Console.WriteLine("Please wait...");
            }
        }
    }
    The moment I try to access the ClientRemark (AgencyODBCdr.GetValue(2) is where I blow up. The client remark field is type varchar(1024).

    The output from the caode typically will look like this:

    96082
    10003388
    VARCHAR
    System.String
    CLIENTREMARKS



    96126
    10003389
    VARCHAR
    System.String
    CLIENTREMARKS
    Disney Magical Express Room Only Pick-up location: Orlando, FL (MCO)




    98196
    11000960
    VARCHAR
    System.String
    CLIENTREMARKS
    .....

    Does anyone have an idea of what is happening and how to get around this DR problem?

  2. #2
    Join Date
    May 2002
    Posts
    511

    Re: ODBCDataReader Crash Issue

    I would think the issue is with the data in the db field.

    Verify the data does not contain any strange characters or otherwise invalid data.

  3. #3
    Join Date
    Feb 2010
    Posts
    2

    Re: ODBCDataReader Crash Issue

    Thanks Tron but that won't be possible. I cannot guarentee that every travel agent in the world won't enter or copy and paste garbage into that field. I need a way to test for validity before actually calling the value in the DR. I can get DR.GetName(2), DR.GetType(2), DR.IsDBNull(2), DR.GetFieldType(2), etc. without issue... I just can't touch the value of the field.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured