Jump to content

Define Connection String At Cs File


lbenezra
 Share

Recommended Posts

I use webdeveloper to edit our web site.When I enter the lines below at a aspx.cs file ' it was OK.but when I enter the lines at cs file I get an Error about "server.mappath: - :Error 2 The name 'Server' does not exist in the current context C:\Documents and Settings\liora\My Documents\website\HBWebsite-10-9-08\App_Code\List.cs 64 28 C:\...\HBWebsite-10-9-08\and the same thing about reader.Can anyone explain me why . :) Liora---------------------- //connect to database string DataPlace = Server.MapPath("App_Data\\hbc_new.mdb"); string connectionString = ConfigurationSettings.AppSettings["DataConnectionString"]; connectionString = connectionString.Replace("TEMP", DataPlace); OleDbConnection m_connection = new OleDbConnection(connectionString); OleDbCommand cmdTemp = m_connection.CreateCommand(); m_connection.Open(); cmdTemp = m_connection.CreateCommand(); cmdTemp.CommandText = "SELECT a.id,a.name_en FROM Activities a WHERE a.parent_id =" + activityID.ToString(); reader = cmdTemp.ExecuteReader(CommandBehavior.Default);----------------------

Link to comment
Share on other sites

a .aspx.cs file contains a class that inherits from the Page class. Server, Response, Request, Session, etc are all part of the Page class so any classes that do not inherit from Page cannot use those methods (not normally anyway).Make sure you have the proper using statements in your file.Post the code of the whole file and I'll point out any problems I see.

Link to comment
Share on other sites

\HBWebsite-10-9-08\App_Code\List.cs
With what aspnetguy said, aspx pages inherit from the System.Web.UI.Page which has references to various other classes in it - Server, Request, Response, etc.. I'm guessing your List class does not inherit from the System.Web.UI.Page class (not that it should, mind you).So, what would work in a web page:
string DataPlace = Server.MapPath("App_Data\\hbc_new.mdb");

Won't work in List.cs. You'll have to get it from the current HttpContext like so:

string DataPlace = System.Web.HttpContext.Current.Server.MapPath("App_Data\\hbc_new.mdb");

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...