ASP.NET Sql Provider - asp.net

I got a problem. When i'm creating the new project in ASP.NET using VS 2010, it's web.config with a default connection string about SQL Express created. But i haven't even got SQL Express installed. What should i do to change the default AspNetSqlProvider to work with my instance of full-weight SQL Server as a services database? And how can i change the template for the ASP.NET project to create a project with my connection?

You mean this one:
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
To change this into one that'll work with SQL Server, it needs to look more like this:
<add name="ApplicationServices"
connectionString="data source=ServerName;Initial Catalog=DatabaseName"
providerName="System.Data.SqlClient" />
where DatabaseName is probably aspnetdb. It'll need some form of authentication, whether you use Windows authentication (in which case you can copy the Integrated Security element from the original connection string) or SQL Server authentication (where you'll have a username/password combination).
There's great info on building connection strings at connectionstrings.com.
To fix this for your future projects, you'll need to change the template for Web Projects. Locate the folder where your C# Web templates are kept (for me this is C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Web\1033.
Take a copy of the WebApplicationProject40.zip file (you might also want to backup the original somewhere!).
Inside it you'll find the web.config file with the SQL Express connection string. Change it to a SQL Server string, and then re-assemble the zip file.
The last step is to rebuild the VS template cache - from a command-line (VS Command Prompt probably works best), run devenv /installvstemplates. See here for details.

I just had a similar problem. I had a web site created in VS 2008 and wanted to try web parts. Added some to a page, then started getting:
SQLExpress database file auto-creation error:
The connection string specifies a local Sql Server Express instance using a database >location within the applications App_Data directory.
I ran aspnet_regsql.exe (wizard, in .Net framework folder) to create the database and then I added a connection string to web.config:
<configuration>
<connectionStrings>
<add name="aspnet_membership" connectionString="Data Source=localhost; Initial Catalog=aspnetdb; Integrated Security=SSPI;"/>
</connectionStrings>
</configuration>
Then I added this to web.config:
<configuration>
<system.web>
<webParts>
<personalization defaultProvider="SqlPersonalizationProvider">
<providers>
<add name="SqlPersonalizationProvider"
type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider"
connectionStringName="aspnet_membership"
applicationName="/" />
</providers>
<authorization>
<deny users="*" verbs="enterSharedScope" />
<allow users="*" verbs="modifyState" />
</authorization>
</personalization>
</webParts>
</system.web>
</configuration>

Related

Website can't connect to database on smarterasp.net webserver - possible error in connection string

I am getting a problem when I try to connect from my website to my database on my webserver (smarterasp.net). Everything works fine on the local host, then I uploaded the site to smarterasp.net and created the database there, now the site can't connect to the database, here is what I have in my web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="UsersConnectionString1" connectionString="Data Source=MYSQL5012.Smarterasp.net;Initial Catalog=db_9df63f_active;Uid=9df63f_active;Pwd=MYPASSWORD; "
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
</configuration>
Where password obviously includes my real password.
Here is what smarterasp.net provided as the connection string:
ASP.net "Server=MYSQL5012.Smarterasp.net;Database=db_9df63f_active;Uid=9df63f_active;Pwd=YOUR_DB_PASSWORD;"
Classic ASP "Driver={MySQL ODBC 5.1 Driver};Server=MYSQL5012.Smarterasp.net;Database=db_9df63f_active;Uid=9df63f_active;Password=YOUR_DB_PASSWORD;"
I also tried putting the connection string in the web.config file as connectionString= "Server=MYSQL5012.Smarterasp.net; Database=db_9df63f_active; Uid=9df63f_active; Pwd=MYPASSWORD;"
but this didn't work either.
Appreciate any help. Thanks!
It looks like smarterasp.net does not allow remote database connections if you dont have a premium account.
to solve that problem , after you publish your website, navigate to the file manager , on the root of your files on your website , there is a webconfig , edit it and modify the connection string there , replace it with the connection string given to you by the website database section.
on the websites webconfig file

Deployment of SQL Server Express database fails

