i am new in concept of dispose and finalize ,but i ask my question
I have a form which named products.aspx all codes are in products.aspx.cs which in it i have these codes
SqlConnection myConnection;
private void Page_Load(object sender, System.EventArgs e)
{
String ConnectionInfo=ConfigurationSettings.AppSettings["ConnectionInfo"];
myConnection=new SqlConnection(ConnectionInfo);
if (!Page.IsPostBack)
BindPage();
}
public void BindPage()
{
String SelCmd="Select * from TblFRProduct order by Prod_id";
SqlCommand myCommand=new SqlCommand(SelCmd,myConnection);
myCommand.Connection.Open();
SqlDataReader dr= myCommand.ExecuteReader();
Repeater1.DataSource=dr;
Repeater1.DataBind();
dr.Close();
myCommand.Connection.Close(); <------
CommonFunction myObject;
myObject = new CommonFunction(); <------
....... /* someworks on myObject
GetProdInfo(MinProd_Id);
}
private void Page_Unload(object sender, System.EventArgs e)
{
myConnection.Close(); <----
}
public void GetProdInfo(int MinId)
{
String SelCmd="Select * from TblFRProduct Where prod_id=@id";
SqlCommand myCommand=new SqlCommand(SelCmd,myConnection);
myCommand.Connection.Open(); <--------- Error
myCommand.CommandText=SelCmd;
myCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int));
myCommand.Parameters["@id"].Value=MinId;
SqlDataReader dr= myCommand.ExecuteReader();
dr.Read();
LblProduct.Text=dr["description"].ToString();
dr.Close();
myCommand.Connection.Close();
}
I have closed the connections and objects but what if i want to dispose it ( if i should it here)?
so instead of myCommand.Connection.Close(); i did as follwoing
myCommand.Connection.Dispose();
it worked ( i mean no error) but i got error in GetProdInfo() Function.
is there anyone can help me what i have done wrong or what if i have not understand the whole concept.
thanks
I have a form which named products.aspx all codes are in products.aspx.cs which in it i have these codes
SqlConnection myConnection;
private void Page_Load(object sender, System.EventArgs e)
{
String ConnectionInfo=ConfigurationSettings.AppSettings["ConnectionInfo"];
myConnection=new SqlConnection(ConnectionInfo);
if (!Page.IsPostBack)
BindPage();
}
public void BindPage()
{
String SelCmd="Select * from TblFRProduct order by Prod_id";
SqlCommand myCommand=new SqlCommand(SelCmd,myConnection);
myCommand.Connection.Open();
SqlDataReader dr= myCommand.ExecuteReader();
Repeater1.DataSource=dr;
Repeater1.DataBind();
dr.Close();
myCommand.Connection.Close(); <------
CommonFunction myObject;
myObject = new CommonFunction(); <------
....... /* someworks on myObject
GetProdInfo(MinProd_Id);
}
private void Page_Unload(object sender, System.EventArgs e)
{
myConnection.Close(); <----
}
public void GetProdInfo(int MinId)
{
String SelCmd="Select * from TblFRProduct Where prod_id=@id";
SqlCommand myCommand=new SqlCommand(SelCmd,myConnection);
myCommand.Connection.Open(); <--------- Error
myCommand.CommandText=SelCmd;
myCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int));
myCommand.Parameters["@id"].Value=MinId;
SqlDataReader dr= myCommand.ExecuteReader();
dr.Read();
LblProduct.Text=dr["description"].ToString();
dr.Close();
myCommand.Connection.Close();
}
I have closed the connections and objects but what if i want to dispose it ( if i should it here)?
so instead of myCommand.Connection.Close(); i did as follwoing
myCommand.Connection.Dispose();
it worked ( i mean no error) but i got error in GetProdInfo() Function.
is there anyone can help me what i have done wrong or what if i have not understand the whole concept.
thanks