7

I am getting this error when I try to launch my website using WebMatrix. I have a .cs file which makes a call var db = Database.Open("dbase");.

I have a database in my project called "dbase". I have no web.config file and no imports for using WebMatrix packages since I am launching the site using WebMatrix, so I don't believe I should need them. Do I need to wrap the code in Razor tags, like @{var db = Database.Open("dbase"); } ? This causes an error for me too.

What could be causing this? Does anyone have any solutions for this?

Simon Kiely
  • 5,880
  • 28
  • 94
  • 180

4 Answers4

7

I ran into this issue when I was going through the w3schools stuff on ASP.NET.

Basically, the above answers are correct: you need the assembly (DLL) WebMatrix.Data, but the commenters don't tell you how to fix the problem. Here's how:

First, copy the file WebMatrix.Data.dll into your site's /bin folder.

If you're not sure where to get it, you can have WebMatrix create a new project using a database-enabled template -- say, Bakery -- and get it out of that project's bin folder. Or you can search your hard drive for the file. I have a copy in C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies.

Then, in your ASP.NET page, import the assembly.

This is kind of a bad idea for a site you're going to have to maintain for a long time, but for the purposes of this demo, you just need to add @using WebMatrix.Data; to the top of your products page. It should end up looking something like this:

@using WebMatrix.Data;
@{
var db = Database.Open("SmallBakery"); 
var selectQueryString = "SELECT * FROM Product ORDER BY Name"; 
}

Now it should recognize the symbol "Database", and all will be well.

Matthew Lowe
  • 1,350
  • 1
  • 17
  • 29
  • In my case I already had the WebMatrix.Data.dll file in my sites bin folder. This is probably because of an update done to WebMatrix. Either way, it's nice to know where it is supposed to be located. – VoidKing May 29 '13 at 18:34
7

You just need to get this "Microsoft.AspNet.WebPages.WebData" from the NuGet Gallery.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Daniel Magana
  • 71
  • 1
  • 1
6

You need a reference to the WebMatrix.Data.dll assembly (which you probably have) and you also need a using directive for the WebMatrix.Data namespace:

using WebMatrix.Data;

That will import the Database class so you can use it without fully-qualifying the name.

It's not clear why you think you wouldn't need any "imports" (by which I assume you mean using directives like the one above) but if this is in a plain C# file, then you certainly do need them (or you need to fully-qualify type names, which is ugly).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you very much for the response! I did not realise the distinction between .cs files and the rest. I have now added the using directive(apologies for the incorrect term, I am a Java programmer trying to learn)- I am now getting the error 'The type or namespace name 'WebMatrix' could not be found (are you missing a using directive or an assembly reference?)'. I assume I need to reference the WebMatrix.Data.Dll? How can I do this? – Simon Kiely Mar 03 '12 at 15:26
  • @SimonKiely: I haven't used WebMatrix myself, but I'd have *thought* it would already be there. If there's a "Project references" part in your project, look for "Add reference". – Jon Skeet Mar 03 '12 at 15:30
  • Thank you for the response. I cannot find a reference like this. I don't believe I should need this; It should be there, and the code works in a .cshtml file. I am very confused. – Simon Kiely Mar 03 '12 at 16:15
  • @SimonKiely: You definitely *should* need a reference (and a using directive) but I'm surprised it isn't provided for you. Have you found any sign of a list of references anywhere else? (mscorlib, System.Data, System, System.Core etc) – Jon Skeet Mar 03 '12 at 16:29
  • Create a folder called App_Code and put your .cs file in there. Then try again. – Mike Brind Mar 03 '12 at 17:18
  • hello I am having the same problem did any find how reference WebMatrix.Data.Dll? – Aya Abdelsalam Jun 22 '12 at 07:41
0

In my case I had the nugget package installed but it was not finding the WebMatrix.Data. The problem was that I created a new project, instead I just created a website (file/new/WEBSITE), then the Database is found by default (I guess it's because of the type of project I created the first time)

Now it is working fine, hopefully this will help someone.

DHLopez
  • 385
  • 5
  • 17