Adam T
Adam T

Reputation: 91

ASP.NET Database Connection Issues with Azure Database

I followed the WingtipToys tutorial on the Microsoft website, which is basically a tutorial for ASP.NET and shows off numerous features it can do, and so on.

I made my own database to use during the tutorial, and had it working flawlessly on my local machine. Then, I decided to publish it to Azure to see it working online properly, which is where my issues began.

Firstly, I made a SQL database in Azure and moved my database across. I can connect to this Azure database in Visual Studio, and can verify that all the right tables are there, each filled with data;

Database Connection in Visual Studio

Database Data

The next step was to update my connection string, which is currently;

  <connectionStrings>
    <add name="WingtipTpys" providerName="System.Data.SqlClient" connectionString="Server=tcp:xxxx.database.windows.net,1433;Database=xxxx;Integrated Security=False;User Id=xxx;Password=xxxx;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" />
  </connectionStrings>

Now, I was under the impression at this stage that the database and the website could now communicate with each other, so I published the website to see how it looks, which is where the really confusing part (for me) occurs.

When I visit the website URL that I published (http://completebusiness2017.azurewebsites.net/) it uses the right master page, and the about and contact pages are both how they should be. But, it is pulling in data from the;

ProductDatabaseInitializer

Class, which contains code such as;

 private static List<Product> GetProducts()
    {
      var products = new List<Product> {
                new Product
                {
                    ProductID = 1,
                    ProductName = "TEST",
                    Description = "This convertible car is fast! The engine is powered by a neutrino based battery (not included)." + 
                                  "Power it up and let it go!", 
                    ImagePath="",
                    UnitPrice = 39.50,
                    CategoryID = 1
               },
                new Product 
                {
                    ProductID = 2,
                    ProductName = "Silly Car",
                    Description = "There's nothing old about this toy car, except it's looks. Compatible with other old toy cars.",
                    ImagePath="",
                    UnitPrice = 15.95,
                     CategoryID = 1
               },

This class has been there all along, but on my local machine, it was never used. The data came straight from the database I created. I don't understand how now I have updated the connection string, it is pulling data from this class, and not my database. Another strange element, is that when I update this class, so for example I changed;

Convertible Car Price from 22.50 to 39.50 and Old-time Car name to Silly Car

These do not update when the website is published again. So I have no idea where or why it is using the old data.

So I guess my first question is, when I update the ProductDatabaseInitializer class, why aren't these changes reflected when the website is published again? I think I should understand why that is happening before I query why my database isn't working properly.

Any help at all would be very much appreciated.

Thank you, happy holidays.

Upvotes: 3

Views: 85

Answers (1)

Rob Reagan
Rob Reagan

Reputation: 7686

I am not 100% convinced that you're pulling data from where you think you are. Can you verify your database connection strings?

You'll need to look in a couple of places. First, in the Azure Portal, navigate to your Web App, then choose the "Application Settings" menu option. Check to see if you have anything listed under your "Connection Strings" section.

Second, log into your SCM site at the address http://completebusiness2017.scm.azurewebsites.net. Once logged in, choose Debug Console -> CMD. Then browse to your /wwwroot folder and examine the web.config file contents. Check to make sure that the connection string listed there is also what you expect.

Note that the App Settings Connection String listed in the portal will take precedence over what's in your web.config file. See if the database you expect to connect to is actually listed in these two places.

Also, log into your SCM site and browse to the /App_Data directory. See if there is a database there that you didn't expect to see.

Upvotes: 1

Related Questions