Solved

c# unspecified error

Posted on 2013-01-31
5
509 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks for the help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now