I am new to Umbraco and have inherited an Umbraco 4.9 site and need to display some data from an SQL database. I have created a macro using a cshtml file but it keeps failing on the creation of the sqldataconnection.
It sounds like it might be an issue with your connection string - what's the error you're receiving? There shouldn't be anything umbraco-specific here.
I have tested the connection string in a user control and it seems to work fine. I currently have it in the webconfig file but I've also tried placing it directly in the statement. It seems more like its not seeing the sqlconnection command as a valid statement. I have the using statements for the sqlclient .net resources but it doesn't seem to recognize them.
Maybe that isn't supported directly in Razor?
It seems hard to find good examples of how to access anything outside of the Umbraco system itself.
Any pointers to documentation, examples would be appreciated.
You should be able to run the same codebehind you do in a usercontrol in the Razor file with no differences. Though, it's probably not best practice to do the data connection straight from the Razor file, might be better to funnel this through a class that handles the data access.
It should still work though - can you paste the error you're receiving? I just tested the code here and had no issues once the connection string was in the correct format ("Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Password=YourPassword")
retrieve data from seperate MS SQL db
I am new to Umbraco and have inherited an Umbraco 4.9 site and need to display some data from an SQL database. I have created a macro using a cshtml file but it keeps failing on the creation of the sqldataconnection.
Here is the code.
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines
@using System.Xml.Linq
@using System.Text
@using System.Data
@using System.Data.SqlClient
@{
SqlConnection sqlConn = new SqlConnection(ConnectionString);
string sql = string.Format("SELECT * FROM [mydb] ");
}
<div class="park-list">
<h2>Parking List</h2>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th>Date</th>
</tr>
<tr>
<th>Name</th>
</tr>
</table>
</div>
Can anyone point me in the right direction. Thanks
Hi Paige,
It sounds like it might be an issue with your connection string - what's the error you're receiving? There shouldn't be anything umbraco-specific here.
I have tested the connection string in a user control and it seems to work fine. I currently have it in the webconfig file but I've also tried placing it directly in the statement. It seems more like its not seeing the sqlconnection command as a valid statement. I have the using statements for the sqlclient .net resources but it doesn't seem to recognize them.
Maybe that isn't supported directly in Razor?
It seems hard to find good examples of how to access anything outside of the Umbraco system itself.
Any pointers to documentation, examples would be appreciated.
Thanks
You should be able to run the same codebehind you do in a usercontrol in the Razor file with no differences. Though, it's probably not best practice to do the data connection straight from the Razor file, might be better to funnel this through a class that handles the data access.
It should still work though - can you paste the error you're receiving? I just tested the code here and had no issues once the connection string was in the correct format ("Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Password=YourPassword")
is working on a reply...