public abstract class clscon
{
protected SqlConnection con = new SqlConnection();
public clscon()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
}
public class clscon1 : clscon
{
public void OpenConnection()
{
con.Open();
}
}
public Int32 GetAuto()
{
nsDal.clscon1 con1 = new nsDal.clscon1();
con1.OpenConnection();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select isnull(max(orgcod),0) from tborg";
Int32 v_no = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
return v_no + 1;
}
It is giving error "ExecuteScalar . Connection has not been initialised".
VulpesPosted Jun 9, 2011, 11:55 AM
Posted Jun 9, 2011, 9:53 AM
cmd.Connection = con1;
Before executing the query, You need assign the command.connection of your connnection.
public Int32 GetAuto()
{
nsDal.clscon1 con1 = new nsDal.clscon1();
con1.OpenConnection();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select isnull(max(orgcod),0) from tborg";
cmd.Connection = con1; //make sure , the con1 is opened its database connection.
Int32 v_no = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
return v_no + 1;
}