Difference between const and readonly in c#
Following are the difference between const and readonly . Const:
1.const can only be initialized at the declaration of the field.
2.const is static by default.
3.Value is evaluated at compile time.
Readonly :
1.Readonly can be initialized either at the declaration or in a constructor.
2.Value is evaluated at run time.
3.Readonly fields can have different values depending on the constructor used.
Example of const and readonly keywords in c# :
class MyClass
{
public readonly int y = 20; // Initialize a readonly field
public readonly int z;
public const int c1 = 5; // Initialize a const
public MyClass()
{
z = 24; // Initialize a readonly instance field in constructor.
}
}
Convert Datatable to generic list collection in c#
Introduction :In this article i will show you how to convert Datatable to generic list collection in c# . It is very easy to conversion form DataTable to generic list collection .I have showed here as step by step
Class Example
public class City
{
public int CityId { get; set; }
public string Cityname { get; set; }
}
C# Code Convert DataTable to generic list collection :
List<City> CityCollection = new List<City>();
DataSet Ds = GetDataSet("Select CountryID,CountryName from Countrytable");
CityCollection = Ds.Tables[0].AsEnumerable().Select(data => new City() { CityId = (int)data["CountryID"], Cityname = (string)data["CountryName"] }).ToList();
public static DataSet GetDataSet(string Query)
{
DataSet Ds = new DataSet();
try
{
string strCon = @"Data Source=ServerName;Initial Catalog=Test;Integrated Security=True";
SqlConnection Con = new SqlConnection(strCon);
SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
Da.Fill(Ds);
}
catch (Exception) { }
return Ds;
}
Introduction : In this article i will show you how to bind a DropDownList to avoid page refresh via a webmethod .It is very useful and many time we require to use Jquery ajax . I have here provided html and c# code behind code to understand Jquery Ajax in asp.net .
C# Code for binding Dropdownlist using Jquery Ajax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using System.Data.SqlClient;
public partial class AjaxInJquery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List GetCityData()
{
List CityCollection = new List();
try
{
DataSet Ds = GetDataSet("Select * from Countrytable");
CityCollection = Ds.Tables[0].AsEnumerable().Select(data => new City() { CityId = (int)data["CountryID"], Cityname = (string)data["CountryName"] }).ToList();
}
catch (Exception) { }
return CityCollection;
}
public static DataSet GetDataSet(string Query)
{
DataSet Ds = new DataSet();
try
{
string strCon = @"Data Source=ServerName;Initial Catalog=Test;Integrated Security=True";
SqlConnection Con = new SqlConnection(strCon);
SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
Da.Fill(Ds);
}
catch (Exception) { }
return Ds;
}
}
public class City
{
public int CityId { get; set; }
public string Cityname { get; set; }
}
Order by Ascending and Descending class List<> in C# :
Introduction : In this article i will explain you how to sort class collection ( List<> of class ) in linq c# . I done the easy code to understand how to uses Order by Ascending and Descending of class list in linq c#.
Class example for sorting class List<> In c#: Following is the user class example with constructor in c#.
public class User
{
public int UserId { get; set; }
public string Firstname { get; set; }
public User(int userId, string firstname)
{
UserId = userId;
Firstname = firstname;
}
}
Ascending and Descending Sorting class list in linq C#:
List<User> UserList = new List<User>();
UserList.Add(new User(1, "bimal"));
UserList.Add(new User(2, "punit"));
UserList.Add(new User(3, "amit"));
UserList = UserList.OrderBy(x => x.Firstname).ToList(); //Ascending Sorted the class list using linq.
UserList = UserList.OrderByDescending(x => x.Firstname).ToList(); // Descending Sorted the class list using linq.
Add,Update,Delete,paging with DetailsView in asp.net c#
Introduction: In this article i will show you how to bind DetailsView in asp.net c# . I have implemented all operation like paging ,add ,edit and delete in DetailsView in asp.net c# .
In following i have written the Css class to format the DetaildView.
Html Code for DetailsView in asp.net :
Introduction : In this article i will show you how to export GridView to excel file. Many times we require to export the GridView to excel sheet file.I have here written following code in asp.net c# to export the gridview control to excel sheet file. Html code for gridview export to excel sheet :
Introduction : In this article i will show how to detect browser in Jquery . Many times we require to detect browser and need to do code accordingly as per browser. By using following code you can detect Internet Explorer Browser ,Mozila Browser ,Safari Browser In Jquery
Jquery Code to Detect Browser :
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
if ($.browser.msie) { alert('Internet Explorer and Version is ' + $.browser.version) }
if ($.browser.safari) { alert('Safari Browser .') }
if ($.browser.mozilla) { alert('Mozila Browser and Version is ' + $.browser.version) }
});
</script>
Sorting, Paging, Add, Update, Delete in GridView asp.net c#
Introduction : In this article i will show full functional Gridview with Sorting, Paging , Add ,Update ,delete operation .The GridView control is used to display the values of a data source in a table. It is very useful asp.net control .Here I have written the code for Sorting , Paging , Update , Delete , Add with footer row .
The Code for All operations in GridVeiw that i have written is very easy understand and easy to implement in you application .
Html Code For Gridview with Sorting, Paging , Add ,Update ,delete operation In Asp.net:
Introduction : In this article i will show you how to pass the arguments to String.Format method in c# .
You need to pass comma separated list of arguments to the string.Format method .There must be argument for each of the place holders .
Following is the example of String.Format in c# :
string Name ="Seta Hamid ";
string Country ="India";
Response.Write( String.Format("My Name is {0}. I am from {1}.", Name, Country));
Get Recursive all childs by parentid in sql server
Introduction : In this article i will show you how to get all recursive childs by parentid in sql server .To get all childs i have used recursive query using with CTE ( Common Table Expressions ) . It is very simple to with parentid and childId relationship table.I have written query for getting recursive child records in sql server is as follow .
Table design for Relationship :
Query for get all childs by parentid in sql server :
declare @ParentId as int;
set @ParentId = 1;
WITH RecursiveTable (ProductId, ParentId,ProductName, Level)
AS
(
SELECT MaintTab.ProductId,
MaintTab.ParentId,
MaintTab.ProductName ,
0 AS Level
FROM ProductTable AS MaintTab
WHERE ParentId = @ParentId
UNION ALL
SELECT MaintTab.ProductId,
MaintTab.ParentId,
MaintTab.ProductName ,
LEVEL + 1
FROM ProductTable AS MaintTab
INNER JOIN RecursiveTable Rtab ON
MaintTab.ParentId = Rtab.ProductId
)
SELECT * FROM RecursiveTable
Introduction : In this article i will show you how to add Chart control in asp.net 4.0 and how to bind chart control in asp.net with Dataset .You just need to drag the chart control in aspx page so following assembly will be register in your web page and corresponding httpHandlers will be added in web config too .
Code For Bind Chart Control In asp.net :
I have following complete html and code behind code for binding Chart control in c# asp.net. I have here created Bar chart control in asp.net.
Set hidden field's value on button's click in Jquery
Introduction : In this i will show you how to set value of hidden field's value on button click usinng Jquery . It is very easy to set value of hidden field's value using Jquery .I have written two way to set value of hidden field using Jquery . In first way you can write code in $(document).ready() function and second you can directly assign to on click of button .
HTML Code for Set hidden field's value on button's click in Jquery :
Check / Uncheck all checkbox in treeview using jquery in asp.net
Introduction : In this article i will show you how check all checkboxes in treeview using Jquery in asp.net .It is very easy just you need add code and replace id as per treeview control .
For the binding the Treeview control in asp.net you can visits my previous post bind treeview in-asp.net .
Jquery for check / uncheck all checkboxes in treeview in c# :
Following is jquery code for check or uncheck all checkboxes in treeview control and HTML code in asp.net .
Introduction : In this article i will show you how to bind treeview in c# using dataset . I have created complete code to bind the treeview and also table structure in image which require for the treeview control to bind in asp.net c# using dataset .
Introduction : This article show you how to merge or combine two more doc files in Asp.net using C# .
I have created one function to merge multiple word file in c# .it is very easy to use .You just need to pass word file path arrays and save file path . Then i will save merged word document file .
Introduction : This article show you how to add validation using Jquery in Asp.net . Validation is easy in jquery just need add my following complete html code for Jquery validation in asp.net .
you can apply required validation by adding class to textbox like : CssClass="required". ..etc .so on you can Url validation ,date validation , digits validation ..etc using jquery .
Complete Html Code for Jquery validation in asp.net page :