Murray Brown
asked on
ASP.net connection string
Hi
I am trying to connect to a database in ASP.net for the first time. As instructed I opened the Web.config and just before the last </configuration> I inserted the code below. Is that the right place? Also used FTP to save my database to a folder called "Database" in the wwwroot folder
so should I change the source to something like "\Database\Contacts.accdb" ?
I am trying to connect to a database in ASP.net for the first time. As instructed I opened the Web.config and just before the last </configuration> I inserted the code below. Is that the right place? Also used FTP to save my database to a folder called "Database" in the wwwroot folder
so should I change the source to something like "\Database\Contacts.accdb"
<connectionStrings>
<add name="WhatEverNameYouWant" connectionString="Microsoft.Jet.OLEDB.4.0;Data Source=c:\domains\myDomain.com\db\myDB.mdb;User Id=myLogin;Password=myPass;" providerName="System.Data.OleDb"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
You can also do it like:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Web.Config Configuration File -->
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;da tabase=Nor thwind;uid =sa;passwo rd=secret; " />
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
You are also doing it right...
<?xml version="1.0" encoding="utf-8" ?>
<!-- Web.Config Configuration File -->
<configuration>
<appSettings>
<add key="ConnectionString"
value="server=localhost;da
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
You are also doing it right...
ASKER
THe code in the Web.config file is as follows. Where do I put the code within this:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
-->
<compilation debug="true" strict="false" explicit="true">
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericEr rorPage.ht m">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm " />
</customErrors>
-->
<pages>
<namespaces>
<clear />
<add namespace="System" />
<add namespace="System.Collecti ons" />
<add namespace="System.Collecti ons.Generi c" />
<add namespace="System.Collecti ons.Specia lized" />
<add namespace="System.Configur ation" />
<add namespace="System.Text" />
<add namespace="System.Text.Reg ularExpres sions" />
<add namespace="System.Web" />
<add namespace="System.Web.Cach ing" />
<add namespace="System.Web.Sess ionState" />
<add namespace="System.Web.Secu rity" />
<add namespace="System.Web.Prof ile" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.W ebControls " />
<add namespace="System.Web.UI.W ebControls .WebParts" />
<add namespace="System.Web.UI.H tmlControl s" />
</namespaces>
</pages>
</system.web>
</configuration>
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
-->
<compilation debug="true" strict="false" explicit="true">
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericEr
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm
</customErrors>
-->
<pages>
<namespaces>
<clear />
<add namespace="System" />
<add namespace="System.Collecti
<add namespace="System.Collecti
<add namespace="System.Collecti
<add namespace="System.Configur
<add namespace="System.Text" />
<add namespace="System.Text.Reg
<add namespace="System.Web" />
<add namespace="System.Web.Cach
<add namespace="System.Web.Sess
<add namespace="System.Web.Secu
<add namespace="System.Web.Prof
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.W
<add namespace="System.Web.UI.W
<add namespace="System.Web.UI.H
</namespaces>
</pages>
</system.web>
</configuration>
You add:
<add name="WhatEverNameYouWant" connectionString="Microsof t.Jet.OLED B.4.0;Data Source=c:\domains\myDomain .com\db\my DB.mdb;Use r Id=myLogin;Password=myPass
within the <connectionStrings /> element, so it becomes:
Although you might want to check that path. If it's on a web host you may find that the path you have specified is invalid.
<add name="WhatEverNameYouWant"
within the <connectionStrings /> element, so it becomes:
<connectionStrings>
<clear />
<add name="WhatEverNameYouWant" connectionString="Microsoft.Jet.OLEDB.4.0;Data Source=c:\domains\myDomain.com\db\myDB.mdb;User Id=myLogin;Password=myPass
</connectionStrings>
Although you might want to check that path. If it's on a web host you may find that the path you have specified is invalid.
ASKER
Hi. I am an absolute beginner at this. I need to know the exact location of the code. Below I have copied the code that was in the Web.Config file. How do I update that code refer to an Access database in the wwwroot folder
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
-->
<compilation debug="true" strict="false" explicit="true">
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericEr rorPage.ht m">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm " />
</customErrors>
-->
<pages>
<namespaces>
<clear />
<add namespace="System" />
<add namespace="System.Collecti ons" />
<add namespace="System.Collecti ons.Generi c" />
<add namespace="System.Collecti ons.Specia lized" />
<add namespace="System.Configur ation" />
<add namespace="System.Text" />
<add namespace="System.Text.Reg ularExpres sions" />
<add namespace="System.Web" />
<add namespace="System.Web.Cach ing" />
<add namespace="System.Web.Sess ionState" />
<add namespace="System.Web.Secu rity" />
<add namespace="System.Web.Prof ile" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.W ebControls " />
<add namespace="System.Web.UI.W ebControls .WebParts" />
<add namespace="System.Web.UI.H tmlControl s" />
</namespaces>
</pages>
</system.web>
</configuration>
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!--
Visual Basic options:
Set strict="true" to disallow all data type conversions
where data loss can occur.
Set explicit="true" to force declaration of all variables.
-->
<compilation debug="true" strict="false" explicit="true">
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericEr
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm
</customErrors>
-->
<pages>
<namespaces>
<clear />
<add namespace="System" />
<add namespace="System.Collecti
<add namespace="System.Collecti
<add namespace="System.Collecti
<add namespace="System.Configur
<add namespace="System.Text" />
<add namespace="System.Text.Reg
<add namespace="System.Web" />
<add namespace="System.Web.Cach
<add namespace="System.Web.Sess
<add namespace="System.Web.Secu
<add namespace="System.Web.Prof
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.W
<add namespace="System.Web.UI.W
<add namespace="System.Web.UI.H
</namespaces>
</pages>
</system.web>
</configuration>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much
go through the below web config
<?xml version="1.0"?><configurat
<configSections>
<sectionGroup name="system.web.extension
<sectionGroup name="scripting" type="System.Web.Configura
<section name="scriptResourceHandle
<sectionGroup name="webServices" type="System.Web.Configura
<section name="jsonSerialization" type="System.Web.Configura
<section name="profileService" type="System.Web.Configura
<section name="authenticationServic
<section name="roleService" type="System.Web.Configura
</sectionGroup>
</sectionGroup>
</sectionGroup>
<section name="activerecord" type="Castle.ActiveRecord.
</configSections>
<appSettings/>
<connectionStrings>
<add name="main" connectionString="Data Source=MAC22\SQLEXPRESS;Da
</connectionStrings>
<activerecord isWeb="true" isDebug="true" pluralizeTableNames="true"
<config>
<add key="connection.driver_cla
<add key="dialect" value="NHibernate.Dialect.
<add key="connection.provider" value="NHibernate.Connecti
<add key="connection.connection
<add key="proxyfactory.factory_
</config>
</activerecord>
<system.web>
<globalization culture="en-GB" uiCulture="en-GB" enableClientBasedCulture="
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C56193
<add assembly="System.Data.Data
<add assembly="System.Web.Exten
<add assembly="System.Xml.Linq,
<add assembly="System.Web.Exten
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D
<add assembly="System.Windows.F
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<!--<authentication mode="Windows"/>-->
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericEr
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm
</customErrors>
-->
<authentication mode="Forms">
<forms name=".ASPXAUTH" protection="All" timeout="60" loginUrl="Login.aspx" defaultUrl="EmpHome.aspx"/
</authentication>
<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Exten
<add tagPrefix="asp" namespace="System.Web.UI.W
</controls>
</pages>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Se
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Se
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSh
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
<system.webServer>
<validation validateIntegratedModeConf
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandl
</modules>
<handlers>
<remove name="WebServiceHandlerFac
<remove name="ScriptHandlerFactory
<remove name="ScriptHandlerFactory
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory
<add name="ScriptHandlerFactory
<add name="ScriptResource" preCondition="integratedMo
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microso
<dependentAssembly>
<assemblyIdentity name="System.Web.Extension
<bindingRedirect oldVersion="1.0.0.0-1.1.0.
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extension
<bindingRedirect oldVersion="1.0.0.0-1.1.0.
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
There is no need to include the database in root folder, as you can acess it through configuration itself