SQL Server table.Script() method throwing strange exception - c#

I have a server with a database on it that I can access through SSMS and I am an owner of. Meanwhile in C#, I create an instance of Microsoft.SqlServer.Management.Smo.Table which inherits from Microsoft.SqlServer.Management.Smo.TableViewTypeBase.
TableViewTypeBase has a method called Script(), and as you can see from the link there, the documentation isn't exactly illuminating.
Here's what I'm trying to do: I connect to the server successfully and to the database successfully. I can get a list (or something like a list, a collection maybe) of tables in the database via database.Tables. Then I grab a specific table from those Tables and I want to call that Script() method on it.
As you might already guess, this isn't code I have written but I'm here now trying to get it to work. For a lot of the tables I've done this with, there are no issues. However, with one of them, I'm getting this error:
Script failed for Table X
Googling for this leads to a lot of answers about "publications" and "subscriptions" that I don't think apply to my situation, but I'm quickly getting out of my depth. This link from MSDN says I need to update my "database compatibility level"?
Soooo of course I head on back to Google and find this link from the Microsoft Docs, and all is going well until I get to Step 5 and wouldn't you know it—that dropdown is grayed right out. No number is shown there. It looks like this:
And I'm pretty sure it's not a permissions issue because I'm an owner of that database!
So, wise ones: what do you recommend? There just aren't that many answers out there for this particular error and the documentation is basically nothing. I'm running out of ideas.

Related

Entity framework: unexpected cursor error

I am using Entity Framework 6 and SQL Server. I am writing some code in a WCF service for saving data when receives a POST http request.
I have done some methods and they work well as expected. But now I'm writing another method.. in debug mode it seems like other, but when I call the saveChanges method I get this:
{"A cursor with the name 'FldCursor1' does not exist.\r\nThe statement has been terminated."}
But I don't even know what is a cursor... of course I have searched in the web, but I don't know why there is this message. Further, the name FldCursor1 isn't written in my code, I've searched it.
Do you know why this can happen?
I don't have used cursors anywhere... why this error?
If you need I can post some of my code, you have only to task. So, please let me know if you need code or if you have useful ideas for this problem.
UPDATE: when I get the error, I'm trying to add some new records in a table.

Determine which fields on a page are required?

I'm working as a UI tester at a small software company. In order to make my life easier, I'm trying to write a scraper in Python that will automatically generate some of the standard tests run on every page. Testing is done in use Quicktest Pro and needs to be written in VBScript. Every page that creates data needs to have a full case, where every field on the page gets filled out, and number of reduced cases, where only required fields get filled out.
The full case should be easy -- I plan to set up a requests.Session object with an already-authenticated cookie, send a GET request to the appropriate page, and parse the the response with BeautifulSoup.
The reduced cases I'm less sure of how to approach. I can think of three ways to go about it, but none of them sound great:
A) Try to submit a blank page. Check the response for error messages of the form "* <field> is a required field." Look for the fields whose names are closest to the one specified. Fill them out. Try to submit again, and repeat, adding fields until it goes through successfully, and return a list of fields.
This isn't great because it's difficult to identify what field the error message corresponds to. A message stating that "* Birth date is required" might actually be referring to a form element with an HTML ID of "dob_entry1." I'm also testing on a development copy of the source, so it's not unusual for partially filled out forms to cause a server error, and I'd probably need to manually clean up any data that this approach creates.
B) Send in a fully filled-out form. Find the database record(s) that just got created, and find out which columns are NOT NULL. Match column names to field names, and return the resulting list.
This seems more promising, but I'm not sure how to go about finding the records that were created. Logs (except for errors) are not turned on for the MySQL server, and the server has ~15 databases on it, all of which are being worked on by developers, so I can't mess with the server's global variables to turn it on. I could query the database for all of the values that I just passed in, but there's a pretty huge amount of data already on the db, so it's unlikely that I would be able, for example, to figure out which date of birth is the one that I just submitted.
Googling, tools like this http://hackmysql.com/mysqlsniffer might be an option, but I'm wary of doing anything to the server as a whole since the developers will be using other dbs on the server at the same time. I don't have much experience with SQL so I'm not very sure how to go about doing this.
C) Somehow parse the C# source code to find the query that corresponds to a given page. Find out which columns it affects, query the database to find out which are NOT NULL, match the column names to field names and return a list.
I have no experience with C# so I don't know how feasible this is, but if it were PHP I think it would be pretty simple. I could find the source for the site if I poked around but I haven't looked at any of it yet. The website is ~10 years old and is pretty massive, so matching page names to source files is probably non-trivial.
I imagined that finding out which fields of a form are required to submit a page would be a pretty common task for scrapers, but Google hasn't turned up much. Are any of these approaches reasonable? Is there an easy solution that I'm missing out on?
I think your first choice - figuring out from the HTML response which fields are required - is your safest bet. Trying to match field names to database column names can be a real problem - you have no idea how many layers the data goes through until being saved in the database - field names make look nothing like the column names.
Seeing if a field is required shouldn't be too hard - start with a full form and submit it to see that it's legal. Then send the form again, without the first field. If you're getting an error - the field is required. Fill the first field again, clear the second one and try again. Do this for every field in the form.
The web application will need to be stable enough for this to work. You should be able to tell the difference between a missing field error and a server error.
Oh, and do check #Ming Slogar's comment - if the HTML guys marked the fields as required in the HTML, you'll have a lot of free time on your hands.

