I have a method that gets sqldatareader as shown below.. my question is how
do i close the sqlconnection (objConn) object within this method?
If i put the objconn.Close() ; after the return then i get "Unreachabl e code
detected" and if i close it before the return i get "Invalid attempt to
FieldCount when reader is closed."
When i refresh the page .. lot of sqlconnection is created.. and then i get
this error message :
Timeout expired. The timeout period elapsed prior to obtaining a connection
from the pool. This may have occurred because all pooled connections were in
use and max pool size was reached.
Also instead of adding this bit of code
[(ConfigurationS ettings.AppSett ings["DSN"]);
SqlCommand selectCmd = new SqlCommand("sp_ DMListFiles",ob jConn)]
in every method.. how can i put it somewhere and reuse it in every methods??
Many thanks in advance.
public SqlDataReader DMListFiles(int DepID, int FolderID)
{
SqlConnection objConn = new
SqlConnection(C onfigurationSet tings.AppSettin gs["DSN"]);
SqlCommand selectCmd = new SqlCommand("sp_ DMListFiles",ob jConn);
selectCmd.Param eters.Add("@Dep ID",DepID);
selectCmd.Param eters.Add("@Fol derID",FolderID );
selectCmd.Comma ndType = CommandType.Sto redProcedure;
objConn.Open();
SqlDataReader dr = selectCmd.Execu teReader();
return dr;
}