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
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Can you show a specific line of code?
Are you sure the database is open before you access it?
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
...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
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Is there a chance you're attempting to open it twice in some circumstances?
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
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
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
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
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402