Returning Good Errors from UpdateDataSet

EDIT: Solution (kind of)
So, what I did had very little in common with what I originally wanted to do, but my application now works much faster (DataSets that took upward of 15 minutes to process now go through in 30-40 seconds tops). Here's roughly what I did:
- Read spreadsheet & populate DataTable/DataSet normally
- [HACK WARNING] Instead of using UpdateDataSet, I generate my own SQL queries, mostly by having a skeleton string for each type of update (e.g. String skeleton = "UPDATE ... SET ... WHERE ..."). I then consult the template database and replace the placeholder ... with the appropriate entries.
- [MORE HACK WARNING] The way I dealt with errors was by manually checking whether those errors will occur. So if I know I am about to do an insert, I'll run an error-checking command before the actual insert; what the error checker will do is construct a JOIN statement, checking whether any of the entries in the user's DataSet already exist in the database. Just by executing the JOIN command, I get back a DataSet with the results, so I know that if there is anything there, it's the errors. Then I can proceed to print them.
If anyone needs more details, I'll be happy to provide them. It's a fairly specific question, so I should probably keep this outline fairly high level.
Original Question
For (good) reasons outside of my control, I need to use the Database.UpdateDataSet() method from Microsoft's Enterprise Library. The way my project will work, I am letting the user make changes to the database (multiple database, multiple schemas, multiple tables, but always only one at a time) by uploading Excel spreadsheets to a web application. The spreadsheets follow a design/template specified by me (usually). I am a state where I read the spreadsheet, turn it into a DataTable/DataSet, and use (dynamically generated) prepared statements to make the appropriate changes to the database. Here's the problem:
Each spreadsheet only allows for one type of change (insert/update/delete). I want to make it so if the user uploads an insert spreadsheet, but several (let's say 10) of the entries are already in the database, I not only return with an error, but also tell them which entries (DataRows) violated the primary key constraint.
The idea solution would be get a DataSet with the list of errors back, but I don't see how I can do that. Perhaps there is a way to construct the prepared statements in such a way that if a DataRow is to be inserted (following the example from above), it proceeds normally; however if it attempts to update or delete, it skips it and adds it to an error collection of some sort?
Note that I am trying to avoid using stored procedures. Since the number of different templates will grow extremely quickly after deployment, it is important that I stay away from manually written code and close to database-driven model as much as possible.

"Invalid attempt to read when no data is present" - why?

I'm getting the error mentioned in my subject line and I don't know why. I have an ADO.net entity and a domain service class and am doing this as I have read in tutorials and what not. Also you should bear in mind that it worked fine up until today, and so far as I know I didn't make any changes.
The database exists and the table is there as it always was. I suppose it's possible someone could have changed the table, but there are still entries (and they seem to be the same as before, but I couldn't guarantee that).
I am still connected to that source and it's not changed IP (I can remote desktop onto the server to see the SQL Server Management Studio).
If you want to ask what changes I made... not really any. I added a drop down box on my page which tells the user when database access is happening and after it finished, but then removing that still left me with this same problem so I rather think it's unrelated. I have searched Google, but none of the solutions seem to be for Silverlight.
Thanks, and I hope someone can shed some light here.

