"table or view does not exist", but only sometimes - c#

A SQL select statement gets run when a user presses a button on my website, and I do this:
connection = new OleDbConnection();
connection.ConnectionString = [connection string];
connection.Open();
cmd = profile.Execute(mySQLStatement);
da = new OleDbDataAdapter(cmd);
table = new DataTable();
da.Fill(table);
90% of the time, this works just fine. But every once in a while, I get the OleDbException table or view does not exist on the line da.Fill(table). There doesn't seem to be a pattern of when this works and when it doesn't, though it's more likely to not work when the site isn't used for a minute or two... Like the session might be expiring. But the rest of the website (that does not require this database) works.
Any ideas of what might be happening or how to fix it?

I have not found a solution. As a temporary fix, whenever this happens, I just re-start the page.

Sometimes the content of mySqlStatement is not exactly correct, maybe?
Trap the exception dump mySqlStatement to a logfile (with date and time this happened), and then throw.

Related

AdsConnection throws EntryPointNotFoundException on second connection but works the first time

I have a piece of code that I reuse that helps me connect to an adt database and read the data.
using Advantage.Data.Provider;
...
protected DataTable FillTable(string tableName)
{
DataTable table = new DataTable();
using (var conn = new AdsConnection(connectionString))
using (var adapter = new AdsDataAdapter())
using (var cmd = new AdsCommand())
{
cmd.Connection = conn;
cmd.CommandText = "select * from " + tableName;
adapter.SelectCommand = cmd;
conn.Open();
adapter.Fill(table);
conn.Close();
}
return table;
}
This code works perfectly the first time I go through it, but gives the following exception the second time I call it with a different table name.
System.EntryPointNotFoundException: 'Unable to find an entry point named 'AdsIsConnectionAlive' in DLL 'ace32.dll'.'
I would like an explanation.
I've tried to read up on this error, but all the possible scenario's I've found don't explain why it works the first time. They mention problems with the DLL like it being the wrong version or some incompatability with the .NET version, ...
If I change the order of the calls the code still fails on the second time, so I know the problem isn't with the name of the table or the way I call my code. The problem is probably with me not closing the connection correctly. I've tried adding more braces just to make sure that that part runs correctly and I've debugged to make sure that the first conn.Close(); is executed correctly.
I could place all my code within this code and only use one connection that I keep open as long as I need it. That would bypass my problem, but I would like to avoid that and to understand what I'm doing wrong.
This is most likely caused by loading an older version of ace32.dll from a newer version of the ado.net components. The AdsIsConnectionAlive was introduced in a later version of the DLL - not sure about the exact version probably 6.0 or later.
The first time the connection was made, the ado.net component knows that the connection was not alive so there was no need to call the IsAlive entry point. The second time around, since there was already a connection made to the same connection path, it would try to reuse it by checking to see if it is still alive. I think that there is a way to disable the connection caching but do not remember the detail. A better solution would be to make sure that the advantage DLLs are matching version.

Need to when ExecuteNonQuery has finished before proceeding to next line of code

