Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anybody tell me how to connect to a database using the web.config file. I'm using Access. I want to check for username and password in the database. I'm unable to connect to the database through web.config using VB.NET.


VB.NET
//in vb.net

 Dim connStr As String = ConfigurationManager.ConnectionStrings("MainConnStr").ConnectionString

 sqlcom = New OleDbCommand("select * FROM registered_customers WHERE login_name= '" & userName & "'  AND pword='" & password & "'", connstr)


XML
//in web.config   
<removed name="LocalSqlServer&quot" />
   <removed name="LocalSqlServer" connectionsting="Data Source=E:\Anil\kank.mdb;User Instance=true" providername="Microsoft.Jet.OLEDB.4.0">


Error: value of type string cannot be converted to system.data.oledb.oledbconnection


How should I change the connectionstring to open the connection?
Posted
Updated 9-Apr-10 2:26am
v2

Of course, only the worst possible code would put that in the code behind and not in a seperate data layer.
 
Share this answer
 
yes i got it, Connection To Database: Using Web>config(VB)
just im adding answer as it might help someone

in web.config

XML
<connectionStrings>
   <add name="MainConnStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\vana\Anil\kan.mdb;"/>
  </connectionStrings>



and in .aspx.vb

strconnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MainConnStr").ConnectionString)
strconnection.Open()
 
Share this answer
 
v3
in code behind page
// For C#
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings[sa1].ConnectionString);
con.Open();

in web.config
<connectionStrings>
<add name=sa1 connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\a\\Database1.mdb" providerName="System.Data.OleDb">
<connectionStrings>
 
Share this answer
 
This might help you to figure it out: ConnectionStrings.com[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900