Web.Config file and Linq to Sql changing its place - linq-to-sql

I'm having a strange issue with my project. It was a Web Site that is now converted to a Web Application that is in a solution. Initially classes were setup using Linq to Sql .dbml file, which stored its connection string in /MyProject/web.config. Now the project ('Web Application') is in a solution and when I modify the Linq to Sql dbml file it creates a web.config file with only its connection string one level above, in /MySolution/web.config, while I still have /MySolution/MyProject/web.config. That gives errors with duplicate connection string names. So, how can I have Linq to Sql just use the web.config file in /MySolution/MyProject/web.config, or is my entire web.config file supposed to be in MySolution/web.config (I would prefer to keep it where it is) Thanks!
PS: the datacontext is in /MySolution/MyProject/MyCode/Models/MyDataContext.dbml

It appears as though updating the dbml will always force only the root web.config to be updated. It will likely be easiest to maintain your project if you do only use that root web.config, but you do have another option.
Each folder can have its own config, which is why you're getting the duplicate name exception. If you want to get around this, you can first remove then add a connection string with the same name. If you do this, your connectionStrings block (within /MySolution/MyProject/web.config) will look similar to the following:
<connectionStrings>
<remove name="MyConnectionString"/>
<add name="MyConnectionString" connectionString="XXXXXXXXXX"
providerName="System.Data.SqlClient" />
</connectionStrings>
Like I said, I can't really recommend that you do this, as your dbml will still save to the root web.config, so it might not be easy for other developers to realize what is going on.

Related

Referring to a separate file for connection strings for appsettings.json?

In my .net framework projects I like to have the following in my app.config
<connectionStrings configSource="connections.config" />
with connections.config containing something like
<connectionStrings>
<add name="ApplicationDatabase" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=mydatabase;User Id=myuser; PWD=mypassword;" />
</connectionStrings>
Then I don't check connections.config into source code.
I want to use a similar trick with .NET Core, but it makes use of appsettings.json
via
services.AddDbContext<ApiDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("MyDatabase")));
How do I proceed?
You can create a separate JSON file (or XML, or any of the configuration providers) and load that at application startup:
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder =>
{
builder.AddJsonFile("connectionstrings.json", optional: true);
})
Take a look at the docs for all the supported configuration providers.
For those who would encounter similar situation when they want to secure the connection string (or any other sensitive values) can simply use User Secrets. In Visual Studio (tested on 2019) right clicking on the project, then clicking on "Manage User Secrets" would open secrets.json file that is a hidden file. Keys/values can be entered here just like appsettings.json file.

Using a single connection for multiple webforms and single database

I am creating a project with multiple webforms using asp.net and C#. In it, I wish to open a connection string on my first form to connect my webpage with MySql database and I wish to use the same connection string for further webforms which are interlinked and then close the connection at last form. Amongst all this, my database table remains the same throughout the execution. How can I do that? Since I am new to asp.net and C#, I wish to have a detailed answer.
u can save ur connection in web.config and get in ur code with configuration namespace.
<add name="DbEntities" connectionString="data source=.;initial catalog=db;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />
and add this to your codes :
string s= System.Configuration.ConfigurationManager.
ConnectionStrings["DbEntities"].ConnectionString;
i think u need add this refrence to ur project :
System.Configuration

Long list of parameters in SSRS

I have a report with a list of customers passed in as a parameter. This list has become quite large and found it hit the limit imposed by MaxHttpCollectionKeys. I've found this documented in several posts, but I now have a more specific issue.
I have set this a lot higher in the SSRS web.config file which has solved the problem when I access the report via the report manager (ServerName/Reports). However this doesn't solve the problem when I render via the report server (ServerName/ReportServer).
I do need to access these reports via the report server as we pass some hidden parameters through from our App, as well as having some user selectable parameters. Searching for the solution only gives me posts related to the original solution which I've found.
Does anyone else have any ideas on solutions for this.
Many thanks.
Ok found solution.
I didn't realise there are two web.config files. One for the manager, one for the server. These both needed to have the mod made. Needed to add the following code to the web.config file in
C:\Program Files\Microsoft SQL Server\MSRS10.MSSQLSERVER\Reporting Services\ReportServer
Between the < system.web > and < runtime > tags.
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="20000" />
</appSettings>

LINQ to SQL Connection String

I've used LINQ to SQL in a unit testing project to mock some test data. I've changed the database name today and consequently the LINQ to SQL code doesn't work any more. I get the following error 'System.Data.SqlClient.SqlException: Cannot open database "XXX" requested by the login. The login failed'. I've tried changing the Connection property on the DataContext in the designer but this doesn't work. I've tried changing the connection string in app.config as well but this doesn't work as well. Is there anything I missing here?
I managed to solve this problem: In opened up the .dbml file in the XML editor (rather than the designer) and found a Database element with the old database name referenced. Once I changed this then everything was OK.

textboxes in Datarepeater dynamically 'databound'

I need to know if it is possible to dynamically bind a textbox residing within a datarepeater to a 'dynamically' created BindingSource. I am using VB.net. The database I am using is a MySQL database.
I have to use the connection dynamically due to the fact that the database my not permanently reside on the same server.
[edit]
ok, so it seams that I am a dolt when asking questions. The app that I am making is not web based. it is a simple (I hope) app that connects to a MySQL database, accesses a table so I can edit/view it. Current setup is using the Add DataSource wizard. I have successfully connected to the dbase dynamically using the mysql connector dll but without the textboxes set at design time to a datasource, I am unsure on how to 'link' them via the datarepeater.
Your connection string should be defined in your Web.Config, and if you move your database to a different server, it's just a matter of modifying the web.config entry. As long as you keep the connection string name the same, the BindingSource object will pick up the new value from the config.edit
In truth, the same concept should apply here as it does in the web app answer listed above.
All of your data objects should be hard-coded, and it's just the connection string (which you'll have to either ask the user for, or push out as update when the DB moves) which will get modified.
For example, create a App.Config file in your project. Have one of your configuration values be the connection string. This config value will be where you go to get the connection string whenever you need it. Then your wizard will be there to allow users to easily modify the connection.
then look in app.config
the conenction string should be there.
If it is not then you should put it in here as you can change this file at any time and not have to recompile your app.