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

VulpesRank #55 · 29.2K reputationPosted Jun 9, 2011, 1:08 AM EDT
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();
Posted Jun 8, 2011, 8:31 PM EDT
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
Daril148 reputationPosted Nov 22, 2021, 3:48 AM EST