Thursday, June 25, 2009

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.


I keep getting the following errors on one of my application sites after clicking for many times:

"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."


Below was my code:

SqlConnection conn = new SqlConnection(DataAccess.GetConnectionString());
conn.Open();
SqlDataSource1.SelectCommand = sql;
SqlDataSource1.SelectParameters.Clear();
Repeater1.DataSource = SqlDataSource1;
Repeater1.DataSourceID = string.Empty;
Repeater1.DataBind();
conn.Close();


I found later, this typically happens when connections are not closed after they are used. You should put everything in a try/catch/finally, with the connection being closed in the Finally to ensure that it always gets closed no mater what.

Hence the solution is:

SqlConnection conn = new SqlConnection(DataAccess.GetConnectionString());
conn.Open();
try
{
SqlDataSource1.SelectCommand = sql;
SqlDataSource1.SelectParameters.Clear();
Repeater1.DataSource = SqlDataSource1;
Repeater1.DataSourceID = string.Empty;
Repeater1.DataBind();
}

finally
{
SqlConnection.ClearPool(conn);
SqlConnection.ClearAllPools();
}

Note: If there is any return statement before you close the connection, there might be a connection leak ; be careful on that.

Wednesday, June 10, 2009

Return value from stored procedure to c#

Suppose you have a stored procedure which returns a bit value 0 or 1. If you want to call that procedure from c# and get that bit value, do like this below:

let say, the stored procedure has:

Name: dbo.sp_dateCheck

Variables:
@laptop varchar(50),
@startdate DateTime,
@enddate DateTime

and return bit variable: @CondFlag

SqlCommand com = new SqlCommand("dbo.sp_dateCheck", conn);
com.CommandType = CommandType.StoredProcedure;

try
{
conn.Open();
com.Parameters.Add(new SqlParameter("@laptop", Laptop.Text));
com.Parameters.Add(new SqlParameter("@startdate", startDate. DateTime));
com.Parameters.Add(new SqlParameter("@enddate", endDate. DateTime));

//read the returned value from stored procedure
com.Parameters.Add("@CondFlag", SqlDbType.Bit);
com.Parameters["@CondFlag"].Direction = ParameterDirection.ReturnValue;
com.ExecuteNonQuery();
int ReturnedVal = (int)com.Parameters["@CondFlag"].Value;

if (ReturnedVal == 0)
{ // do this
}

else
{//do this
}
}

catch
{
}

Happy Programming!!!

Monday, May 25, 2009

Running a SQL query to replace certain characters in a table

Let say,
You have a database that contains many characters and symbols. If you want to replace those , just run the following SQL query:

(Here, suppose a symbol ~! need to replace by - then your query will be:)

UPDATE table
SET ColumnName= replace(LTRIM(RTRIM(ColumnName)), '~!', '-')

Happy Programming!:)

Sunday, May 10, 2009

Master page with Searchbox

Consider a master page with a search functionality and other number of content pages derived from that master page, so that searchbox appears in all pages. If you want to display a search results in Search.aspx page no matter where you are( in other content pages),add this code:

In MasterPage.cs:
Protected void search_button_Click(object sender, EventArgs e)
{
//redirect textbox's value to search.aspx page
Response.Redirect(@"~/search.aspx?q=" + Server.UrlEncode(txtSearch.Text));
}

In Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
//read the master page's redirected value
string str = Request.QueryString[@"q"];
}

--Happy Coding