Hi guys,
Can anybody help to figure out the problem. In my C# application project with access database when fetching the records from db it will give me error "Unspecified error".
Thanks in advance...
Hi guys,
Can anybody help to figure out the problem. In my C# application project with access database when fetching the records from db it will give me error "Unspecified error".
Thanks in advance...
Intermittent "Unspecified error" from OleDb against an Access file usually points to an environmental or lifecycle problem rather than a single line of C# logic. The advice from to create and open a dedicated OleDbConnection (instead of reaching into the command for the connection) is the right foundation — it prevents accidental opens/closes and makes it easier to log connection state. In this thread the next useful steps are focused diagnostics and a few common Access-specific failure modes that often appear as vague, sporadic errors.
Likely causes to check, and practical mitigations:
Two concise snippets that help with robustness and diagnosis:
Exception logging (inspect errors and HResult):
catch (System.Data.OleDb.OleDbException ex)
{
foreach (System.Data.OleDb.OleDbError err in ex.Errors)
System.Diagnostics.Trace.WriteLine("OLEDB Error: " + err.Message);
System.Diagnostics.Trace.WriteLine("HResult: " + ex.HResult);
System.Diagnostics.Trace.WriteLine(ex.StackTrace);
throw;
} Safe parameterized select (avoids quoting/type issues; Access uses positional ? parameters):
using (var conn = Connect.AccConn())
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT TimeTakenSec FROM TestMark WHERE que_id = ? AND test_no = ?";
cmd.Parameters.AddWithValue("?", Question);
cmd.Parameters.AddWithValue("?", GlobalMember.TestNumber);
using (var rdr = cmd.ExecuteReader())
{
if (rdr.Read()) TTTaken = rdr["TimeTakenSec"].ToString();
}
}
} After adding targeted logging and switching to a connection-per-operation pattern (as suggested), the remaining intermittent cases usually reveal themselves as permission/lock/AV or platform issues. If problems persist, capture a timestamped log of Connection.State, thread id, full exception details, and whether the DB resides on a network share — that information will narrow the root cause quickly.
Jump to Post— thines01 401Does it always do this?
Does it give the error at a specific line of code?
Does it ONLY give the error when it's getting the data from the database?
Jump to Post— thines01 401Can you show a specific line of code?
Are you sure the database is open before you access it?
Jump to Post— thines01 401...hard to tell.
Can you set a breakpoint on what shows as line 2, then step through the code (with F10 in Visual Studio) until it fails and get the specific line that's causing the problem?
Jump to Post— thines01 401Is there a chance you're attempting to open it twice in some circumstances?
Does it always do this?
Does it give the error at a specific line of code?
Does it ONLY give the error when it's getting the data from the database?
Does it ONLY give the error when it's getting the data from the database?
Can you show a specific line of code?
Are you sure the database is open before you access it?
Here...
string strSel = "select * from TestMark where que_id= " + Question + " and test_no='" + GlobalMember.TestNumber + "'";
Ocmd = new OleDbCommand(strSel, Connect.AccConn() );
Ocmd.Connection.Open();
Odr = Ocmd.ExecuteReader();
if (Odr.Read())
{
TTTaken = Odr["TimeTakenSec"].ToString();
if (rdb1.Checked == true) { UpdateTestAnswersDetail("Option1"); foundQue = true; }
else if (rdb2.Checked == true) { UpdateTestAnswersDetail("Option2"); foundQue = true; }
else if (rdb3.Checked == true) { UpdateTestAnswersDetail("Option3"); foundQue = true; }
else if (rdb4.Checked == true) { UpdateTestAnswersDetail("Option4"); foundQue = true; }
else
{
UpdateTestAnswersDetail();
foundQue = true;
}
}
else
{
foundQue = false;
TTTaken = "0";
label11.Text = "0";
}
label11.Text = TTTaken;
Ocmd.Connection.Close(); ...hard to tell.
Can you set a breakpoint on what shows as line 2, then step through the code (with F10 in Visual Studio) until it fails and get the specific line that's causing the problem?
In line 2 i declare connection string globally as
Class Connect
{
public static OleDbConnection AccConn()
{
//Access 2002-03
return new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\Data\\OTSDB.mdb");
//Access 2007
// return new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\Data\\SNDB.accdb;Persist Security Info=False;");
}
} this error will create at some time not regularly
here i get error msg
Ocmd.Connection.Open(); Is there a chance you're attempting to open it twice in some circumstances?
No. Actually i tried to close connection before opening but still sometimes it throws an error.
Instead of reaching into the command to get the connection, you should create a connection and pass it to the command. That way, they are separate and the connection is not dependent on the life of the command.
If you're doing more than one type of command, this is absolutely necessary.
Otherwise, it's just good practice.
using System.Data.OleDb;
namespace DW_405239_CS_CON
{
class Program
{
static void Main(string[] args)
{
OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder()
{
//fill connection stuff here
};
using(OleDbConnection conn = new OleDbConnection(csb.ToString()))
{
conn.Open();
string strSQL = "/*some select command*/";
// Do select command (passing connection to the command)
using(OleDbDataReader rdr = (new OleDbCommand(strSQL, conn)).ExecuteReader())
{
while (rdr.Read())
{
// pass variables from rdr to variables
}
rdr.Close();
}
// Do update command (passing same connection to the command)
// Do insert command (passing same connection to the command)
//
conn.Close();
}
}
}
} Also, if you ever want to use transactions, you'll need the connection to be maintained outside of any commands.
Thanks thines01 i will try it and get back to you....
But on line 20 thows an error
ExecuteReader requires an open and available Connection. The connection's current state is closed.
Even though this is still not exactly the way I would do it, try this:
private static bool Load(ref string strError)
{
bool blnRetVal = true;
try
{
string strSel = "select * from TestMark where que_id= " + Question + " and test_no='" + GlobalMember.TestNumber + "'";
using (OleDbConnection conn = Connect.AccConn())//assuming AccConn is closed
{
conn.Open();
using (OleDbDataReader rdr = (new OleDbCommand(strSel, conn)).ExecuteReader())
{
if (rdr.Read()) //assuming ONE read
{
TTTaken = rdr["TimeTakenSec"].ToString();
label11.Text = TTTaken;
if (rdb1.Checked == true) { UpdateTestAnswersDetail("Option1"); foundQue = true; }
else if (rdb2.Checked == true) { UpdateTestAnswersDetail("Option2"); foundQue = true; }
else if (rdb3.Checked == true) { UpdateTestAnswersDetail("Option3"); foundQue = true; }
else if (rdb4.Checked == true) { UpdateTestAnswersDetail("Option4"); foundQue = true; }
else { UpdateTestAnswersDetail(); foundQue = true;}
}
else
{
foundQue = false;
TTTaken = "0";
label11.Text = "0";
}
//
rdr.Close();
}
//
conn.Close();
}
}
catch (Exception exc)
{
blnRetVal = false;
strError = exc.Message;
}
return blnRetVal;
} Then you could call that like:
string strError = "";
if (!Load(ref strError))
{
System.Diagnostics.Debug.WriteLine("Could not load data: " + strError);
return;
} Hey thines01,
Thank you very much........... Gr8 help.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.