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...

Recommended Answers

All 13 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.