Simple database connection in ASP.NET? - asp.net

I would like to make a very simple page that reads and displays data from a (SQL Server) database.
I've been messing with ASP.NET MVC, but for what I want (a quick solution in a single, simple webpage) this seems like overkill. Razor WebPages seems a better solution.
The database object from WebMatrix.Data seems good, but I cannot get this to work in Visual Studio Express 2013. I keep on getting "The type or namespace name 'WebMatrix' cound not be found", even though I added WebMatrix.Data to the References of the project.
I've also tried using Entity Framework (even though that also seems like overkill), but that comes with other cryptic errors like "A namespace cannot directly contain memebers such as fields or methods" (with the error message leading to a source file that only contains "ErrorGeneratingOutput").
I really can't understand why this needs to be so difficult? I just want to quickly output some records from a database onto a webpage, nothing fancy.

I found the solution; I had to install the "Microsoft ASP.NET Web Pages Web Data" package using nuget.
I have not found any documentation on this; the StackOverflow answer at https://stackoverflow.com/a/11870874/72809 was the only one that pointed me in the right direction. It's still very unclear to me when one has to use "Add Reference" and when one has to use nuget, but I'm happy this problem is solved for now...

Related

Unable to load the specified metadata resource still comes

Yes, I know, this is the n'th question around this issue. I tried a lot of mentioned ~solutions, but none of them worked for me.
Windows 8.1, Visual Studio 2012, .NET 4.5.51641, Entity Framework 6.1.1
My struggle is to configure a well structured ASP.NET project, separate the different entities. I need more entities and in separate folders because of some overlaps among generated class names.
Therefore I've created a DAL folder and the first one, the Production. My model is quite simple, during the mode creation I've created the ProductionEntities entity connection string as it seems bellow:
Here is the ProductionModel.edmx and its properties:
I checked the assembly with reflector, all the resources are correct:
After that I've created a webpage and an EntityDataSource within it. If I select the ProductionEntities for Named Connection, I get above mentioned error message.
Have anybody some fresh idea for this? Thanks in advance!
I've got the solution!
After creating the entity model, it is MANDATORY to compile the project! After compilation the VS is going to recognize the entitydatasource wizard works fine...

MVC conversion project, fully qualified classes are not available unless 'Global' is added or System... is removed

My question is similar to here: ASP.NET System.Anything is not defined
I am in the process of converting a website project to an MVC 4 project. To do this, I created a new MVC project and imported all of my content from the previous website. Both projects are in VB, and I'm using Visual Studio 2010 SP1, with both the MVC 4 update and TFS 2012 update applied.
I now have errors popping up when I build the MVC project. Things like "Type 'System.Web.UI.Webcontrol' is not defined." When I hover over the error, two of the prompts I get to fix it are
1) Change 'System.Web.UI.WebControl' to 'Global.System.Web.UI.WebControl'
2) Change 'System.Web.UI.WebControl' to 'WebControl'
Both of these seem to fix it, but does anyone know why I can't use "System.Web.UI.WebControl" to refer to this class? I'd rather not change all of my code... there are 100s of thousands lines in there.
Update: Outside of the System and System.Web.UI namespaces, I can also drop the 'System.'. So 'System.Drawing.Color' would become 'Drawing.Color'.
There exists another namespace containing System in your solution somewhere (e.g. Abc.System.def) - probably in a referenced library. After the conversion, the project file probably imported the prefix to System in said namespace (e.g. Abc), so when you type in System, it resolves to Abc.System.
I posted this question before I had whittled down as much of the compile errors as possible.
It appears that after I had removed all the System. references from the previous App_Code classes, the real errors started appearing. There were all sorts of messages about aspx controls not existing.
Ultimately, it seemed that I needed to right-click on the .aspx pages from my website project and choose 'Convert to Web Application.' This generated a .aspx.designer.vb file with the asp.net control declarations in yet another partial class.
After doing that for all pages, I am now able to use System. throughout the app.
To others who've asked, this System. error was only affecting the non-page code. .aspx, .aspx.vb, .ascx, .ascx.vb, .ashx, etc., files were not affected.
This does make some sense, anyhow, as I had previously pulled in all the App_Code libraries prior to pulling in the pages, and the site compiled. After importing the pages and their code behinds, the order of the errors was just odd. But I stuck with it and found the underlying cause.
Thanks for the willingness to help. If you have any other questions, I'd be glad to respond.

