How to have relative path for Microsoft Access Driver(.mdb) file - c#

<connectionStrings>
<add name="localconnection" connectionString="Driver={Microsoft Access Driver (*.mdb)}; DBQ=D:/abc/pqr/xyz/abc.mdb; UID=;PWD=12345;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Question: How can i pass relative path for (.mdb) file in web.config file? I tried using (../) and (~/) but its not working. Can any one please help me out.

In web.config
<connectionStrings>
<add name="localconnection" connectionString="Driver={Microsoft Access Driver (*.mdb)} ;pwd=12345; DBQ="/>
</connectionStrings>
In .aspx file
string Connection = ConfigurationManager.ConnectionStrings["localconnection"].ConnectionString + Server.MapPath("//abc//pqr//xyz//abc.mdb");
OdbcConnection SGemsConn = new OdbcConnection(Connection);

Related

In C#, how do i cope with XmlException: 'xdt' is an undeclared prefix. when trying to set new password in connectionString?

I am trying to set a new password in my app.config files. I tried to do this using code below. Exception is thrown here because of the xdt:Transform in the connectionString:
configuration.ConnectionStrings.ConnectionStrings["DbContext"].ConnectionString
= string.Format("Data Source=x ;Initial Catalog=x ;User='sa';Password='{0}';",
textBox1.Text);
Reason: "System.Configuration.ConfigurationErrorsException: ''xdt' is an undeclared prefix. "
This is a transformed config file and hence contains "xdt:Transform"
Here is the connectionstring I want to change:
<connectionStrings>
<add name="DbContext" connectionString="Data Source=x;Initial
Catalog=x;User='sa';Password='x';"
xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>
Is there any possibility to somehow parse a connectionString part of the xml file? Thank you in advance!
The xdt namespace should be defined. In the a web.release.config file (*), this is done like this:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
in the example below, the "SetAttributes" transform will change the value of a connectionstring
-->
<connectionStrings>
<add name="MyConnectionString" connectionString="some value" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
</configuration>
*) I must admit that these transformations are normally used for web.config files rather than app.config files. However, it can be done for app.config also, but this is another topic.

How do I modify my web.config file to support SQL Server 2016?

I'm totally new to C#/.Net development. I've read extensive documentation about the web.config file on msdn and still am unsure what to change.
If answered would you please provide an example of what I should do? I can provide more info if necessary.
You don't have to provide information about SQL Server, You just need to give correct connection string, which points to your database.
Something like:
<connectionStrings>
<add name="myConnectionString"
connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
This is one way if you are just using some local database.
Otherwise you can set it up like this:
<connectionStrings >
<add
name="myConnectionString"
connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;"
providerName="System.Data.SqlClient"/>
And then in your code you should write:
using System.Web.Configuration;
And then at last you can set up your connection variable something like this:
SqlConnection con = new SqlConnection( WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
Are you using the entity framework? Because then it can be generated automatically.

Why aren't new connection strings in Web.config file retrieved?

In my ASP.NET project I added a new connection string in the Web.config file having the name "Proba":
<connectionStrings>
<add name="Users" connectionString="Data Source=.\SQLExpress;Initial Catalog=Registratura;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="Test" connectionString="Data Source=.\SQLExpress;Initial Catalog=REGDATABASE;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="Proba" connectionString="Data Source=.\SQLExpress;Initial Catalog=AnotherReg;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
This piece of code should list all the 3 connection strings:
List<String> conns = new List<string>();
foreach (ConnectionStringSettings conn in System.Configuration.ConfigurationManager.ConnectionStrings)
{
if(conn.Name != "LocalSqlServer")
conns.Add(conn.Name);
}
But it only detects the former 2 strings. I have built and rebuilt, closed Visual Studio and then reopened it, but nothing changed.
I have also tried to update the database in the Package Manager Console, but once again the connection is not found and the following red error occurs:
No connection string named 'Proba' could be found in the application
config file.
Why could it happen?
After different attempts, I came to realize that the Web.config file onto which I had imprinted the new connection strings was kind of a phantom of the real Web.config that needed to be changed. It had been opened before some configuration settings I made, so it was no more available. That is why the code written in it was not considered.

ASP.Net can't see my database from the ISS

I decided to learn how to make a simple ASP.Net project, with a reference to a database project through the Repository Pattern.
I have my Controller calling for a List<Weight> to handle:
public IActionResult MyWeight()
{
var repo = new Database.Repositories.WeightRepository();
var data = repo.GetWeight().Result;
return View(data);
}
When repo.GetWeight() is called, I get an AggregateException error, with an inner exception saying:
"No connection string named 'MyDatabaseConnection' could be found in the application config file."
So for clarity, let me outline the solution's structure:
aspProj
Controllers
Views
Service
App.config (1)
Web.config
...
Database
Entities
Repositories
App.config (2)
...
Database.Test
Test.cs
App.config (3)
...
I've added the following connectionString to all App.configs and the Web.config:
<connectionStrings>
<add
name="MyDatabaseConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I have tested the database both from Visual Studio's Server Explorer, and through the Test.cs file from the test-project. I can insert data and retrieve without a problem.
But when the ASP.Net-part wants to access it, there is no love.
I thought it might be the ISS which did not know the path from where it is...
Any thoughts?
__
Edit:
My Web.config:
My AppSetting.json:
Well the problem is clear - ASP.NET is trying to access the database using a connection string with the name MyDatabaseConnection:
<connectionStrings>
<add name="MyDatabaseConnection" connectionString="put the connection to the db here..." />
</connectionStrings>
And in your Web.config you only have a connection string with the name WeightDatabaseConnection:
<connectionStrings>
<add name="WeightDatabaseConnection" connectionString="put the connection to the db here..." />
</connectionStrings>
Just add a new element for MyDatabaseConnection under <connectionStrings> in the Web.config file and it should work

Dynamics Connection SQL Server C#

I would like some explanations on how to create and set up a dynamic connection to SQL Server DB engine in a C # project
if you want connection string in config and read it than you need to do like this , put possible connectionstring in config
<connectionStrings>
<add name="CharityManagement"
connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
<add name="CharityManagement_two"
connectionString="Data Source=.;Initial Catalog=CharityManagement_two;Integrated Security=True"/>
</connectionStrings>
and than read it base on condition using configurationmanager class
//to read first connection string
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
//to read second connection string
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement_two"].ConnectionString;
This is what you're after
In the config file
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
Then to read the connection string in your code you will do
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Don't forget to use using System.Configuration;
Further reading here

Categories