I searched and checked but did not see what I need. I am using a data adapter's UpdateCommand's ExecuteNonQuery to update a local Access database. When I run the code, the ExecuteNonQuery does not appear to finish before the next line of code is executed and therefore, the updated database is not reflected in my grid or my text boxes. If I put a messagbox right after the ExecuteNonQuery, then when I hit ok in the messagebox, the rest of the code is executed and the updated database is reflected in my grid and text boxes. Or if I step through the code in debug without the messagebox to hault execution, the update to the database is reflected on my form. But if I just run the code without something after the query to slow it down or stop it, the update is not shown in my controls.
OleDbConnection connection = new OleDbConnection(connString);
string sql = "Update Table1 set Country = '" + txtNewText.Text + "' where SomeNum =
1111";
connection.Open();
dAdapter.UpdateCommand = connection.CreateCommand();
dAdapter.UpdateCommand.CommandText = sql;
dAdapter.UpdateCommand.ExecuteNonQuery();
//MessageBox.Show("Pause");
dTable.Clear(); //Clear or it will add everything to what was there.
dAdapter.Fill(dTable);
row = dTable.Rows[0];
txtCompany.Text = row["Name"].ToString();
txtGenre.Text = row["Job"].ToString();
txtId.Text = row["Id"].ToString();
txtSomeNum.Text = row["SomeNum"].ToString();
txtCountry.Text = row["Country"].ToString();
I'm assuming that the update query is not finishing before I re-fill the data table. Can someone please help me with this. I looked for something to show the status of the update query but did not see what I need. What am I missing here???
Thank you!
VH
10/28/14 Last night I added dAdapter.UpdateCommand.ExecuteNonQuery() right after dAdapter.UpdateCommand.ExecuteNonQuery(); then everything worked. I was not sure if this was just something that took enough time to allow the query to finish and therefore causing the update to display, or if it was just some fluke "band-aide" that worked. I then replaced that with connection.Close(); and that worked as well. Again...I don't know if closing the connection caused the query to finish and the connection to close before moving to the next line of code, or if this was also just "fudging" it to work. I have seen other people having similar problems in my searching for answers but I have not seen any one explain what the problem is and why this happens or the most appropriate solution.
You should use the OleDbDataReader, since you aren't using SQL.
Provides a way of reading a forward-only stream of data rows from a data source.
An example:
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0].ToString());
}
reader.Close();
}
This will iterate through each specified column allowing you to fill your data. Documentation can be found here.

INSERT INTO command not working

