Solved

c# unspecified error

Posted on 2013-01-31
5
511 Views
Last Modified: 2013-02-01
I have a little bug in my c#2010 winforms app.

I have a class that I am using to connect to my database (access database)

at random points my application crashes at the same request with the following


OleDbException was unhandled

and the really useful bit...

'unspecified error'


it always happens here, on the connection.open() line:


OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=W:\\pth\\mydatabase.mdb");

connection.Open();

Open in new window



how can I get to find out what is going on?
0
Comment
Question by:cycledude
  • 4
5 Comments
 
LVL 6

Accepted Solution

by:
esolve earned 500 total points
ID: 38840587
I think you need to manage the state of your connection:

try
{
if (connection.state == ConnectionState.Closed)
{
   connection.Open()
}

//Do what you need to do with your connection

}
catch
{
}
finally
{
  if(connection != null){connection.Close();}
}

A better approach would be to do this:

using(OleDbConnection connection = new OleDbConnection(...))
{
   connection.Open();
   // ... do your stuff here

}  // Connection is disposed and closed here, even if an exception is thrown
0
 

Author Comment

by:cycledude
ID: 38842884
Hi

Thanks for the input, I only posted a small snippet of the code, here is the lot

 public OleDbDataReader data_sql(string strsql)
        {
            
            OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=W:\\pth_to\\my_database.mdb");
           
                connection.Open();
           
            OleDbCommand cmd = new OleDbCommand(strsql);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = connection;
            OleDbDataReader reader = default(OleDbDataReader);
           
            reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            return reader;

        }

Open in new window


as you can see, I am opening the connection, then running a sql command, closing the connection then returning the datareader...
0
 

Author Comment

by:cycledude
ID: 38842968
Hi esolve

I just tried your recommendation  with using()

when the connection closes, it is not returning the datareader (I guess because the connection is closed)  How can I get around this?
0
 

Author Comment

by:cycledude
ID: 38842998
Looks like you were right about the connection state...


everywhere I opened the datareader (dr) to access the database, after I have finished accessingthe data I have now put a dr.Close(); and the problem has gone away I think.

Thanks for the help ;o)
0
 

Author Closing Comment

by:cycledude
ID: 38843001
thanks for the help
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question