Using a single connection for multiple webforms and single database - mysql

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

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.

Create a shared datasource in VS2012 for SSRS

I'm just looking to create a shared datasource in VS2012 without selecting an entire database scheme. We'll mostly put doing all the dataset queries by sql query.
I found this but i'm still unable to create a shared datasource.
http://msdn.microsoft.com/en-us/library/ms159165.aspx
For an embedded data source, verify that Embedded connection is selected. Does not exists.
So how do you create a shared datasource in VS2012 with just a connection string?
A DataSource is just a connection string in an element in an xml structure as far as SSRS is concerned. A shared one is just one kept as it's own object and then the rdl elements(reports) have a reference in their xml to that object. Think of an rds file (data source for SSRS) as this very similar to .NET standard connection strings:
Data Source=(server);Initial Catalog=(database)
Plus you can store credentials to mock who is running the report. Thus you can make a proxy user to run the database connection. However SSRS does this through a GUI called 'Business Intelligence Development Studio' it is an add on to Visual Studio that is generally SQL Server version matches that VS version EXCEPT FOR 2012. That one creates a shell Visual Studio of VS 2010 just meant for BIDS.
To my knowledge you should be creating these directly in BIDS and not try to hack the RDS file directly unless you get the ReportService2010.asmx web service to mess with it's properties in .NET (which is a lot more work.).
To add one you just do this:
Go into BIDS with a report project
Expand a project
Right Click 'Shared Data Sources'> 'Add New'
Click 'Edit...' next to connection string
You get a menu very similar to ADO.NET standard connection string creator
Put in ServerName
Put in DatabaseName
8*** Optional put in default credentials.
Click OK
Generally SSRS has three parts to everything it does
Datasource = connection string (rds file when not embedded)
Dataset = select query or proc results or other data source return (rsd file when not embedded)
Report = resultant xml display of elements such as parameters, tables, matrices, etc. (RDL file when working on hosted report)
Generally reports can have everything embedded or else just reference everything they use. References are often easier for deployments sake as SSRS is designed to look if DataSources first exist and NOT OVERWRITE them by default. Thus if you reuse a datasource it is much easier in the long run as long as policy for it is set up correct.
If you want to just know the structure of an rds file they look like this:
<?xml version="1.0" encoding="utf-8"?>
<RptDataSource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="Test">
<ConnectionProperties>
<Extension>SQL</Extension>
<ConnectString>Data Source=TestServer;Initial Catalog=TestDatabase</ConnectString>
</ConnectionProperties>
<DataSourceID>45be0ac1-80a8-4d5c-906b-c13b03298e0a</DataSourceID>
</RptDataSource>

asp.net no access to ConfigurationManager

I created one asp.net mvc application using linq to sql, and in the generated code it created this:
public ApowDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["APOWConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
Which worked well, However in my next mvc application for some reason it decided instead of looking at the web.config to look at a project resource instead... (and thus when deploying it did not work because the connectionstring was pointing to a dev server.
So to fix this I have edited the new projects datacontext to use the configurationmanager to grab the connectionstring BUT even after adding System.Configuration I cannot access the configuration manager (in my resources I can browse to the System.Configuration.ConfigurationManager and see the ConnectionStrings command, but in the code intellecence does not contain the ConfigurationManager under System.Configuration (but I can see a bunch of other methods, like ConfigurationSettings, for example)
So my questions are:
1\ Why has linq to sql objects decided to use a different method of looking up my connection string in my second project (i.e not use web.config).
2\ What is going on with the ConfigurationManager (why can't I access it?)
1&2\ The problem is that in my second solution, I have a separate project for the 'Model', and due to it not being in the .web project it does not by default look at the web.config, but in the project resources.
here is a good article how to sort this out: link text

Web.Config file and Linq to Sql changing its place

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.

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.