Before I start, I'll let you know that I tried everything that has already been suggested on previous questions and other websites before I considered posting a question myself. As it happens, nothing seems to work and I'm just about fed up with this.
As some background information, this is for my Computing A2 project, so I'm kind of stuck for time now - i.e. I can't be changing loads of my code ideally.
Anyway, onto the issue...
I'm using SQLCe in my code to read from various tables and write to one. So far, the code for reading from the tables works fine, so that's any connection issues out the way first. The piece of code I am struggling with is as follows:
string connectionString = Properties.Settings.Default.BookingSystemDatabaseConnectionString;
using (SqlCeConnection myConnection = new SqlCeConnection(connectionString))
{
myConnection.Open();
try
{
string commandStr = "INSERT INTO bookings(username, room, time) VALUES(#username, #room, #time)";
SqlCeCommand myCommand = new SqlCeCommand(commandStr);
//Passes parameters into SQL command.
myCommand.Parameters.AddWithValue("username", StaticUser.StudentUser.username);
myCommand.Parameters.AddWithValue("room", roomBox.Text);
myCommand.Parameters.AddWithValue("time", timeBox.Text);
//Executes SQL command. Returns the number of affected rows (unecessary for my purposes; a bi-product if you will).
myCommand.ExecuteNonQuery();
}
catch
{
System.Windows.Forms.MessageBox.Show("Could not write new booking to database. This is likely because the database cannot be reached.", "Error");
Program.AccessError = true;
}
myConnection.Close();
}
This is just one of the many ways I have tried to combat the issue I am having. I have also explored:
myCommand.Parameters.Add(new SqlCeParameter("username", StaticUser.StudentUser.username));
to pass the parameters...and another method which escapes me now (using ".Value = StaticUser.StudentUser.username" I think). Furthermore, I have tried using a 'using' statement for the command to save me closing the connection myself (I will probably end up using a solution that uses 'using'). Finally (albeit this isn't a chronological recollection), I tried:
SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO bookings(username, room, time) VALUES(#username, #room, #time)", myConnection)
Again, of course, to no avail.
To highlight the actual symptoms of the issue I am having: The code appears to run fine; stepping through the full method I have pasted above shows that no error is being caught (of course, the message box does not appear - I realised afterwards that stepping through was arguably an unnecessary procedure) and in the other methods I have touched on, the same thing happens. The issue, then, is that the table 'bookings' is not actually being updated.
So, my question, why?
I didn't do the obvious and check the Debug folder for an updated database.
Look for a copy of the database file in your bin/debug folder.
Use full path in connection string, and preferably do not include the sdf file in your project (or at least set build action to None)
i think you are not defining a connection for the command
try
mycommand.connection = connectiostring;

Very long request time VS2010

I have at small web-application that get some data from a SQLdb in Visual Studio 2010. When i try to display this, using a simple dropdownlist it takes around 15 sec the first time just after compilation and then after that 5 sec every request. I use a LINQ connection to db and a repeater to print everything out. It´s four columns and around 50 rows, so not so mutch data. It doesn´t matter if i try take 10 rows, same response time.
We have tested the same thing on another computer with VS2008 installed and the time was like 0.1 sec or something like that. I use .NET 3.5 and have installed all the latest updates (SP1 and so).
If i look at the trace i see it takes time at these rows in my code:
var cust = dbContext.Customers
.Select(c => new
{
c.customerID,
c.Email
}).Take(10);
repeater.DataSource = cust;
repeater.DataBind();
Anybody know what could be taking som mutch time?
Regards
Daniel
I have tried this other aproach:
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = CommandType.Text; //default
cmd.CommandText = "SELECT Email FROM Customer";
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
It takes just as long.. there must be some other more fundamental problem than the code to call the db i think?
Some more information:
I did som trace, this is first time page is loaded:
Begin Raise ChangedEvents 0,00219604937555932 0,000014
After repeater has printed 15,8138335259613 15,811637
End Raise ChangedEvents 15,8138801733289 0,000047
Second time:
Begin Raise ChangedEvents 0,00219604937555932 0,000014
After repeater has printed 5,25090396258066 5,248825
End Raise ChangedEvents 25095106283536 0,000047
What´s happening at ChangeEvents?
Get the actual SQL being generated by LINQ, copy-paste it to your database management tool and look at the execution plan to identify potential performance issues.
You can see the actual SQL by using Log property of the data context. In a console application, you can do this:
dbContext.Log = Console.Out;
// ...
Or, write the SQL to file like this:
using (var writer = new StreamWriter("sql.log")) {
dbContext.Log = writer;
// ....
}
Also, it might be worth adding ToList() or ToArray() at the end of your LINQ expression, just to make sure it is not unintentionally re-executed.
I have no idee what i did to make it work.. just kept on doing stuff at other places in the code and updated latest windows update.. hmm.. now it work like a charm, must be a bug in VS2010??
Thanks for all suggestions though, learned some great stuff!
Regards

Simple WinForm with gridview app not responding

I got a simple winform app, basically it just loads data from the DB into a gridview and displays it, in order to do it i'm using a DataAdapter, I have around 7000 rows to show, in my local computer (Win7) it works fine, in the server howerver (2008 server) it loads the data, shows at least the first screen (although it doesn't show the scrollbar on the right to scroll down) and then it goes to Not Responding.
Trying to find the problem, I made sure the .net framework 4.0 is installed
string cs = ConfigurationManager.ConnectionStrings[csName].ConnectionString;
try
{
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter adapter = new SqlDataAdapter("sp_getAllDocuments", con);
adapter.SelectCommand.CommandTimeout = 600;
dt = new DataTable();
adapter.Fill(dt);
}
catch (Exception ex)
{
toolStripStatusLabel1.Text = ex.ToString();
}
I even tried printing any possible Exception to a label there, but since it goes to not responding nothing else happens.
When I cut down the results from 7000 to 4 then it works, any ideas?, the sever is a good computer.
Change the Timeout to 10 secs and try
adapter.SelectCommand.CommandTimeout = 10000;
May be the time out is the problem
Maybe it just takes longer in production environment... could that be it?
Have you considered using a DataReader instead of the DataAdapter? I think the DataReader will be better in your case
http://msdn.microsoft.com/en-us/library/ms254931.aspx

Categories

Resources