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 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 as 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, first the records of the Facebook OpenGraph MetaTags are fetched into a DataTable using the GetData method.
Then a loop is executed and each Facebook OpenGraph MetaTag is added to an object of HtmlMeta class.
Note: The HtmlMeta class is special in-built class for Meta Tags.
 
Finally the object of HtmlMeta class is ultimately to the Page Header (HEAD section).
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 constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Name, Content, Property FROM OpenGraphTags", con))
        {
           cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                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 constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT Name, Content, Property FROM OpenGraphTags", con)
            cmd.CommandType = CommandType.Text
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dt As DataTable = New DataTable()
                    sda.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Using
End Function
 
 
Screenshot
Dynamically add FaceBook OpenGraph MetaTags from Database in ASP.Net
 
 
Downloads