简体   繁体   中英

Timeout connection error while executing this piece of code in ASP.NET 2.0 and C#

I am using the below code to grab the page tiles, meta description from database created in SQL Server 2005. My site is built with ASP.NET 2.0 and C#.

On page_load I am executing this code:

string query = "select MetaDescription, MetaKeywords, H1Text, Ptitle, H2Text FROM SeoText Where CatalogItemId ='" + this.CurrentEnsemble.ItemId + "'";

SqlConnection myconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString);

SqlCommand SqlCmd = null;
SqlCmd = new SqlCommand(query, myconnection);

SqlDataAdapter ad = new SqlDataAdapter(SqlCmd);

DataTable dt = new DataTable();
ad.Fill(dt);

if (dt.Rows[0]["Ptitle"].ToString() == "")
{
   this.Page.Title = this.CurrentEnsemble.Title;
}
else
{
   this.Page.Title = this.CurrentEnsemble.Title + " " + dt.Rows[0]["Ptitle"].ToString();
}

HtmlMeta metaDesc = (HtmlMeta)Page.Header.FindControl("metaDesc");

if (dt.Rows[0]["MetaDescription"].ToString() == "")
{
   metaDesc.Attributes.Add("Content", this.CurrentEnsemble.Title );
}
else
{
   metaDesc.Attributes.Add("Content", dt.Rows[0]["MetaDescription"].ToString());
}

HtmlMeta metaKey = (HtmlMeta)Page.Header.FindControl("metaKey");

if (dt.Rows[0]["MetaKeywords"].ToString() == "")
{
   metaKey.Attributes.Add("Content", "site general text");
}
else
{
   metaKey.Attributes.Add("Content", dt.Rows[0]["MetaKeywords"].ToString());
}

HtmlGenericControl h1 = (HtmlGenericControl)Master.Master.Master.FindControl("h1top");

if (dt.Rows[0]["H1Text"].ToString() == "")
{
    h1.InnerText = "site general text";
}
else
{
    h1.InnerText = this.CurrentEnsemble.Title + " " + dt.Rows[0]["H1Text"].ToString();
}

HtmlGenericControl h2 = (HtmlGenericControl)Master.Master.Master.FindControl("h2bottom");

if (dt.Rows[0]["H2Text"].ToString() == "")
{
    h2.InnerText = "site general text";
}
else
{
    h2.InnerText = this.CurrentEnsemble.Title + " " + dt.Rows[0]["H2Text"].ToString();
}

The error is thrown at

ad.Fill(dt)

I am not sure where I am making the mistake.

Thanks and appreciate it

Try adding with (nolock) statements to your statment. Similar problem here: SqlDataAdapter.Fill() Timeout - Underlying Sproc Returns Quickly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM