I've been developing an app which uses strongly typed datasets and stored procedures. I've just graduated and this was the method that was sold to us as the way to go. I'm starting to have severe doubts.
My client has advised me that he might change from SQL Server to MySQL. From what I've read it might be better to not use stored procedures as migrating could become more difficult. So anyhow I've just implemented a new table adapter query using the wizard and selected Use SQL Statements rather than Create new stored procedure.
My call to the query
Intranet.administratorsDataTable dt = taAdministrators.GetAdministrators();
now generates this error:
executereader requires an open and available connection. the
connection's current state is closed
I have no idea why this auto generated code doesn't have a connection and I'm hungover and in no shape to deal with this. I decided to just go back to the SP's for the moment so I can get some work done. This error is still being thrown (same table adapter, same method name but reconfigured to use a SP). All of my other DB calls work fine.
I'm assuming the generated SQL code is still floating around somewhere even though I changed the adapter to use SP's. Can someone tell me where it is so I can delete it?
On another note I'm really starting to think that using SqlConnection and SqlCommand manually is a much better option, as using these query 'Tools' are just way to much trouble when it comes to flexibility such as modifying database tables etc. Can any of you more experienced people tell me if that's correct or do you advocate using table adapters?
*Edit
it also throws these:
Invalid operation. The connection is closed.
and
There is already an open DataReader associated with this Command which must be closed first.
The solution was to go to the query properties in the tableAdapter and manually change the "Command Type" to StoredProcedure.
highlight the query > go to properties window > change the command type
Seem this didn't (or doesn't) get auto updated when I reconfigure the query.
if you provide some code, it would be better.
I think, you need to open the the connection.
SqlCommand Cmd= new SqlCommand();
Cmd.Open();
// then u can use Cmd.ExecuteReader();
Related
we have an architectual problem with our data grid. The grid supports searching, paging and sorting using a linq2entity query, that contains all of the above parameters.
At some pages, the grid should not display the content from the database (e.g. column 'name'), but the translated ressource, loaded by the Resource Provider.
Our Resource Provider gets the translations from the database and caches them to the application cache to avoid unneccessary trips to the database.
At this point, we have the following possibilities:
No searching, sorting and paging on the databse, so loading all rows, then load the translations from the Resource Provider, then do the searching, sorting and paging at the application.
Bad performance, because the database is very big
Searching, sorting and paging on the database, then load the ressources for the results.The Displayed Values will not match to the search and sort configuration
Get the Resources directly from the database, within the linq query.
The Ressource Provider Caching cannot be used. The Join with the Resource Provider Texts will be very bad and slow
Every possibility is very bad, but I can't think of another solution. Any good suggestions? How are these problems solved in other software?
You can do it by bring the associated filtered records from the database and keep it in ViewState.
How can you improve the Performance?
You can make use of JSON / Page Methods for Database Callings. I will explain it to you with the help
of an example.
Click here to check the code for GridView Bindings using JSON
Mark Up With JSON
I am calling the Code Behind methof from Client side
Code Behind
Output
Click here to check the output in case of Update Panels
Is application is holding too much memory then you need a Doctor like Red Gate Ants Memory Profiler. Click here to see more details about it
Make sure to use the Using Statements to avoid Memory Out of Exceptions
using (SqlConnection connection = new SqlConnection())
{
connection.Open();
//Also for SqlCommand... Sample code...
using (SqlCommand cmd = new SqlCommand())
{
}
using (SqlTransaction transaction = connection.BeginTransaction())
{
transaction.Commit();
}
}
Are you aware about the Teleric Grid? It loads all the records first from the database and keeps in Cache. In order to go for this you must use Paging and Disposing the objects is mandatory. Teleric Grid shows these records in Paging. Thus you can avoid the rendering issue to get rid of displaying all records in once.
i don't understand your question properly but if you want to all operations on translated resource you have to store translated resource in database then get them in to session object and apply filter on it if it is either in form of table or list then give source to grid which has to configured as work with translated resource which is filtered from session object.
heys guys,
i have a website, which contains lots of db work to display data on page, so i have created a VB class which is public, under App_Code.
Now i have all the methods and functions under that class are Shared(Static), also i have a connection variable which is also static.
Client complains, that sometime there appears an error on the page, one of those error is Field Name does not belong to table Table, i dont understand, about this, as this is very rare, if there is no field with name, then this should occur everytime, one of my colleague says that there should not be Shared methods or functions... is this correct..
There is no "security" problem with a static method. Your colleague is confused. Whether or not the code you wrote should be static or instance methods depends on what exactly it does. But having them as static methods is not "dangerous."
I suggest you track down the query that is causing the problem because the method being static is certainly not the issue.
As far as your connection goes, I would not recommend keeping it as a static variable. I assume this is a SqlConnection, or something similar. In that case, if you keep it as a static variable, it is possible for the following to occur:
Your connection is never closed, even after you're done using it.
You will have issues if you have multiple queries trying to use the connection at the same time.
So I recommend you use the following pattern to ensure your connections are only kept open as long as they are in use.
public void DoSomething()
{
//Doing some work that doesn't need a connection.
//Now ready to submit or fetch data from the database.
using (SqlConnection connection = new SqlConnection(...))
{
using (SqlCommand command = new SqlCommand(..., connection))
{
//Now, working with the connection and command.
}
}
//Done with the connection, doing more work now.
}
The using statement works with anything that is IDisposable. Your connection variable here will be automatically closed and destroyed at the closing bracket of the using statement. I recommend you use it for anything that you can. Streams, SqlConnections, Fonts, etc.
It sounds to me like you have a infrequently-used SQL statement that refers to a column that does not exist on a table.
For example - suppose you had SQL like so
SELECT Col4 FROM Table2
and Col4 was not a member of Table2. You would get the error you describe.
If you're building SQL dynamically (which is dodgey) you might run into this.
But I don't think it has anything to do with your method 'security.'
I am working on a content management system which is being sort of retrofitted onto an existing database, and the database has many many tables. There will be a staging database, where we will make changes and allow users to 'preview in place'. Then any changes have to be approved, and to publish them we will connect to a live version of the same database (same schema) and play-forward the captured changes.
I have found some code (called Doddle Audit) which, with some customization, is giving me great information about what is changing. I am able to get a list of all columns, before and after, for updates, inserts, and deletes. But what I would really like to have is the underlying SQL being run by SubmitChanges(). LinqToSql has to generate this, so why can't I have it?
I have googled around and looked at code involving SubmitChanges, mousing over stuff, and I can't seem to find it. Does anyone know of a way to obtain this?
Use the DataContext.Log property like this:
using(DataContext dc = new DataContext()){
StringBuilder sb = new StringBuilder();
dc.Log = new StringWriter(sb);
}
You will see the generated query in debug mode.
Linq To Sql Profiler. It does that and a whole lot more.
You can try this:
Console.WriteLine(context.GetCommand(query).CommandText);
Hope this helps.
Thanks,
Raja
You could use SQL Server Profiler, not as good as Linq To SQL Profiler, but free.
To get the sql-statement in a string, you can do something like this:
using (var context = new MyDataContext())
{
var query = from p in context.Persons
select p;
string sql = context.GetCommand(query).CommandText;
}
Afternoon all
This is driving me nuts.
For no apparent reason (of course there must be one), my web project will no longer generate LINQ to SQL classes/data contexts!!!
I am going about the usual routine of right clicking the project, adding new item, selected LINQ to SQL classes, then dragging over a table from Server Explorer, saving and build but no...the bloody thing won't appear!!!
Does anyone know why this might be happening/what have I done wrong?
Figured it out.
Basically, NEVER rename any items.
I had previously been using OnlineReporting in one item, the name space when linq creates the classes was using this as it's namespace.
However, I then later changed this to Reporting but when the linq to sql generation occurred, it was still using the 'old' namespace.
I went into the designer.cs file, amended the name space to match the new format and it worked.
I am using link to sql, I have a connected set of objects.
I start out and do a linq statement like this
Dim L= from II in context.InventoryItems select II
Dim L2 = L.tolist
The second line was so that I could narrow down where the problem was occuring. When the second line is hit I get an error "The EntitySet is already loaded and the source cannot be changed"
Any ideas what might be causing this?
Omer's comment brings up a very good point: is this DataContext being re-used from a previous operation? If so, you may want to check out Dino Esposito's blog post regarding lifetime of the DataContext to make sure you're not keeping it around too long.
That error sounds like maybe you have already loaded data from the InventoryItems table using that DataContext and possibly made some changes to entities bound to the DataContext that you have not yet submitted. If you try your code with a brand new DataContext without specifying any special DataLoadOptions it should work.
For anyone interested, be careful what you do in the constructor, I was initializing some stuff in the constructor that I shouldn't have been and it caused an error on loading from the datacontext.