common public variables in Class file using C#

Posted by Dhilip under C# on 6/1/2008 | Views : 4176 | Status : [Member] | Replies : 2
Hi all,
im using ASP.NET with C# web application.i need to all the Public variables in one Class File ,for example,the ADO objects like SqlConnection conn,
SqlCommand cmd,SqlDataReader dr in the class file.
instead of writing this every webpage ,i need to add this in separate class file need to call this every web page ,how to do this using C#




Responses

Posted by: Raja on: 6/2/2008 [Member] Starter

Up
0
Down
I think it is not suggestable to create variable like SqlConnection, SqlCommand and SqlReader in public scope as in case of heavy load on your application, it may crash or throw several tough to solve error in later stage.

However, If you want to do it, you can create a parent class and create Properties of these variables. In all other class you just need to inherit this Parent class to use them

like
public class Parent

{
private SqlConnection _conn = new SqlConnection();
public SqlConnection conn
{
get {return _conn; }
}
}

public class yourClass : Parent
{
void LoadData()
{
SqlConnection = conn // conn property from parent class

}

}


You can use the modifiers (private, public) as per your security need.

Its recommended and standard to use 3-architecture in developing your application. See this article http://dotnetfunda.com/articles/article71.aspx

Thanks

Regards,
Raja, USA

Dhilip, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jaydeep on: 10/7/2008 [Member] Starter

Up
0
Down
Create one class to hold the connection variables
eg
public class AccessHelper
{
private static string strConnectionString;
private static string m_connectionStringProvider;// = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
private static OleDbConnection objConnection;

public static string StrConnectionString
{
get { return CAccessSqlHelper.strConnectionString; }
set { CAccessSqlHelper.strConnectionString = value; }
}

public static OleDbTransaction BeginTransaction
{
get
{
OpenConnection();
return objConnection.BeginTransaction();
}
}
public static string ConnectionStringProvider
{
get
{
return m_connectionStringProvider;
}
set
{
m_connectionStringProvider = value;
}
}

public static void OpenConnection()
{
objConnection = new OleDbConnection(ConnectionStringProvider);
objConnection.Open();
}
}
And assign these variables
Declare properties as static because that are same throughout the application.


Dhilip, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response