hello all,

as i know that if we don't want to specify the static path of databse

then ,

we can use "Server.mapPath" like,( in any .aspx page )

str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("student1.mdf") + ";" + "Integrated Security=True;User Instance=True";

but my question is that ,

why this "Server.mapPath" can't work with in the any .class file

in c# ( and also vb.net )


/// <summary>
/// Summary description for sql_data
/// </summary>
public class sql_data
    public SqlConnection conection = new SqlConnection();
    public SqlCommand sql1 = new SqlCommand();
       public string str;
	public sql_data()
        str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("student1.mdf") + ";" + "Integrated Security=True;User Instance=True";
                    this.conection = new SqlConnection(str); 

because i want to make a class wich contain sql conection,sql command & adapter etc which can define in the class & can be used
in my entire web site

pls help me to solve this problem
i have search so many web site but i can't get proper web site

thanx in advance

Edited by ~s.o.s~: Moved to forums from code snippet

8 Years
Discussion Span
Last Post by john_beginner

Make a static variable with your connection string and set it from the windows form or ASP.NET web app when the code initializes and use that. You don't need to construct it every time.


Hi John,

Why do you want to construct the connection string dynamically in your class file?

Store the connection information in web.config file for web applications and app.config file for WinForms application. You can retrieve the conneciton information using System.Configuration.ConnectionStringSettings class in your class file.

For your question, You can use Server.MapPath in your class file using HttpContext object. Try System.Web.HttpContext.Current.Server.MapPath method.

Edited by Ramesh S: n/a


It's really works !!

thnak u so much

n whold u pls give me any example (or any web site address ) how can use web.config file for connection

one agin thank u ...!!


Place your connection information in web.config. Assume that your database is in App_data folder of your of web site.


<add name="TestConnectionString" connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|student1.mdf;Integrated Security=True;User Instance=True"  providerName="System.Data.SqlClient"/>

To use the connection string from web.config

Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);

    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(
            "SELECT * FROM Customers", connection);
    DataSet dsCustomers = new DataSet();

Edited by Ramesh S: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.