In this article I will explain with an example, how to create and download vCard (.VCF) file from Database in ASP.Net using C# and VB.Net.
 
 
Database
This article makes use of table named Contacts whose schema is defined as follows.
Create and download vCard (.VCF) file from Database in ASP.Net
 
I have inserted a record in the table.
Create and download vCard (.VCF) file from Database in ASP.Net
 
Note:You can download the database table SQL by clicking the download link below. 
         Download SQL file
 
 
HTML Markup
The following HTML Markup consists of an Image and some Label controls and a Button.
The Button has been assigned an OnClick event handler.
<table>
    <tr>
        <td rowspan="10" valign="top">
            <asp:Image ID="imgPhoto" runat="server" />
        </td>
    </tr>
    <tr>
        <td style="width: 100px">Name:</td>
        <td><asp:Label ID="lblName" runat="server" /></td>
    </tr>
    <tr>
        <td style="width: 100px">Company Name:</td>
        <td><asp:Label ID="lblOrganization" runat="server" /></td>
    </tr>
    <tr>
        <td style="width: 100px">Title (Position):</td>
        <td><asp:Label ID="lblTitle" runat="server" /></td>
    </tr>
    <tr>
        <td>Mobile Number:</td>
        <td><asp:Label ID="lblMobileNumber" runat="server" /></td>
    </tr>
    <tr>
        <td>Home Number:</td>
        <td><asp:Label ID="lblHomeNumber" runat="server" /></td>
    </tr>
    <tr>
        <td>Work Number:</td>
        <td><asp:Label ID="lblWorkNumber" runat="server" /></td>
    </tr>
    <tr>
        <td>Email Address:</td>
        <td><asp:Label ID="lblEmailAddress" runat="server" /></td>
    </tr>
    <tr>
        <td>Website:</td>
        <td><asp:Label ID="lblWebsite" runat="server" /></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="txtCreate" runat="server" Text="Create VCF" OnClick="txtCreate_Click" /></td>
    </tr>
</table>
 
 
Namespaces
You need to import the following namespaces.
C#
using System.IO;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.IO
Imports System.Text
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Populating the Table with the records fetched from the Database
Inside the Page Load event handler, the records from the Contacts Table are fetched using SqlDataReader.
Then, using WHILE Loop records are set to the Image Url and Label controls.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Contacts", con))
            {
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    if (sdr.Read())
                    {
                        imgPhoto.ImageUrl = sdr["ImageUrl"].ToString();
                        lblName.Text = sdr["Name"].ToString();
                        lblOrganization.Text = sdr["CompanyName"].ToString();
                        lblTitle.Text = sdr["Title"].ToString();
                        lblMobileNumber.Text = sdr["MobileNumber"].ToString();
                        lblHomeNumber.Text = sdr["HomeNumber"].ToString();
                        lblWorkNumber.Text = sdr["WorkNumber"].ToString();
                        lblEmailAddress.Text = sdr["EmailAddress"].ToString();
                        lblWebsite.Text = sdr["Website"].ToString();
                    }
                }
                con.Close();
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If NotMe.IsPostBack Then
        Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constring)
            Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Contacts", con)
                con.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    If sdr.Read() Then
                        imgPhoto.ImageUrl = sdr("ImageUrl").ToString()
                        lblName.Text = sdr("Name").ToString()
                        lblOrganization.Text = sdr("CompanyName").ToString()
                        lblTitle.Text = sdr("Title").ToString()
                        lblMobileNumber.Text = sdr("MobileNumber").ToString()
                        lblHomeNumber.Text = sdr("HomeNumber").ToString()
                        lblWorkNumber.Text = sdr("WorkNumber").ToString()
                        lblEmailAddress.Text = sdr("EmailAddress").ToString()
                        lblWebsite.Text = sdr("Website").ToString()
                    End If
                End Using
                con.Close()
            End Using
        End Using
    End If
