I've looked and looked but could not come up with a working solution.
As an intermediate with C# and decent with HTML and things, I thought I'd fool around with Razor Web Pages and make a simple website. I was hoping to create a database with a list of all the titles and descriptions of each page. Then I could easily change all instances and references to each html file to stay DRY. But I can't seem to get anything but exceptions from the SQL table.
What I tried using Visual Studio 2015:
- Create Database.mdf with a simple table
- Connect to Database using Server Explorer
Add connectionStrings to Web.config
<connectionStrings> <add name="Database" connectionString="Data Source=|DataDirectory|\Database.mdf" providerName="System.Data.SqlClient" /> </connectionStrings>
- Make sure all WebMatrix dll packages are installed with NuGet.
Create default.cshtml
@using WebMatrix.Data @using System.Data.SqlClient <Html><body> etc... @{ var db = Database.Open("Database"); try { db.Query("SELECT * FROM Table"); } catch (SqlException odbcEx) { System.Diagnostics.Debug.WriteLine("It is an Exception: " + odbcEx); } } </body><Html>
Run in Microsoft Edge
After about 10 seconds, the website appears on localhost. But there is results from the table, only an exception in the Output:
It is an Exception: System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at WebMatrix.Data.Database.EnsureConnectionOpen()
at WebMatrix.Data.Database.<QueryInternal>d__0.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at WebMatrix.Data.Database.Query(String commandText, Object[] parameters)
at ASP._Page_Default_cshtml.Execute() in c:\Users\Person\OneDrive\Documents\Website\Default.cshtml:line 29
I've searched online and realized that since WebMatrix is out dated, there aren't to many solutions for Visual Studio. Razor doesn't appear to be widely used at this time--as far as I can tell.
I tried changing the connectionStrings to a few different things but with no luck. How can I get this to work?