Hi
namespace nsDal
{
public abstract class clscon
{
protected SqlConnection con = new SqlConnection();
public clscon()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
}
}
Now i want to open connection in Web Page . How it shoul be done.
Thanks
Loading
VulpesPosted Jun 9, 2011, 5:08 AM
Although you need to create an instance of a class which derives from clscon in order to open the connection created in the abstract class, you don't need to redeclare the protected field 'con' which will give you a compiler error saying that you're hiding the base class member and, worse still, will reset it to a new SqlConnection object with no associated connection string. So I'd do instead:
namespace nsDal
{
public abstract class clscon
{
protected SqlConnection con = new SqlConnection();
protected clscon()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
}
public class clscon1 : clscon
{
public void OpenConnection()
{
con.Open();
}
}
}
// code to open connection
clscon1 con1 = new clscon1();
con1.OpenConnection();
DarilPosted Nov 22, 2021, 8:48 AM
Posted Jun 9, 2011, 12:31 AM
http://www.codeproject.com/KB/cs/jmabstractclasses.aspx
namespace nsDal
{
public abstract class clscon
{
protected SqlConnection con = new SqlConnection();
public clscon()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
}
public class clscon1 :clscon
{
protected SqlConnection con = new SqlConnection();
public clscon1() : base ()
{
}
}
}
HTH