Solved

c# unspecified error

Posted on 2013-01-31
5
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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