Difference between const and readonly in c#.


Const and readonly perform a similar function on data members, but they have a few important differences.


A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const keyword and must be initialized as they are declared. For example;

public class MyClass
public const double PI = 3.14159;

PI cannot be changed in the application anywhere else in the code as this will cause a compiler error.

Constants can be marked as public, private, protected, internal, or protected internal.


A read only member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared. For example:

public class MyClass
public readonly double PI = 3.14159;


public class MyClass
public readonly double PI;

public MyClass()
PI = 3.14159;


Code for accessing master control to content page in Asp.net.


Using this code we can access the master control to content page

protected void Button1_Click(object sender, EventArgs e)
Label lblName = FindControlRecursive(this.Master, “label1”) as Label;
TextBox1.Text = lblName.Text;
public static Control FindControlRecursive(Control Root, string Id)

if (Root.ID == Id)

return Root;

foreach (Control Ctl in Root.Controls)

Control FoundCtl = FindControlRecursive(Ctl, Id);

if (FoundCtl != null)

return FoundCtl;


return null;


How to fix the limit on TextBox using Javascript code ?

Here is the simple JavaScript code to do this task

<%@ Page Title=”” Language=”C#” MasterPageFile=”~/MasterPage.master” AutoEventWireup=”true” CodeFile=”JavaScriptTest.aspx.cs” Inherits=”Default2″ %>

<asp:Content ID=”Content2″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>
<asp:TextBox ID=”txt_HeadLine” onkeyup=”chkmsg1(‘txt_HeadLine’)”  runat=”server” Height=”97px” TextMode=”MultiLine”
&nbsp;<asp:Label ID=”lbl_HeadlineCount” runat=”server” Text=””></asp:Label>

<script language=”javascript”>

function chkmsg1(as) {
var a = document.getElementById(‘<%=txt_HeadLine.ClientID%>’).value.length;
var len = document.getElementById(‘<%=txt_HeadLine.ClientID%>’).value.length;
var str = document.getElementById(‘<%=txt_HeadLine.ClientID%>’).value;

a = parseInt(a);
a = 75 – a;

if (a >= 0) {
document.getElementById(‘<%=lbl_HeadlineCount.ClientID%>’).innerHTML = a + ‘ Remaining’;
else {
var x = str.substring(0, 75);
document.getElementById(‘<%=txt_HeadLine.ClientID%>’).value = x;
return true;



Synatx for fetching data using Generic and DataReader in DataAccess Layer.

public List<EmailTemplateBE> FetchEmailtemplate()

List<EmailTemplateBE> results = new List<EmailTemplateBE>();
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings[“Test”].ConnectionString);
SqlCommand cmd = new SqlCommand(“TemplateFetch”, con);
cmd.CommandType = CommandType.StoredProcedure;
using (con)
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
EmailTemplateBE obj = new EmailTemplateBE();
obj.Id = (int)dr[“Id”];
obj.Title = (string)dr[“Title”];
obj.Description = (string)dr[“Description”];

return results;