1

I am trying to create Web API following this book http://www.apress.com/9781484201107?gtmf=c. I got to the end of chapter 5 and when I run my solution I got this error message:

Cannot open database "WebApi2BookDb" requested by the login. The login failed. Login failed for user 'Domain\myusername'.

I thought that maybe I have messed up somwhere but when I downloaded source (available here Chapter 5: http://www.apress.com/downloadable/download/sample/sample_id/1577/) I got the same exact message.

The connection string is: <add name="WebApi2BookDb" providerName="System.Data.SqlClient" connectionString="Server=.;initial catalog=WebApi2BookDb;Integrated Security=True;Application Name=WebApi2Book API Website" />

I am using Visual Studio 2013. What could be solution for this problem?

Supposedly sending this request:

POST ht tp://localhost:61589/api/v1/tasks HTTP/1.1 Content-Type: text/json {"Subject":"Fix something important"}

You should get response that should look something like this:

HTTP/1.1 201 Created Content-Type: text/json; charset=utf-8 Loation: ht tp://localhost:61589/api/v1/tasks/10 {"TaskId":10,"Subject":"Fix something important","StartDate":null,"DueDate":null, "CreatedDate":"2014-05-04T02:52:39.9872623Z","CompletedDate":null, "Status":{"StatusId":1,"Name":"Not Started","Ordinal":0},"Assignees":[], "Links":[{"Rel":"self","Href":"ht tp://localhost:61589/api/v1/tasks","Method":"GET"}]}

user3677314
  • 43
  • 1
  • 7
  • can you connect with SA user? what version of SQL Server you are using? – Zam May 13 '15 at 19:13
  • I don't know how to check that in VS2013 (I am complete novice). I am using SQL Server 2012. – user3677314 May 13 '15 at 19:16
  • 1. can you run SQL Server Management Studio? 2. can you try with Provider=SQLNCLI11 ? 3. in Visual Studio, select View -> SQL Server Object Explorer – Zam May 13 '15 at 19:30
  • 1. Yes, but what sould I run in it? 2. Not sure what you mean by this. 3. Yes, but what nex? – user3677314 May 13 '15 at 19:40
  • 1. Can you login with your login informaiton ('Domain\myusername') into SQL Server? 2. Try to change provider name in your connection string 3. Can you navigate to your database? (WebApi2BookDb) – Zam May 13 '15 at 19:45
  • 1. Yes I can log in SQL Server with my domain name into SQL Server Management Studio, but I don't see WebApi2BookDb there. 2. Tried it, but it seems like nothing changed. 3. Yes, I can, but I also see WebApi2BookDb_1 through 4 (4 additional ones) – user3677314 May 13 '15 at 19:48
  • This is answer on your question -- you cannot connect, because database does not exist. – Zam May 13 '15 at 19:53
  • In your connecting string change database name to WebApi2BookDb_1 – Zam May 13 '15 at 19:54
  • In that case how do I create the database? Do I simply create it in SSMS? If I change database name to WebApi2BookDb_1 I get Server Error in '/' Application. Not sure if it is good or not... – user3677314 May 13 '15 at 20:04
  • your database already created. right now you have two different way to go 1) change database in your connecting string; or 2) rename database -- right click on database and select "rename" – Zam May 13 '15 at 20:16
  • ok, so this error: "Object reference not set to an instance of an object." actually means that I have successfully connected to the database? – user3677314 May 13 '15 at 20:22

2 Answers2

1

I've just run into this problem myself. This is what you need to do:

  1. Open the solution in visual studio containing the code from chapter 5.
  2. Make sure you can build the "WebApi2BookDb" project. If you are having issues building this project see this stack overflow post.
  3. Create a database in SSMS called: "WebApi2BookDb".
  4. Right click on the WebApi2BookDb project in visual studio and select publish.
  5. Click on Edit.
  6. Enter "." for the server name or the name of your local SQL server instance.
  7. Enter the username / password or use windows authentication.
  8. Select the "WebApi2BookDb" database.
  9. Test the connection.
  10. Click OK.
  11. Click Publish.

You should then be able to run the application without any issues.

You will then need to download fiddler. Once installed and running you will need to create a Post Request:

  1. Open Fiddler
  2. Click on the Composter tab.
  3. Change the drop down list to "Post"
  4. Enter the URL e.g. http://localhost:61589/api/v1/tasks/ (Yours might be different).
  5. Ensure Http/1.1 is selected from the drop down list.
  6. The content type should be as follows: Content-Type: text/json
  7. In the request body enter: {"Subject":"Fix something important"}
  8. Click on the "Execute" button.

You should then get a successful response and a new row should be added in your database!

Community
  • 1
  • 1
Cool Breeze
  • 1,289
  • 11
  • 34
0

The error tells you exactly what is wrong. It can't open the WebApi2BookDb database, which, given your connection string, probably doesn't exist. You need to create that database.

Becuzz
  • 6,846
  • 26
  • 39
  • Isn't the data base created when I run the solution? What in the connection string makes you think it doesn't exist? – user3677314 May 13 '15 at 19:40
  • No the database is not created by running the solution (unless you have a setup script that runs, which seems unlikely). The fact that you are using integrated security in your connection string means it isn't a permissions issue, which means that the database doesn't exist. – Becuzz May 13 '15 at 19:46
  • In this case how do I create the data base? Do I simply create the data base in SSMS and the solution will be able to connect to it? – user3677314 May 13 '15 at 20:07
  • @user3677314 Make the database in SSMS and then that should solve your current problem. However, the database won't have the necessary tables and data, etc. It appears that in the code you downloaded there is a WebApi2BookDb folder in the src folder. That contains a .sqlproj file (which is apparently for setting up the database, I'm not familiar with them). There are also a handful of scripts in there that you could just manually run. Run the ones to create the tables then the ones to add the sample data. – Becuzz May 13 '15 at 20:42