Retrieve database values using Dynamic Keyword in C# 4.0

This keyword was introduced in .NET 4.0. It provides the access to DLR (Dynamic Language Runtime) using this specific keyword. It intimates the compiler to perform dynamic calling which is resolved at the run time. It uses namespace. It simplifies tasks with interoperability providing access to COM API's and Dynamic API's.

Now, we will not spend more time in studying theory, It time to go and do a simple practical demo.

So, Firstly I have created simple database which looks like this:

dynamic 1.png
Let's create new project with unique name:

The Complete Code of Default.aspx file looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Dynamic_Keyword.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head id="Head1" runat="server">
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099"  HorizontalAlign="Center" />

<RowStyle BackColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle <br /> <br />
<center><asp:Button ID="Button1" runat="server" Text="Click Here" onclick="Button1_Click" />

We are now using dynamic keyword by adding some properties and method which enables to retrieve database values:

 dynamic p = new ExpandoObject();
 p.Title = 
"Simple GridView Using Dynamic";
 p.Data = new 
Action(() =>

The Complete Code of Code Behind files for this looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;using System.Web.UI.WebControls;

using System.Dynami;
using System.Data.SqlClient;
using System.Data;
namespace Dynamic_Keyword
    partial class WebForm1 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
        protected void Button1_Click(object sender, EventArgs e)
            dynamic p = new ExpandoObject();
            p.Title = "Simple GridView Using Dynamic";

            p.Data = new Action(() => { DataSet ds = new DataSet(); SqlConnectio
con = new SqlConnection(@"Data Source=VIJAY-PC\SQLEXPRESS;InitialCatalog=Candidate;Integrated Security=True");

                SqlDataAdpter da = new SqlDataAdapter("Select * from Student", con);

                GridView1.DataSource = ds;
                GridView1.DataBind(); });
            Response.Write("<center><b>" + p.Title + "</center></b>"); p.Data();

The output of the Program looks like this:


After Button Click:


I hope this article is useful for you.. I look forward for comments and feedback..Thanks Vijay Prativadi.

Next Recommended Reading Custom Alert Message using Ajax

MVC Corporation
MVC Corporation is consulting and IT services based company.