Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

access connection

Status
Not open for further replies.

HrvojeVd

Programmer
Jun 22, 2008
36
HR
I'm using this code to get data from database, but I get an error: "Connection string property has not been initialized."

web.config:

<?xml version="1.0"?>
<configuration>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true"/></system.web>
<connectionStrings>
<add name="PjesmeConn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\domains\webfilozofa.net\db\Pjesme.mdb;"></add>
</connectionStrings>
</configuration>


web page:

sConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["PjesmeConn"].ConnectionString;
 
you need to provide more information.
the web.config is correct. and your assigning the connection string to a varible. you still need a connection object and set the connection string property.
Code:
IDbConnection cnn = new OleConnection(sConn);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
this is the rest of a code:

DataSet ds = new DataSet();
string strSQL = "";

OleDbConnection conn = new OleDbConnection(sConn);
strSQL = "SELECT Naziv FROM Pjesme WHERE Naziv like '" + ddPjesme.SelectedValue.ToString() + "'";
OleDbCommand cmd = new OleDbCommand(strSQL, conn);

conn.Open();

OleDbDataReader reader;
reader = cmd.ExecuteReader();

txtTekst.Text = "";
while (reader.Read())
{
ddPjesme.Items.Add(reader[0].ToString());
}

conn.Close();
 
correction:
strSQL = "SELECT Naziv FROM Pjesme";
 
i'm at a loss on this. I cannot see any problems with the code above. as a final test step through the code and ensure the sConn varible does contain the connection string.
if it doesn't this is the problem.

I would make 3 recommendations though
1. use parameterized queries, not injected sql.
2. unless ddPjesme.SelectedValue contains the wildcards for the like expression your like expression will only return exact matches.
3. you shouldn't mix the concerns like this
Code:
txtTekst.Text = "";
while (reader.Read())
{
    ddPjesme.Items.Add(reader[0].ToString());
}
instead have a member (or better yet, an object) to manage fetching data. have another member within the codebehind to manage the GUI. with the code above your trying to manage GUI controls, and database objects together. this can become very difficult to debug/refactor in the future.


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The problem is that this code is working localy but when I put on web I get an error.
 
then the file path to the database may be incorrect. this works locally because your box in is the server and you have full rights to your machine. but on the web server the path is probably different.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top