WCF Web-Service crashes after minor change to underlying database and entities

I've been bashing my head on this for quite a while and I'll really appreciate some help.
I am writing a website using ASP.NET. The Business Layer is separated into two parts: BL-Server and BL-Client. The BL-Server in actually a WCF-Service that is there to shield the Data Access Layer and the DB. The WCF-Service is connected to the DAL that in turn reads from the DB. We are using MSSQL.
Problems began after making a minor change to one of the tables of the DB (Adding a bool column to table "Responses").
Suddenly, every time i request information through the WCF-Service, i get the following exception:
"System.ServiceModel.CommunicationException was unhandled by user code
Message=The underlying connection was closed: The connection was closed unexpectedly."
This is kind of weird because the error occurs even when i request data from a different table (such as "Departments").
I've done some digging, and after setting a trace to the WCF-Service (see: http://forums.asp.net/t/1476129.aspx/1), i found out that the problem in the WCF-Service that caused it to suddenly close the connection was a buffer overflow. When trying to load the objects to the return buffer, the service throws the following exception:
"Writing an object with a recursive structure has limitations when it has large depth. Consider reducing the depth of the object."
That lead me to put a break point in the DAL method that loads the "Departments" data and examine the object that was about to be sent through the WCF-service.
Turns out the object has some loops in it. You can go through it's elements in an infinite loop, since "Departments" has some children elements that point back to him.
That seamed weird to me, so i checked the .dbml file, an walla - "Departments" has a list of entities that point to it. This, i found out, could be enabled/disabled in the link's (between the two entities, in the .dbml file) properties. The property is called "Child Property" and when enabled, ads a list of objects that have a foreign key to it to the element. (Example: if A has a FK to B. The B element will have the following member: private EntitySet<A> _A;)
At this point one might think that the solution would be to set all the "child property" for all the links between the entities to false. But that ends up with other exceptions such as:
"System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException was unhandled by user code
Message=Operation is not valid due to the current state of the object." (thrown by the entity).
Moreover, i don't want to mess with the .dbml file. I know that everything was fine before the MINOR change to the "Responses" table, and i find it hard to believe that a minor change in one table can cause so much of a mess in the entities' code.
I'm sorry for the long post, but this issue is driving me crazy, and i wanted to make sure i give all the relevant information.
Note: the application's structure is a prerequisite and i can't change it, so let's not go into that, please.
Thanks in advance,
SummerBulb
Update:
Since I'm using source control, i was able to compare the old "BusinessElements.designer.cs" and the new one. I found that the old file contains lots of code that deals with serialization. For example: [global::System.Runtime.Serialization.DataMemberAttribute(Order=1)], an attribute added to all the properties (getters and setters).
Was this code removed because of the new Boolean value? How can i get back the serialization-support code?
SOLVED!
Turns out the "Serialization mode" property was set to "none".
I set it to "Unidirectional" and now everything is fine.
If you've had the same problem and found this helpful, let me know by leaving a message.
Thanks!
Have a look at this article it might help you to find out why/when specific attributes are added (or removed)
Another thing to check is whether or not you are still using the same version of the Entity framework.

Categories

Resources