In this article I will explain how to dynamically populate Title and Meta Tags like Keywords from and Description database and programmatically add to page head section in ASP.Net.
 
Database
For this article I have created a database with the following structure
ASP.Net Meta Tags: Programmatically populate Title, Keywords and Description from database
 
It stores the Name of the Page, its Title, Keywords and Description as shown below
ASP.Net Meta Tags: Programmatically populate Title, Keywords and Description from database
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.HtmlControls
 
 
Dynamically fetching the Meta Tags from Database and programmatically adding to page
The following code is executed in the Page Load event of page. Firstly we need to determine the page, for that I am making use of Request.Url.Segments which consists of the parts of URL and the last part contains the name of the Page for example. If the URL of the page is http://localhost:6496/DemoProject/Default.aspx, then the last segment will be Default.aspx.
Once we get the name of the page, a database call is made and using the Page Name, the Title, Keywords and the Description is fetched.
Finally the Title, Keywords and the Description are added to the Page Head section
C#
protected void Page_Load(object sender, EventArgs e)
{
    string page = Request.Url.Segments[Request.Url.Segments.Length - 1];
    DataTable dtMeta = this.GetData(page);
 
    //Add Page Title
    this.Page.Title = dtMeta.Rows[0]["Title"].ToString();
 
    //Add Keywords Meta Tag
    HtmlMeta keywords = new HtmlMeta();
    keywords.HttpEquiv = "keywords";
    keywords.Name = "keywords";
    keywords.Content = dtMeta.Rows[0]["Keywords"].ToString();
    this.Page.Header.Controls.Add(keywords);
 
    //Add Description Meta Tag
    HtmlMeta description = new HtmlMeta();
    description.HttpEquiv = "description";
    description.Name = "description";
    description.Content = dtMeta.Rows[0]["Description"].ToString();
    this.Page.Header.Controls.Add(description);
}
 
private DataTable GetData(string page)
{
    string query = "SELECT Title, Description, Keywords FROM MetaTags WHERE LOWER(Page) = LOWER(@Page)";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Page", page);
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim page As String = Request.Url.Segments(Request.Url.Segments.Length - 1)
    Dim dtMeta As DataTable = Me.GetData(page)
 
    'Add Page Title
    Me.Page.Title = dtMeta.Rows(0)("Title").ToString()
 
    'Add Keywords Meta Tag
    Dim keywords As New HtmlMeta()
    keywords.HttpEquiv = "keywords"
    keywords.Name = "keywords"
    keywords.Content = dtMeta.Rows(0)("Keywords").ToString()
    Me.Page.Header.Controls.Add(keywords)
 
    'Add Description Meta Tag
    Dim description As New HtmlMeta()
    description.HttpEquiv = "description"
    description.Name = "description"
    description.Content = dtMeta.Rows(0)("Description").ToString()
    Me.Page.Header.Controls.Add(description)
End Sub
 
Private Function GetData(page As String) As DataTable
    Dim query As String = "SELECT Title, Description, Keywords FROM MetaTags WHERE LOWER(Page) = LOWER(@Page)"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand(query)
            Using sda As New SqlDataAdapter()
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@Page", page)
                cmd.Connection = con
                sda.SelectCommand = cmd
                Dim dt As New DataTable()
                sda.Fill(dt)
                Return dt
            End Using
        End Using
    End Using
End Function
 
The following screenshot displays the Title, Keywords and Description Meta tags
ASP.Net Meta Tags: Programmatically populate Title, Keywords and Description from database
 
 
Downloads