I have a ASP.Net Web Application which connects to a SQL Server. The web.config has <connectionStrings/>. It appears web.config is not being read at runtime, and it's causing data bound controls to fail at runtime with "The ConnectionString property has not been initialized"
My connection string uses a System DSN, and appears to follow connectionStrings Element. The DSN tests properly, and when I use the connection string from web.config on a control's property, all works well.
EDIT: Its blowing up on the Visual Studio generated line grvSysStatus.DataBind()
. Here's the code:
<asp:GridView ID="grvSysStatus" runat="server" GridLines="None" AutoGenerateColumns="False" Width="95%" showHeader="False">
<Columns>
<asp:BoundField DataField="message_text" />
</Columns>
</asp:GridView>
Any ideas?
Thanks,
Jeff
<configuration>
<connectionStrings>
<add name="MasterTraq_Dev"
connectionString="Dsn=MasterTraq_Dev;uid=xxxxx;pwd=yyyyy;trusted_connection=No;app=Microsoft® Visual Studio® 2010;wsid=zzzzz;database=MasterTraq_Dev"
providerName="System.Data.Odbc"
/>
</connectionStrings>
...
</configuration>
That connection string looks so wrong.
I've not used a DSN since classic ASP days but I'm pretty sure it didn't allow a 'registered trademark' character in it.
Have a look at connectionstring.com perhaps?
Related
Get an error when trying to open some of my forms in the designer.
This is the stack trace:
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkPropertyDescriptor.SetValue(Object component, Object value)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)
Here's my app.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="Main.ConnectionString" value="Server=localhost;Database=ACTUAL_DB_NAME_HERE;User ID=ACTUAL_USER;Password=ACTUAL_PASSWORD;"/>
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
When you are trying to open your form in designer, your app.config is not accessible to your form. It is because your program is not running and form is not intended to show relevant data from the database. This is by design. It is intended to show just form layout and visuals. So you must not try to open database connections, read files, play video and etc while in design mode.
See this question for the details.
If the component on this form was written by you, try to wrap the code that breaks the designer in the component Load event in the following:
if ( this.Site == null || !this.Site.DesignMode )
{
... // code that breaks the designer
}
EDIT: Best practice is to store connection strings in specialized section of app.config. Here is a good explanation of this.
With VS 2010 SP1 I created an ASP.NET MVC4 project from the "internet" template. I then created a connection string for SQL Server CE 4.0:
<add name="DefaultConnection"
connectionString="Data Source=|DataDirectory|MyDatabase.sdf;Password=123456"
providerName="System.Data.SqlServerCe" />
With the web application successfully debug-launched in cassini, I choose the "Register" user option. Immediately this causes the InitializeSimpleMembershipAttribute filter to execute. The filter crashes the site when it reaches this code:
Database.SetInitializer<UsersContext>(null);
using (var context = new UsersContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
//This line never executes. It is meant to configure my custom user table.
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "ID", "UserName", true);
The Exists() check throws an ArgumentException stating:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
Now I wanted to be sure that (1) there was nothing wrong with my connection string and (2) there was nothing wrong with my provider. To do that, I inserted a snippet of code before the Exists() check. The snippet used a new SqlCeEngine to create the database, and Dapper calls to setup user tables. That code worked just fine, just before exploding on the Exists() check again.
I then considered that EF might need some additional setup help. I tried replacing the EF defaultConnectionFactory in my web.config:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
Still the exception I received did not change.
I'm now wondering if EF needs a "special" connection string to work with SQL Server CE. I will be checking here and here. But at first glance I'm not sure that is the issue.
Thoughts?
EF requires the providers to be installed.
Use one the connect to DB options to check. eg ADD Entity DAta Model to Project. Just to the providers available to it. Do you see your preferred connection Client ?
Seems Compact edition should work with some restrictions...
http://technet.microsoft.com/en-us/library/cc835494.aspx
So then I think its The "DEFAULT CONNECTION" issue
See the Context constructor. EF looks for a connection by that name unless you pass an alternative in.
try...
<connectionStrings>
<add name="MyContextName" connectionString="bla";App=EntityFramework"
providerName="System.Data.SqlServerCe.4.0" />
You need to install Sql Server Compact edition. You can download it from here.
I am trying to create a simple application using the WebMatrix tool & Razor syntax. Hence learning a bit of Razor. I dont see a feature to create/call stored procedure's. Is this not supported?
SQL Compact (the default database for ASP.NET Web Pages) does not support Stored Procedures. If you have access to SQL Server, you can use Stored Procedures in it (and it's full supported in ASP.NET Web Pages).
First create a connection to SQL Server that supports Stored Procedures. See example below of entry in web.config file that WebMatrix can create for you.
<configuration>
<connectionStrings>
<add connectionString="trusted_connection=True;server=.\;database=YourDB" name="YourDB" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Second, connect to and run your Stored Procedure as in example below.
#{
var db = Database.Open("YourDB");
string YourSP = String.Format("EXEC dbo.YourSP");
db.Execute(YourSP);
}
Third, output results of YourSP to verify everything worked.
<table class="table-data">
<tbody>
#foreach(var row in db.Query(YourSP))
{
<tr>
<td>#row.YourQueryFieldName</td>
</tr>
}
</tbody>
</table>
I have set up IIS 7.5 to statically serve some files, and some of these files are actually symbolic links (created by mklink).
Even if I disabled both kernel and user caching, these files seems to be cached somehow by IIS. And IIS is still serving old versions after the files are modified.
To be sure that it is not caused by ASP.NET, I've created a dedicated unmanaged AppPool. I have also checked that these file are not cached by browsers.
My web.config is following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<caching enabled="false" enableKernelCache="false" />
<urlCompression doStaticCompression="false" doDynamicCompression="false" />
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</configuration>
There are several people mentioning this problem:
http://forums.iis.net/t/1166077.aspx
http://forums.iis.net/t/1171204.aspx
Any hints how to solve this problem?
This problem drove me nuts for like a month a while back. You have to disable IIS caching in the registry, as far as I know this isn't documented anywhere for IIS 7 but instead is an old IIS 5 trick that still works. You can either turn the below into a .reg file and import it or you can just navigate to the section and add it manually. I recommend rebooting after changing this parameter, I'm not sure if IIS picks it up after just an iisreset.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\InetInfo\Parameters]
"DisableMemoryCache"=dword:1
I was previously able to fix this issue on IIS7 with Banin's fix. Since then, I have moved to Windows 10 with IIS 10, and suffered the same problem again. DisableMemoryCache did not help.
I then disabled kernel caching, and for now, that seems to fix the issue (I'm sorry for the Dutch in the screenshot):
Banin's solution worked for me. The issue was resolved after changing registry parameter and resetting IIS. The C# program below (you can use LINQPad to run it) will help you reproduce the issue:
using System.IO;
using System.Net;
void Main()
{
var virtualPath = "JunctionPoint/sample.js";
var physicalPath = $#"C:\IISROOT\JunctionPoint\{virtualPath}";
for (int i = 0; i < 100; i++) {
File.WriteAllText(physicalPath, i.ToString());
Console.Write(i + "=");
var client = new WebClient();
string html = client.DownloadString($"http://localhost/{virtualPath}");
Console.WriteLine(html);
if (i.ToString() != html) {
Console.WriteLine("Issue reproduced!!!");
}
}
}
I'm playing with this Azure web role sample. It contains a class derived from RoleEntryPoint and a .aspx page that contains a button click handler.
I test it in Azure Emulator. I put the following code (taken from here)
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
in both role OnStart() and the button click handler. When role OnStart() is invoked it happens to run in WaIISHost.exe under MachineName\\MyLogin account and when button handler code is invoked it happens to run in w3wp.exe under MachineName\\NETWORK SERVICE account. That's surprising.
Why are these pieces of code from the same role project run inside different processes and under different accounts? Can I change that?
David is correct. In addition to that, you can turn off this behavior and run everything in the hostable web core (as it worked before SDK 1.4). You just need to comment out the "Sites" section in the services definition like in the example below:
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="aExpense.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="aExpense" vmsize="Medium">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="HttpsIn" endpointName="HttpsIn" />
</Bindings>
</Site>
</Sites>
<ConfigurationSettings>
<Setting name="DiagnosticsConnectionString" />
<Setting name="DataConnectionString" />
<Setting name="allowInsecureRemoteEndpoints" />
</ConfigurationSettings>
With Windows Azure v1.3 and beyond, a Web Role takes advantage of the full IIS, rather than Hosted Web Core. IIS runs in a separate appdomain.
See this blog post from the Windows Azure team for the gory details.