Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dispose

Status
Not open for further replies.

nnmmss72

Programmer
May 14, 2006
110
IR
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
 
nnmmss72,

I've just tried to compile your code (I had to take a few things out - what was Repeater1 supposed to be?).

It ran fine the first time (i.e. it ended without throwing an exception). Then I tried changing
Code:
myCommand.Connection.Close();
inside the BindPage method to
Code:
myCommand.Connection.Dispose();
and received a "ConnectionString has not been properly initialized" error inside the GetProdInfo() method. This seems to be the same experience that you're getting.

I think the answer is that your program can't reopen the connection inside GetProdInfo because the whole object has been binned. You wouldn't normally dispose of an object until you're completely finished with it. So don't dispose of myCommand until the end of your code - you can add a 'finally' block at the bottom to seperate housekeeping like this (check out for more info on this)

Hope this helps. Oh, and by the way, if you're posting code in the future surround it with
Code:
tags - makes it easier to read.

Mark.
 
thanks ,so i have to dispose the object excatly in GetProdInfo ,is that correct?

and also i should confess that i have read a few articel about Dispose and Finalize but i couldn't feel it , don't u know any article or website or specially sample code for a beginner?

thanks
 
Frankly, I think it gets a bit messy with these .close and .dispose methods when you can also just implement the using clause. The using clause will take care of the close and dispose for you.

Code:
public void BindPage()
{            
String SelCmd="Select * from TblFRProduct order by Prod_id";
using(SqlCommand myCommand=new SqlCommand(SelCmd,myConnection))
{
myCommand.Connection.Open();
using(SqlDataReader dr= myCommand.ExecuteReader())
{
Repeater1.DataSource=dr;
Repeater1.DataBind();
}
}        
      
}
 
thank you GoTerps88 for respose. i didn't know that.

but now i have a question about finalize an dispose, i need a very simpel sample for that, to understand it

thank u
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top