In this article I will explain with an example, how to dynamically add Facebook OpenGraph MetaTags from SQL Server database in ASP.Net using C# and VB.Net.
The different Facebook OpenGraph MetaTags such as og:title, og:description, og:image, og:url, and og:type will be saved into a SQL Server database table and will be dynamically added to the Page Header (HEAD section).
 
 
Database
I have made use of the following table OpenGraphTags with the schema follows.
Dynamically add FaceBook OpenGraph MetaTags from Database in ASP.Net
 
I have already inserted few records in the table.
Dynamically add FaceBook OpenGraph MetaTags from Database in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
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 adding FaceBook OpenGraph MetaTags from Database in ASP.Net
Inside the Page Load event handler, the GetData method is called.
Inside the GetData method, the values of Facebook OpenGraph MetaTags i.e. Name, Content, Property are fetched from the SQL Server database.
All values are set to an object of the DataTable class and the DataTable is returned.
A FOR EACH loop is executed over the rows of the returned DataTable and an object of HtmlMeta class is created and Name and Content values are set.
Note: The HtmlMeta class is special in-built class for Meta Tags.
 
Then, the property value is set using Attribute property of the HtmlMeta class and a new line is added using NewLine property of Environment class.
Finally, the object of HtmlMeta is added as control to the Page Header (HEAD section) using Add method.
C#
protected void Page_Load(object sender, EventArgs e)
{
    //Fetch the Facebook OpenGraph Meta Tags from Database.
    DataTable dt = this.GetData();
 
    foreach (DataRow row in dt.Rows)
    {
        //Populate the HtmlMeta object from Database Row.
        HtmlMeta meta = new HtmlMeta
        {
            Name = row["Name"].ToString(),
            Content = row["Content"].ToString()
        };
        meta.Attributes["property"] = row["Property"].ToString();
 
        //Add a Line break.
        this.Page.Header.Controls.Add(new Literal() { Text = Environment.NewLine });
 
        //Add the HtmlMeta object to the Page Header (HEAD Tag).
        this.Page.Header.Controls.Add(meta);
    }
 
    //Add a Line break.
    this.Page.Header.Controls.Add(new Literal() { Text = Environment.NewLine });
}
 
private DataTable GetData()
{
    string query = "SELECT Name, Content, Property FROM OpenGraphTags";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
        {
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                return dt;
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    'Fetch the Facebook OpenGraph Meta Tags from Database.
    Dim dt As DataTable = Me.GetData()
 
    For Each row As DataRow In dt.Rows
        'Populate the HtmlMeta object from Database Row.
        Dim meta As HtmlMeta = New HtmlMeta With {
            .Name = row("Name").ToString(),
            .Content = row("Content").ToString()
        }
        meta.Attributes("property") = row("Property").ToString()
 
        'Add a Line break.
        Me.Page.Header.Controls.Add(New Literal() With {
            .Text = Environment.NewLine
        })
 
        'Add the HtmlMeta object to the Page Header (HEAD Tag).
        Me.Page.Header.Controls.Add(meta)
    Next
 
    'Add a Line break.
    Me.Page.Header.Controls.Add(New Literal() With {
        .Text = Environment.NewLine
    })
End Sub
 
Private Function GetData() As DataTable
    Dim query As String = "SELECT Name, Content, Property FROM OpenGraphTags"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
            Using dt As DataTable = New DataTable()
                sda.Fill(dt)
                Return dt
            End Using
        End Using
    End Using
End Function
 
 
Screenshot
Dynamically add FaceBook OpenGraph MetaTags from Database in ASP.Net
 
 
Downloads