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 :