Solved

c# unspecified error

Posted on 2013-01-31
5
519 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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