I have to deploy ASP.Net application with remote database connection in IIS server.
Deployment has worked fine without database. And I was able to run that application on the IIS server.
Coming to the application with remote database I have given the connection string in C# code. And it is connected.
My web.config has thsi connection string :
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
I have replaced that with my database connection credentials as follows:
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=RemoteHost,1433;Initial Catalog=DBNAME;user Id=MyUserId;Password=MyPassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
In Package/Publish options I have selected (Import from web.config) and deployed that. Then I got the following error:
Web deployment task failed.(Object of type 'dbFullSql' and path 'Data
"Data Source=RemoteHost,1433;Initial Catalog=DBNAME;user
Id=MyUserId;Password=MyPassword" cannot be created.)
Object of type 'dbFullSql' and path "Data Source=RemoteHost,1433;Initial Catalog=DBNAME;user
Id=MyUserId;Password=MyPassword" cannot be created.
The source is invalid.
Script failed for Database 'DBNAME'.
Script failed for Database 'DBNAME'.
Index was outside the bounds of the array.
I am new to ASP.Net. Anybody please help me.
Thank you in advance
I think you didn't configure Web deployment package correct you can do that right Click on Project ,Select Properties and go to package/Publish SQL
I have add link for Walkthrough: Deploying a Web Application Project Using a Web Deployment Package (Part 1 of 4) for VS2010
http://msdn.microsoft.com/en-us/library/dd483479(v=vs.100).aspx

mvc4 authorisation doesn't work with ASP.net Configuration

I am following along this series:
http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api-with-entity-framework,-part-1
It all goes well up to the point, where one is requiered to use the ASP.net Configuration tool to create a role Administrator, a user and then log in.
[Authorize(Roles="Administrator")]
works fine, but I can't log in with the "Admin" account created via ASP.NET configuration; or, vice versa, see the users which I registered in the demo on the ASP.NET configuration panel.
And why did this demo create 2 Connectionstrings to two differend databases?
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WebStore-20120820081613;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebStore-20120820081613.mdf" providerName="System.Data.SqlClient" />
<add name="OrdersContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=OrdersContext-20120820083248; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|OrdersContext-20120820083248.mdf" providerName="System.Data.SqlClient" />
I'm running the whole thing on Windows Server 2008 R2; Visual Studio Express 12.
I've solved the issue by recreating the application with visual studio 2010.
... copied the connectionstring to the vs2010 project and voilá ... done.

web.config and membership

I have an Runtime error when I'm trying to perform login action or create user action on published website.
I have an ASPNETDB.MDF in appData folder, that created from ASP.NET Configuration.
I created new database in hosting, exported from ASPNETDB.MDF to a new database. I changed connection string.
Localy it work perfect, even with new connection string, but after i publish it i have an Runtime error.
I'm using asp login control and CreateUserWizard.
BTW, i've created some GridView and SqlDataSource to show all users, and it works.. But login and createWizard are not.
web.config
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="Server=mssql3322.dotnet-host.com;Database=aspnetdb3;Uid=SomeUserNAme;Password=SomePassword;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<roleManager enabled="true" />
<authentication mode="Forms" />
<compilation debug="false" targetFramework="4.0" />
</system.web>
</configuration>
BTW, i tryed to add membership provider to web.config and it didn't work even localy ))
UPD:
The error
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)
Well for starters you could add the following to your web.config:
<configuration>
<system.web>
<customErrors mode="off"></customErrors>
</system.web>
</configuration>
This should allow you to get a more detailed error message about what is wrong. See if that get's you started down the right path. It should at least provide more information about what might be wrong.
Update - try this in your web.config:
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Server=mssql3322.dotnet-host.com;Database=aspnetdb3;Uid=SomeUserNAme;Password=SomePassword;" providerName="System.Data.SqlClient"/>
<add name="ConnectionString" connectionString="Server=mssql3322.dotnet-host.com;Database=aspnetdb3;Uid=SomeUserNAme;Password=SomePassword;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Update - That worked! Here is a short explanation why:
By default the ASP membership classes use the connection string LocalSqlServer. The reason the code worked on your local machine is because the connection LocalSqlServer is defined in a machine level config file to point to the local sql database. When you published it the SqlDataSource/GridView worked because it was pulling the connection named ConnectionString that pointed to the correct server. However ASP membership was still trying to connect to the LocalSqlServer connection. The above config just ignores the machine level LocalSqlServer connection and defines it's own (and it leaves the ConnectionString one in place so that the GridView continues to function).

Web.Debug.config not processing substitution

I have a web application in VS2010 with a web.config like this:
...
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=MyProdDb;Initial Catalog=MyCat;User Id=MyUser;Password=MyPass;"
providerName="System.Data.SqlClient" />
</connectionStrings>
...
and a Web.Debug.config like this:
...
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=MyDevDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
...
The project is set to create a Debug build, and when I run it in the debugger, I get MyProdDb rather than MyDevDb
What am I missing?
UPDATED INFORMATION
Arbitrary XML-based .config files can now be processed, and the processing can happen at build time rather than at deployment time
http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx
Brilliantly, the transforms can also be previewed directly in Visual Studio.
As people have said the web.config versions only get applied at publish (MSDeploy) time. The normal way you would do things is to have your 'Debug' config in the actual web.config file and make changes to that for each of the deployment scenarios you have.
What am I missing?
You are missing the fact that web.config transformation occurs only when you do a deployment. If you don't publish your web application you cannot expect any transformation to occur. If you just run your website locally by hitting F5 no transformation will occur. It's only when you publish the application that the transformation is performed.
Web.config transformations gets processed at publish time only. When debugging (even in release mode), the basic Web.config file is being used.

Resources