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