End Sub
 
 
Creating and Downloading vCard (.VCF) file in ASP.Net
When the Create VCF Button is clicked, the following event handler is executed.
Inside this event handler, first a StringBuilder class object is created. Then the information for creating the vCard (.VCF) is read from the Label controls and appended to the StringBuilder class object using Append function.
For the contact Photo, Image file is read from the Folder path and converted to BYTE Array and then converted to BASE64 string and finally, appended to the StringBuilder object.
Once the vCard details such as Name, Organization, Phone Numbers, Email Addresses, Websites and Photo of the contact is filled, the file is downloaded using the Response class.
Note: The ContentType of Response class is set as text/vcard which informs the Browser that the file type is a vCard.
 
C#
protected void txtCreate_Click(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("BEGIN:VCARD\r\nVERSION:2.1");
    sb.Append(Environment.NewLine);
 
    sb.Append("N:");
    sb.Append(lblName.Text.Split(' ')[0]);
    sb.Append(";");
    sb.Append(lblName.Text.Split(' ')[1]);
    sb.Append(";");
    sb.Append(Environment.NewLine);
 
    sb.Append("FN:");
    sb.Append(lblName.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("TEL;CELL:");
    sb.Append(lblMobileNumber.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("TEL;HOME:");
    sb.Append(lblHomeNumber.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("TEL;WORK:");
    sb.Append(lblWorkNumber.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("EMAIL;WORK:");
    sb.Append(lblEmailAddress.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("ORG:");
    sb.Append(lblOrganization.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("TITLE:");
    sb.Append(lblTitle.Text);
    sb.Append(Environment.NewLine);
 
    sb.Append("URL:");
    sb.Append(lblWebsite.Text);
    sb.Append(Environment.NewLine);
 
    byte[] bytes = File.ReadAllBytes(Server.MapPath("~/Images/Mudassar.png"));
    string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
    sb.Append("PHOTO;ENCODING=BASE64;PNG:");
    sb.Append(base64);
    sb.Append(Environment.NewLine);
 
    sb.Append("END:VCARD");
 
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=Mudassar.vcf");
    Response.Charset = "";
    Response.ContentType = "text/vcard";
    Response.Output.Write(sb.ToString());
    Response.Flush();
    Response.End();
}
 
VB.Net
Protected Sub txtCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("BEGIN:VCARD" & vbCrLf & "VERSION:2.1")
    sb.Append(Environment.NewLine)
 
    sb.Append("N:")
    sb.Append(lblName.Text.Split(" "c)(0))
    sb.Append(";")
    sb.Append(lblName.Text.Split(" "c)(1))
    sb.Append(";")
    sb.Append(Environment.NewLine)
 
    sb.Append("FN:")
    sb.Append(lblName.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("TEL;CELL:")
    sb.Append(lblMobileNumber.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("TEL;HOME:")
    sb.Append(lblHomeNumber.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("TEL;WORK:")
    sb.Append(lblWorkNumber.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("EMAIL;WORK:")
    sb.Append(lblEmailAddress.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("ORG:")
    sb.Append(lblOrganization.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("TITLE:")
    sb.Append(lblTitle.Text)
    sb.Append(Environment.NewLine)
 
    sb.Append("URL:")
    sb.Append(lblWebsite.Text)
    sb.Append(Environment.NewLine)
 
    Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/Images/Mudassar.png"))
    Dim base64 As String = Convert.ToBase64String(bytes, 0, bytes.Length)
    sb.Append("PHOTO;ENCODING=BASE64;PNG:")
    sb.Append(base64)
    sb.Append(Environment.NewLine)
 
    sb.Append("END:VCARD")
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=Mudassar.vcf")
    Response.Charset = ""
    Response.ContentType = "text/vcard"
    Response.Output.Write(sb.ToString())
    Response.Flush()
    Response.End()
End Sub
 
 
Screenshot
Create and download vCard (.VCF) file from Database in ASP.Net
 
 
Demo
 
 
Downloads