In this article I will explain with an example, how to dynamically add meta tags in ASP.Net Website.
The dynamic meta tags will be populated from database and each meta tag will be added dynamically to head section of page.
I have made use of the following table MetaTags with the schema as follows.
Dynamically add meta tags in ASP.Net
I have already inserted few records in the table.
Dynamically add meta tags in ASP.Net
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
You will need to import the following namespaces.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.HtmlControls
Dynamically adding Meta Tags from Database in ASP.Net
Inside the Page Load event, first the name of the Current Page is determined from the Request.Url.Segments property.
Note: For more details on determining the name of Current Page, please refer my article Get Current Page and Previous Page names in ASP.Net.
Once the name of the Current Page is determined, the Title, Keywords and the Description values are fetched from the Database Table.
Finally the Title, Keywords and the Description are added to the Page Head section using the HtmlMeta class objects.
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();
    //Add Description Meta Tag.
    HtmlMeta description = new HtmlMeta();
    description.HttpEquiv = "description";
    description.Name = "description";
    description.Content = dtMeta.Rows[0]["Description"].ToString();
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();
                return dt;
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()
    'Add Description Meta Tag.
    Dim description As New HtmlMeta()
   description.HttpEquiv = "description"
    description.Name = "description"
    description.Content = dtMeta.Rows(0)("Description").ToString()
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()
                Return dt
            End Using
        End Using
    End Using
End Function
Dynamically add meta tags in ASP.Net