Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c# unspecified error

Posted on 2013-01-31
5
Medium Priority
?
527 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

660 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