rdsserver.datafactory converttostring failure

I have a classic ASP application written many years ago that I'm trying to fix.
The following lines recently stopped working after w7 sp1 was applied.
set address = Server.CreateObject("ADOR.Recordset")
count = lo_connection.GetRecordset(sql,address,false, error)
Set RDF2 = Server.CreateObject("RDSServer.DataFactory")
Set teststring = RDF2.ConvertToString(address)
It fails on the last line there saying "Microsoft VBScript runtime error: Class doesn't support Automation"
A bit of searching found several articles like this one
http://blogs.technet.com/b/asiasupp/archive/2011/03/14/changes-in-mdac-adodb-com-components-in-windows-7-service-pack-1.aspx
that described the problem exactly. Where I am failing is that none of the suggested fixes seems to fix the problem. Any suggestions?
The solutions described in the link are not relevant here, since ASP is not early-bound - it uses OLE Automation. It might be worth re-registering the component which creates RDSServer.DataFactory objects (which is C:\Program Files\Common Files\system\msadc\msadcf.dll on my machines).
I looked up this object, and I found this Microsoft web page:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681447(v=vs.85).aspx
"This feature will be removed in a future version of Windows. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Applications that use RDS should migrate to WCF Data Service."
Doesn't sound too good.

How to find underlying source of this error? Designer/FormView/ObjectDataSource

In the designer I get this error: "Error Creating Control: Object Reference Not Set to an instance of an object". The page itself runs fine. One of the objects, possibly an objectdatasource has to be failing at design time. But how to find which one?
The project was originally designed with a XSD for the data pointing to SQL Server queries. All based on typical Microsoft demonstrations which don't really take the product lifecycle into account.
I have had to refactor a number of things in the database, and the XSD designer doesn't automatically show you what you need to fix, you just have to run and get a runtime error. Obviously this means that some of the many individual adaptor methods are probably still incorrect, and I'm going through and updating them to match the new database model, and then tracking through the effects through all the bound controls and everything. And eventually, I may get away from a lot of this wizard-generated code and have something a bit more able to be statically checked by the compiler during the build.
But for now, how do I find out which object reference is the problem?
Problem appears to be due to a null Context in code in a base class which worked fine in VS 2008 which was being called in design-time

Using nAnt to build projects containing EDMX

I've been working on training myself in the ways of using nAnt over the past few days, and have stumbled across an issue. During the development process, I've been using the new tools, like Entity Data Model, for database access.
When you go to try to build a library/executable that contains the Edmx product, you cannot embed the required files from visual studio. Now, I realize that I can do an exec task inside of nAnt and call msbuild for the particular project file, but I am trying to keep this to be completely nAnt build for now, so I'm in a heavy struggle to get things to work.
I did some searching to find a way ot handle this, and came across this Inline C# class that is supposed to do the trick. My problem is that I do not see how you call this in the target stack in order to get it to do its job. Can anyone shed some light on this? It would be of some great help.
Ok... so I'm a bit further along with this. I have since found that the code the gentleman has posted needs to be under the task for which it is meant to be run for. I'm even getting the *.ssdl, *.csdl, and *.msl files rendering into the directory... cool beans.
nwo i'm getting something interesting coming through... I've got reference via a "references" tag to System.Data.Entity, but I keep getting the following compile error:
error CS0234: The type or namespace name 'Objects' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)
Any suggestions?
OK!!!
Got it! I had to edit the NAnt.exe.config file and add the System.Data.Entity.dll file into the Framework element for the .net framework 3.5!!!
I may wind up building a 3.5 SP1 entry, and if someone can give me a good area to post it, I'd be good to go.

Resources