954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unspecified error

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

bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

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?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Does it ONLY give the error when it's getting the data from the database?

bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

Can you show a specific line of code?
Are you sure the database is open before you access it?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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();
bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

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

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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();
bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

Is there a chance you're attempting to open it twice in some circumstances?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

No. Actually i tried to close connection before opening but still sometimes it throws an error.

bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

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.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Thanks thines01 i will try it and get back to you....

bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

But on line 20 thows an error

ExecuteReader requires an open and available Connection. The connection's current state is closed.

bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

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;
         }
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Hey thines01,
Thank you very much........... Gr8 help.

bhagawatshinde
Posting Whiz
315 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You