locked
System.Data.OleDb.OleDbException: Unspecified error RRS feed

  • Question

  • User-1693623980 posted

    Hi,

    tody I found three of my websites throw this error

    Unspecified error

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.OleDb.OleDbException: Unspecified error

    Source Error:

    Line 54:                 con.Close();
    Line 55:             }
    Line 56:             throw;
    Line 57:         }
    Line 58:     }


    Source File: f:\domains\k-karafarin.com\wwwroot\App_Code\DALBase.cs    Line: 56

    Stack Trace:

    [OleDbException (0x80004005): Unspecified error]
       System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) +351
       System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) +86
       System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +31
       System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +76
       System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
       System.Data.OleDb.OleDbConnection.Open() +43
       DALBase.ExecuteReader(CommandType commandType, String commandText, OleDbParameter[] commandParameters) in f:\domains\k-karafarin.com\wwwroot\App_Code\DALBase.cs:56
       _Default.GetTop10News() in f:\domains\k-karafarin.com\wwwroot\Default.aspx.cs:24
       _Default.Page_Load(Object sender, EventArgs e) in f:\domains\k-karafarin.com\wwwroot\Default.aspx.cs:17
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
       System.Web.UI.Control.OnLoad(EventArgs e) +91
       System.Web.UI.Control.LoadRecursive() +74
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
    

    this is my dalbase class

    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.Web;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data.OleDb;
    
    /// <summary>
    /// Summary description for DALBase
    /// </summary>
    public class DALBase
    {
    
        private Class1 c = new Class1();
        //public string ConnectionString
        //{
        //    get
        //    {
        //        return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\NextYear\Desktop\bazaryabi\tdb\db1.mdb";
        //    }
        //}
        private DataTable dtt = new DataTable();
        public OleDbDataReader ExecuteReader(CommandType commandType, string commandText, OleDbParameter[] commandParameters)
        {
            OleDbConnection con = new OleDbConnection(c.PRV1());
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;
            cmd.CommandType = commandType;
            cmd.CommandText = commandText;
            cmd.Parameters.AddRange(commandParameters);
    
            bool musCloseConnection = false;
            try
            {
                if (con.State != ConnectionState.Open)
                {
                    musCloseConnection = true;
                    con.Open();
                }
                else
                {
                    musCloseConnection = false;
                }
                OleDbDataReader dataReader = null;
                dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dataReader;
            }
            catch (Exception generatedExceptionName)
            {
                if (musCloseConnection)
                {
                    con.Close();
                }
                throw;
            }
        }
    
    
        public DataTable ExecuteDataTable(CommandType commandType, string commandText, OleDbParameter[] commandParameters)
        {
            OleDbConnection con = new OleDbConnection(c.PRV1());
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;
            cmd.CommandType = commandType;
            cmd.CommandText = commandText;
            cmd.Parameters.AddRange(commandParameters);
    
            bool musCloseConnection = false;
            try
            {
                if (con.State != ConnectionState.Open)
                {
                    musCloseConnection = true;
                    con.Open();
                }
                else
                {
                    musCloseConnection = false;
                }
                OleDbDataReader dataReader = null;
                dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                dtt.Load(dataReader);
                return dtt;
            }
            catch (Exception generatedExceptionName)
            {
                if (musCloseConnection)
                {
                    con.Close();
                }
                throw;
            }
        }
    
    
        public int ExecuteNoneQuery(CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
        {
            using (OleDbConnection con = new OleDbConnection(c.PRV1()))
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = con;
                cmd.CommandType = commandType;
                cmd.CommandText = commandText;
                cmd.Parameters.AddRange(commandParameters);
                con.Open();
                int retVal = cmd.ExecuteNonQuery();
                con.Close();
                return retVal;
            }
        }
        public object ExecuteScaler(CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
        {
            using (OleDbConnection con = new OleDbConnection(c.PRV1()))
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = con;
                cmd.CommandType = commandType;
                cmd.CommandText = commandText;
                cmd.Parameters.AddRange(commandParameters);
                con.Open();
                object retVal = cmd.ExecuteScalar();
                con.Close();
                return retVal;
            }
        }
    
    
    
    
    }
    

    Also I found this 3 websites's targetFramework is "4.0"

    but another one that it's targetFramework is 3.5 hasn't problem.

    error is in line 56 and line 56 is here

        DataTable dtt = new DataTable();
        public OleDbDataReader ExecuteReader(CommandType commandType, string commandText, OleDbParameter[] commandParameters)
        {
            OleDbConnection con = new OleDbConnection(c.PRV1());
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;
            cmd.CommandType = commandType;
            cmd.CommandText = commandText;
            cmd.Parameters.AddRange(commandParameters);

            bool musCloseConnection = false;
            try
            {
                if (con.State != ConnectionState.Open)
                {
                    musCloseConnection = true;
                    con.Open();
                }
                else
                {
                    musCloseConnection = false;
                }
                OleDbDataReader dataReader;
                dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dataReader;
            }
            catch (Exception)
            {
                if (musCloseConnection)
                {
                    con.Close();
                }
                throw; ///////// here is line 56
            }
        }

    what's the problm?

    please help, thanks

    Tuesday, January 21, 2014 3:27 AM

Answers

  • User-1199946673 posted

    Bahare Feizi

            try
            {
                if (con.State != ConnectionState.Open)
                {
                    musCloseConnection = true;
                    con.Open();
                }
                else
                {
                    musCloseConnection = false;
                }
                OleDbDataReader dataReader;
                dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dataReader;
            }
            catch (Exception)
            {
                if (musCloseConnection)
                {
                    con.Close();
                }
                throw; ///////// here is line 56
            }

    The problem is this. You check if the connection is open. If not, you set the boolean to True. However, at this point, the connection isn't open, you try this in the next line.  But for some reason, the connection cannot be opened, and an error is raised. Your code jumps to the Catch part, where you check the boolean, which was set to true, so you try to close a connection which isn't open, which will result in the error. So to solve this, you need to set the boolean AFTER you open the connection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2014 1:21 PM

All replies

  • User-1693623980 posted

    I check those site again and there isn's any problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    what was the reason?Innocent

    Tuesday, January 21, 2014 3:36 AM
  • User-1199946673 posted

    Bahare Feizi

            try
            {
                if (con.State != ConnectionState.Open)
                {
                    musCloseConnection = true;
                    con.Open();
                }
                else
                {
                    musCloseConnection = false;
                }
                OleDbDataReader dataReader;
                dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dataReader;
            }
            catch (Exception)
            {
                if (musCloseConnection)
                {
                    con.Close();
                }
                throw; ///////// here is line 56
            }

    The problem is this. You check if the connection is open. If not, you set the boolean to True. However, at this point, the connection isn't open, you try this in the next line.  But for some reason, the connection cannot be opened, and an error is raised. Your code jumps to the Catch part, where you check the boolean, which was set to true, so you try to close a connection which isn't open, which will result in the error. So to solve this, you need to set the boolean AFTER you open the connection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 21, 2014 1:21 PM