|
-
May 2nd, 2005, 01:22 PM
#1
Thread Starter
Member
My Connection String Is not working properly [RESOLVED]
Dear all;
I am connecting my app to an mdb file...
I need to accomplish the following:
1- Connect to my acces file succefully
2- Query the database and see the data.
3- Compare it with the users password in order to grant or fail access to the website.
Here is my code:
Code:
Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter
MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\DBs\db1.mdb"))
MyCommand = New SqlDataAdapter("select * from Students", myConnection)
DS = new DataSet()
MyCommand.Fill(DS, "Students")
datagrid1.DataSource = DS.Tables("Students").DefaultView
datagrid1.DataBind()
end sub
When I run it, the following error comes:
Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.
Source Error:
Line 20:
Line 21: DS = new DataSet()
Line 22: MyCommand.Fill(DS, "Students")
Line 23:
Line 24: datagrid1.DataSource = DS.Tables("Students").DefaultView
Source File: C:\Documents and Settings\Administrator\My Documents\STS\index.aspx Line: 22
Stack Trace:
[InvalidOperationException: The ConnectionString property has not been initialized.]
System.Data.SqlClient.SqlConnection.Open() +809
System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState) +44
System.Data.Common.DbDataAdapter.Fill(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +98
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +36
ASP.index_aspx.Page_Load(Object Sender, EventArgs E) in C:\Documents and Settings\Administrator\My Documents\STS\index.aspx:22
System.Web.UI.Control.OnLoad(EventArgs e) +55
System.Web.UI.Control.LoadRecursive() +21
System.Web.UI.Page.ProcessRequestMain() +724
Please advice, i have never made a connection to a database through ASP.Net before
Regards
Ahmed Abughoush
Last edited by ahmedabugh; May 3rd, 2005 at 02:05 AM.
-
May 2nd, 2005, 01:42 PM
#2
Addicted Member
Re: My Connection String Is not working properly
I think you should be using OleDBConnection and not sqlConnection.
Plus your line for the connection string seems to be wanting to lookup the connectionstring in the web.config.
For your connection string it would be simply
Dim oleDB as new OleDB.OleDBConnection( <insert connection string> )
This site has the connection strings for all data providers:
http://www.connectionstrings.com/
-
May 2nd, 2005, 01:49 PM
#3
I wonder how many charact
Re: My Connection String Is not working properly
VB Code:
MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\DBs\db1.mdb"))
This line is all jacked up. The way it is written is to grab the connectino string from the configsettings section of your app's web.config file.
But where the string is, is supposed to be the key name.
VB Code:
MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))
Then in your web.config file, below system.web you put something like:
VB Code:
<configSettings>
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\DBs\db1.mdb">
</configSettings>
The other problem is it looks like you're using a access database, and trying to use a SqlConnection and SqlCommand object, instead of using OLEDBConnection and OLEDBCommand object.
-
May 2nd, 2005, 01:53 PM
#4
Addicted Member
Re: My Connection String Is not working properly
-
May 2nd, 2005, 02:14 PM
#5
Thread Starter
Member
Re: My Connection String Is not working properly
Thanks for your fast reply
Dear token;
You are right, it worked fine thanks
Dear nemaroller;
Thanks for your advice, it looks more practical to do it in your way.
But when i use it, it gives me an error that I should add the "<configuration>" tag
when I do, It gives me this error "Parser Error Message: Unrecognized configuration section 'configSettings'"
Can you help me if you don't mind
-
May 2nd, 2005, 03:04 PM
#6
Addicted Member
Re: My Connection String Is not working properly
If you are going to put the connection string in the web.config file it should look like this:
<configuration>
<system.web>
.
.
bunch of code
.
.
</system.web>
<appSettings>
<add key="myConnectString" value=" <insert connection string> ">
</appsettings>
</configuration>
-
May 3rd, 2005, 01:40 AM
#7
Thread Starter
Member
Re: My Connection String Is not working properly
I'll try it, it will be much more profisinal this way...
Thnks
-
May 3rd, 2005, 09:53 AM
#8
Addicted Member
Re: My Connection String Is not working properly [RESOLVED]
Once you start getting more used to building web applications you can perhaps take a peek at Microsoft enterprise library. It abstracts a lot of the code out of your application as well as demonstrates good patterns and practices.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|